1
+ /**
2
+ * The `Heap` class represents a simple heap data structure.
3
+ * A heap is a specialized tree-based data structure that satisfies the heap property.
4
+ * In this implementation, the heap is represented as an array.
5
+ * The class provides methods for insertion, deletion, and heap sort.
6
+ *
7
+ */
8
+ public class Heap {
9
+
10
+ /** The current number of elements in the heap. */
11
+ int load = 0 ;
12
+
13
+ /** The array representing the heap. */
14
+ int [] hArray = new int [100 ];
15
+
16
+ /**
17
+ * Constructs an empty heap.
18
+ */
19
+ Heap () {
20
+ }
21
+
22
+ /**
23
+ * Performs the reheapUp operation to maintain the heap property after insertion.
24
+ *
25
+ * @param currentIndex The index of the current element.
26
+ */
27
+ void reheapUp (int currentIndex ) {
28
+ // Exercise 1
29
+ // add your code here
30
+ if (currentIndex > 0 ){ //if index isn't a leaf node
31
+ int parentIndex = (currentIndex - 1 )/2 ;
32
+ if (hArray [currentIndex ] > hArray [parentIndex ]){
33
+ swap (hArray , currentIndex , parentIndex );
34
+ }
35
+ reheapUp (parentIndex );
36
+ }
37
+ }
38
+
39
+ /**
40
+ * Inserts a new element into the heap and performs the necessary reheapUp operation.
41
+ *
42
+ * @param data The data to be inserted into the heap.
43
+ */
44
+ void insert (int data ) {
45
+ // Exercise 2
46
+ // add your code here
47
+ hArray [load ] = data ; //add new data to the next current data
48
+ load ++;
49
+ reheapUp (load - 1 ); //reheap up the data we inserted
50
+ }
51
+
52
+ /**
53
+ * Performs the reheapDown operation to maintain the heap property after deletion.
54
+ *
55
+ * @param currentIndex The index of the current element.
56
+ */
57
+ void reheapDown (int currentIndex ) {
58
+ // Exercise 3
59
+ // add your code here
60
+ int lastIndex = load - 1 ; // Subtract 1 to get the index of the last element in the heap
61
+
62
+ // if (currentIndex < lastIndex) {
63
+ // int largestChildIndex;
64
+ // int leftNodeIndex = 2 * currentIndex + 1;
65
+ // int rightNodeIndex = 2 * currentIndex + 2;
66
+ //
67
+ // if (rightNodeIndex <= lastIndex) { // Check if the right child exists within the bounds
68
+ // if (hArray[rightNodeIndex] > hArray[leftNodeIndex]) { //if rightNode is the largest child
69
+ // largestChildIndex = rightNodeIndex;
70
+ // } else { //if leftNode is the largest child
71
+ // largestChildIndex = leftNodeIndex;
72
+ // }
73
+ // } else { // If there's no right child, use the left child
74
+ // largestChildIndex = leftNodeIndex;
75
+ // }
76
+ //
77
+ // if (hArray[currentIndex] < hArray[largestChildIndex]) {
78
+ // swap(hArray, currentIndex, largestChildIndex);
79
+ // reheapDown(largestChildIndex);
80
+ // }
81
+ // }
82
+
83
+ //same method as in the lab's skeleton
84
+ if (2 *currentIndex + 1 <= lastIndex ){
85
+ int largestChildIndex ;
86
+ int leftNodeIndex = 2 * currentIndex + 1 ;
87
+ int rightNodeIndex = 2 * currentIndex + 2 ;
88
+
89
+ if (rightNodeIndex > lastIndex ){
90
+ largestChildIndex = leftNodeIndex ;
91
+ } else if (hArray [rightNodeIndex ] > hArray [leftNodeIndex ]) {
92
+ largestChildIndex = rightNodeIndex ;
93
+ } else {
94
+ largestChildIndex = leftNodeIndex ;
95
+ }
96
+
97
+ if (hArray [currentIndex ] < hArray [largestChildIndex ]) {
98
+ swap (hArray , currentIndex , largestChildIndex );
99
+ reheapDown (largestChildIndex );
100
+ }
101
+ }
102
+ }
103
+
104
+ /**
105
+ * Deletes the root element of the heap and performs the necessary reheapDown operation.
106
+ *
107
+ * @return The value of the root element that was deleted.
108
+ */
109
+ int deleteRoot () {
110
+ // Exercise 4
111
+ // add your code here
112
+ int temp = hArray [0 ]; //temp = root(array index 0) for return
113
+ if (load - 1 >= 0 ){ //if last index > 0 -> if level > 0 -> root has children
114
+ hArray [0 ] = hArray [load -1 ]; //replace the data at root with the latest data
115
+ hArray [load -1 ] = 0 ; //clear the last data
116
+ load --; //update the load value
117
+ reheapDown (0 ); //call reheap down starting at Index 0
118
+ }
119
+ return temp ;
120
+ }
121
+
122
+ /**
123
+ * Sorts the heap in ascending order.
124
+ */
125
+ void makeHeapSort () {
126
+ // Exercise 5
127
+ // add your code here
128
+ while (load > 0 ){ //do it until no nodes left
129
+ System .out .print (deleteRoot () + " " );
130
+ }
131
+ System .out .println ();
132
+ }
133
+
134
+ /**
135
+ * Finds the level of a given node in the heap.
136
+ *
137
+ * @param nodeIndex The index of the node.
138
+ * @return The level of the node in the heap.
139
+ */
140
+ int findLevel (int nodeindex ) {
141
+ int parent = (nodeindex - 1 ) / 2 ;
142
+ int level = 0 ;
143
+
144
+ if (parent > 0 )
145
+ level ++;
146
+
147
+ while (parent > 0 ) {
148
+ parent = (parent - 1 ) / 2 ;
149
+ level ++;
150
+ }
151
+ return level ;
152
+ }
153
+
154
+ /**
155
+ * Swaps two elements in the heap array.
156
+ *
157
+ * @param A The heap array.
158
+ * @param ind1 The index of the first element to be swapped.
159
+ * @param ind2 The index of the second element to be swapped.
160
+ */
161
+ void swap (int [] A , int ind1 , int ind2 ) {
162
+ int temp = A [ind1 ];
163
+ A [ind1 ] = A [ind2 ];
164
+ A [ind2 ] = temp ;
165
+ }
166
+
167
+ /**
168
+ * Prints the elements of the heap array.
169
+ */
170
+ void printHeapArray () {
171
+ for (int i = 0 ; i < load ; i ++)
172
+ System .out .print (hArray [i ] + " " );
173
+ System .out .println ();
174
+ }
175
+
176
+ }
0 commit comments