Heap

来源:互联网 发布:淘宝网如何打造爆款 编辑:程序博客网 时间:2024/06/05 18:20


import java.util.Scanner;


class Node{

private int iData;

public Node(int key){

iData=key;

}

public int getKey(){

return iData;

}

}


class Heap{

private Node[] heapArray;

private int currentSize;

private int maxSize;

public Heap(int mx){

maxSize=mx;

currentSize=0;

heapArray=new Node[maxSize];

}

public Node remove(){

Node root=heapArray[0];

heapArray[0]=heapArray[--currentSize];

trickleDown(0);

return root;

}

public 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 void displayHeap(){

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.println(' ');

System.out.println(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.println(' ');

}

System.out.println("\n"+dots+dots);

}

public void displayArray(){

for(int j=0;j<maxSize;j++)

System.out.print(heapArray[j].getKey()+" ");

System.out.println(' ');

}

public void insertAt(int index,Node newNode){

heapArray[index]=newNode;

}

public void incrementSize(){

currentSize++;

}

}


public class app {


public static void main(String[] args) {

int size,j;

Scanner sc=new Scanner(System.in);

System.out.println("enter number of items");

size=sc.nextInt();

Heap theHeap=new Heap(size);

for(j=0;j<size;j++){

int random=(int)Math.random()*100;

Node newNode=new Node(random);

theHeap.insertAt(j,newNode);

theHeap.incrementSize();

}

System.out.print("random: ");

theHeap.displayArray();

for(j=size/2-1;j>=0;j--)

theHeap.trickleDown(j);


System.out.print("heap: ");

theHeap.displayArray();

theHeap.displayHeap();

for(j=size-1;j>=0;j--){

Node biggestNode=theHeap.remove();

theHeap.insertAt(j,biggestNode);

}

System.out.print("sorted: ");

theHeap.displayArray();

sc.close();

}


}