package Heap;
/**
* 堆有如下的特点:
* 它是完全二叉树,除了最后一层不需要满,其他的都需要满
* 常常用一个数组实现
* 堆中的每一个节点满足堆的条件每一个节点的关键字都大于这个节点的子关键字
* @author Administrator
*
*/
public class Heap {
private Node [] heapArray;
private int maxSize;
private int currentSize;
public Heap(int mx){
maxSize = mx;
currentSize = 0;
heapArray = new Node[maxSize];
}
//判断是不是为空
public boolean isEmpty(){
return currentSize==0;
}
//插入元素的方法的实现
public boolean insert(int key){
if(currentSize == maxSize){
return false;
}
Node newNode = new Node(key);
heapArray[currentSize] = newNode;
trickleUp(currentSize++);
return true;
}
private void trickleUp(int index) {
int parent = (index-1)/2;
Node bottom = heapArray[index];
while(index > 0 && heapArray[parent].getKey() < bottom.getKey()){
heapArray[index] = heapArray[parent];//向下移动
index = parent;
parent = (parent-1)/2;
}
heapArray[index] = bottom;
}
//删除元素
public Node remove(){
Node root = heapArray[0];
heapArray[0] = heapArray[--currentSize];
trickleDown(0);
return root;
}
//向下筛选
private void trickleDown(int index) {
int largerChild;
Node top = heapArray[index];
while(index < currentSize/2){
int leftChild = 2*index+1;
int rightChild = leftChild+1;
if(rightChild < currentSize && heapArray[leftChild].getKey() < heapArray[rightChild].getKey()){
largerChild = rightChild;
}else{
largerChild = leftChild;
}
if(top.getKey() >= heapArray[largerChild].getKey()){
break;
}
heapArray[index] = heapArray[largerChild];
index = largerChild;
}
heapArray[index] = top;
}
public boolean change(int index,int newValue){
if(index < 0 || index>= currentSize){
return false;
}
int oldValue = heapArray[index].getKey();//记住以前的值
heapArray[index].setKey(newValue);
if(oldValue < newValue){
trickleUp(index);
}else{
trickleDown(index);
}
return true;
}
//打印
public void displayHeap() {
System.out.print("heapArray: ");
for (int m = 0; m < currentSize; m++)
if (heapArray[m] != null)
System.out.print(heapArray[m].getKey() + " ");
else
System.out.print("-- ");
System.out.println();
int nBlanks = 32;
int itemsPerRow = 1;
int column = 0;
int j = 0;
String dots = "...............................";
System.out.println(dots + dots);
while (currentSize > 0)
{
if (column == 0)
for (int k = 0; k < nBlanks; k++)
System.out.print(' ');
System.out.print(heapArray[j].getKey());
if (++j == currentSize)
break;
if (++column == itemsPerRow)
{
nBlanks /= 2;
itemsPerRow *= 2;
column = 0;
System.out.println();
} else
for (int k = 0; k < nBlanks * 2 - 2; k++)
System.out.print(' ');
}
System.out.println("\n" + dots + dots);
}
}