📄 heap.java
字号:
import java.io.*;
import java.util.*;
import java.lang.*;
public class Heap{
String RelationName;
TableScan R;
//key for sorting
int Key=-1;
TupleNode[] tupleArray=new TupleNode[100];
int HeapPointer=0;
public Heap()
{
this(null,null,-1);
}
public Heap(String Relation,String File,int KeyIndex)
{
RelationName=Relation;
R=new TableScan(RelationName,File);
Key=KeyIndex;
}
void swap(int i,int j)
{
TupleNode tempNode=new TupleNode();
tempNode=tupleArray[i];
tupleArray[i]=tupleArray[j];
tupleArray[j]=tempNode;
tempNode=null;
}
int parent(int i)
{
int k=(i-1)/2;
return k;
}
int leftchild(int i)
{
int k=2*i+1;
return k;
}
int rightchild(int i)
{
int k=2*i+2;
return k;
}
TupleNode root()
{
TupleNode tempNode=tupleArray[0];
return tempNode;
}
//-------------------
void checkup(int i)
{
if(i!=0)
{
TupleNode a=tupleArray[i];
TupleNode b=tupleArray[parent(i)];
if(new Integer(a.Value[Key]).intValue()<=new Integer(b.Value[Key]).intValue())
{
int k=parent(i);
swap(i,k);
checkup(k);
}
}
}
void insert(TupleNode a)
{
tupleArray[HeapPointer]=a;
checkup(HeapPointer);
HeapPointer++;
}
//-------------------
int min(int i,int j)
{
if(tupleArray[i]==null)
{
return j;
}
else if (tupleArray[j]==null)
{
return i;
}
else if(new Integer(tupleArray[j].Value[Key]).intValue()<new Integer(tupleArray[i].Value[Key]).intValue())
{
return j;
}
else if(new Integer(tupleArray[j].Value[Key]).intValue()>new Integer(tupleArray[i].Value[Key]).intValue())
{
return i;
}
else
{
return j;
}
}
void checkdown(int i)
{
int k=leftchild(i);
int l=rightchild(i);
if(k<HeapPointer)
{
int min=min(k,l);
if(tupleArray[min]!=null)
{
if(new Integer(tupleArray[min].Value[Key]).intValue()<new Integer(tupleArray[i].Value[Key]).intValue())
{
swap(i,min);
}
checkdown(min);
}
}
}
void deletemin()
{
tupleArray[0]=null;
tupleArray[0]=tupleArray[HeapPointer-1];
tupleArray[HeapPointer-1]=null;
HeapPointer--;
checkdown(0);
}
//-------------------
TupleNode[] getarray()
{
return tupleArray;
}
TupleNode Minimum()
{
return tupleArray[0];
}
//Iterator functions
void Open()
{
R.Open();
String[] s=R.GetNext();
while(s!=null)
{
TupleNode t=new TupleNode(s);
insert(t);
s=R.GetNext();
}
R.Close();
}
TupleNode GetNext()
{
if(HeapPointer<=0)
{
return null;
}
TupleNode t=tupleArray[0];
deletemin();
return t;
}
void Close()
{
HeapPointer=0;
}
//main function
/*
public static void main(String args[])
{
Heap H=new Heap("R","R.txt",0);
H.R.PrintRelation();
System.out.println("\n");
H.Open();
TupleNode tuple=H.GetNext();
while(tuple!=null)
{
H.R.PrintTuple(tuple.Value);
tuple=H.GetNext();
}
H.Close();
}
*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -