자바 힙트리(heapTree) 공부중인데
소스가 도저히 이해가 안되네용..
각 줄에 대해 주석좀 달아주세요..
01 class Heap{
02 private int heapSize;
03 private int itemHeap[];
04
05 public Heap(){
06 heapSize = 0;
07 itemHeap = new int [50];
08 }
09
10 public void insertHeap(int item){
11 int i = ++heapSize;
12 while((i != 1) && (item > itemHeap[i/2])){
13 itemHeap[i] = itemHeap[i/2];
14 i/=2;
15 }
16 itemHeap[i] = item;
17 }
18
19 public int getHeapSize(){
20 return this.heapSize;
21 }
22
23 public int deleteHeap(){
24 int parent, child;
25 int item, temp;
26 item = itemHeap[1];
27 temp = itemHeap[heapSize--];
28 parent = 1; child = 2;
29
30 while(child <= heapSize){
31 if((child < heapSize) && (itemHeap[child] < itemHeap[child+1]))
32 child++;
33 if(temp >= itemHeap[child]) break;
34
35 itemHeap[parent] = itemHeap[child];
36 parent = child;
37 child *= 2;
38 }
39 itemHeap[parent] = temp;
40 return item;
41 }
42
43 public void printHeap(){
44 System.out.printf("\nHeap >>> ");
45 for(int i=1; i<=heapSize; i++)
46 System.out.printf("[%d] ", itemHeap[i]);
47 }
48 }
49
50 class Ex9_3{
51 public static void main(String args[]){
52 int n, item;
53 Heap h = new Heap();
54
55 h.insertHeap(13);
56 h.insertHeap(8);
57 h.insertHeap(10);
58 h.insertHeap(15);
59 h.insertHeap(20);
60 h.insertHeap(19);
61
62 h.printHeap();
63
64 n = h.getHeapSize();
65 for(int i=1; i<=n; i++){
66 item = h.deleteHeap();
67 System.out.printf("\n deleted Item : [%d]", item);
68 }
69 }
70 }