-
Notifications
You must be signed in to change notification settings - Fork 0
/
Heap.java
113 lines (93 loc) · 2.14 KB
/
Heap.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* Brett Waugh
* 3 December 2019
* Heap.java
* Contains the logic for the Heap Sort.
* This program is based off of heapSort.java from
* "Data Structures & Algorithms in Java" Second
* Edition by Robert Lafore.
*
*/
public class Heap {
private static Hnode[] heapArray;
private int maxSize;
private int currentSize;
public Heap(int max) {
maxSize = max;
currentSize = 0;
heapArray = new Hnode[maxSize];
}
/*
* Deletes the item with the max key.
*/
public Hnode remove() {
// (assumes non-empty list)
Hnode root = heapArray[0];
heapArray[0] = heapArray[--currentSize];
trickleDown(0);
return root;
}
/*
* Logic for the Heap Sort.
*/
public void trickleDown(int index) {
int largerChild;
Hnode top = heapArray[index];
// Not on bottom row.
while (index < currentSize / 2) {
int leftChild = 2 * index + 1;
int rightChild = leftChild + 1;
// Find larger child.
if (rightChild < currentSize && heapArray[leftChild].getKey() < heapArray[rightChild].getKey()) {
largerChild = rightChild;
} else {
largerChild = leftChild;
}
if (top.getKey() >= heapArray[largerChild].getKey()) {
break;
}
// Shift child up.
heapArray[index] = heapArray[largerChild];
// Go down.
index = largerChild;
}
// Root to index.
heapArray[index] = top;
}
/*
* Display function for Heap Sort.
*/
public static void display(int max) {
for (int i = 0; i < max; i++) {
System.out.print(heapArray[i].getKey() + ", ");
}
System.out.println("\n");
}
/*
* Inserts the data point into the heapArray.
*/
public void insertAt(int index, Hnode newNode) {
heapArray[index] = newNode;
}
/*
* Increments the size of the heap.
*/
public void incrementSize() {
currentSize++;
}
/*
* Starts the Heap Sort process.
*/
public static Heap heapCall(int max, Heap theHeap) {
// make random array into heap
for (int i = max / 2 - 1; i >= 0; i--) {
theHeap.trickleDown(i);
}
// Remove from heap and store at array end.
for (int j = max - 1; j >= 0; j--) {
Hnode biggestNode = theHeap.remove();
theHeap.insertAt(j, biggestNode);
}
return theHeap;
}
}