-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryMaxHeap.java
More file actions
316 lines (271 loc) · 11.3 KB
/
BinaryMaxHeap.java
File metadata and controls
316 lines (271 loc) · 11.3 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
public class BinaryMaxHeap {
// the array used to implement max heap
// left child index is [2*x] and right child index is [(2*x)+1] if 'x' is the index of parent in array 'arr'.
// Cell 0 is unused for the sake of mathemaical simplicity.
int arr[];
// variable to store the last used index of arr
int lastUsedIndex;
// creating heap using constuctor
// Time Complexity - O(1) , Space Complexity - O(n)
BinaryMaxHeap(int size){
this.arr = new int[size];
this.lastUsedIndex=0;
}
// method to initialize the arr with Integer.MIN_VALUE, when it is empty
public void initializeArray(){
if(this.lastUsedIndex==0){
for(int i=0;i<this.arr.length;i++){
this.arr[i]=Integer.MIN_VALUE;
}
}
}
// method to add a new node to the binary max heap
// Time Complexity - O(log n) , Space Complexity - O(1)
public void addNode(int x){
// if binary max heap is full
if(this.lastUsedIndex==this.arr.length-1){
System.out.println("Binary heap is full, can't add new node");
return;
}
// else if binary max heap is empty
this.lastUsedIndex++;
this.arr[this.lastUsedIndex]=x;
// heapify bottom to top
heapifyBottomToTop();
}
// helper method to heapify from bottom node (newly added node) to top node. Heapify means swaping nodes to maintain the max heap property
// Time Complexity - O(log n) , Space Complexity - O(1)
private void heapifyBottomToTop(){
// child node / newly added node's index
int childNodeIndex = this.lastUsedIndex;
// move from bottom to top , swaping nodes if needed to maintain the max heap property
while(childNodeIndex>1){
int parentNodeIndex = childNodeIndex/2;
if(this.arr[parentNodeIndex]<this.arr[childNodeIndex]){
int temp = this.arr[parentNodeIndex];
this.arr[parentNodeIndex] = this.arr[childNodeIndex];
this.arr[childNodeIndex] = temp;
}
childNodeIndex = parentNodeIndex;
}
}
// method to do a level order traversal in binary heap
// Time Complexity - O(n) , Space Complexity - O(1)
public void levelOrderTraversal(){
// if binary max heap is empty
if(this.lastUsedIndex==0 || this.arr==null){
System.out.println("Binary heap is empty, can't traverse");
return;
}
// else if binary max heap is not empty
System.out.println("Level Order Traversal...");
for(int i=1;i<=this.lastUsedIndex;i++){
System.out.print(this.arr[i]+" ");
}
System.out.println();
}
// method to extract the top node having maximum value from the binary max heap
// we can only extract the root node from any heap. NO OTHER NODES CAN BE EXTRACTED.
// Time Complexity - O(log n) , Space Complexity - O(1)
public void extractMaxFromHeap(){
// if binary max heap is empty
if(this.lastUsedIndex==0 || this.arr==null){
System.out.println("Binary heap is empty, can't extract");
return;
}
// else if binary max heap is not empty
System.out.println("Extracted node from binary heap is: "+this.arr[1]);
// copy data of deepest node into the root node
this.arr[1] = this.arr[this.lastUsedIndex];
// delete the deepest node
this.arr[this.lastUsedIndex] = Integer.MIN_VALUE;
// update lastUsedIndex
this.lastUsedIndex--;
heapifyTopToBottom();
}
// helper method to heapify from top to bottom of heap. Done after extraction of node from any heap.
// Time Complexity - O(log n) , Space Complexity - O(1)
private void heapifyTopToBottom(){
// Start from the root node and move to the bottom, swapping nodes if needed to maintain the max heap property
int parentNodeIndex = 1;
while(parentNodeIndex<this.lastUsedIndex){
int childNodeIndex=-1;
int leftChildIndex = 2*parentNodeIndex;
int rightChildIndex = (2*parentNodeIndex)+1;
// if current parent node has no child
if(leftChildIndex>this.lastUsedIndex && rightChildIndex>this.lastUsedIndex){
break;
}
// if current parent node has both child
else if(leftChildIndex<=this.lastUsedIndex && rightChildIndex <= this.lastUsedIndex){
if(this.arr[leftChildIndex]>=this.arr[rightChildIndex]){
childNodeIndex = leftChildIndex;
}
else{
childNodeIndex = rightChildIndex;
}
}
// else if current parent node has only left child
else if(leftChildIndex<=this.lastUsedIndex && rightChildIndex>this.lastUsedIndex){
childNodeIndex=leftChildIndex;
}
// else if current parent node has only right child
else{
childNodeIndex=rightChildIndex;
}
// now as we have childnode index, compare child with parent and swap if needed
if(this.arr[childNodeIndex]>this.arr[parentNodeIndex]){
int temp = this.arr[parentNodeIndex];
this.arr[parentNodeIndex]=this.arr[childNodeIndex];
this.arr[childNodeIndex]=temp;
}
parentNodeIndex = childNodeIndex;
}
}
// wrapper method to do an inorder traversal on binary max heap - LEFT ROOT RIGHT
// Time Complexity - O(n) , Space Complexity - O(n) [As recursion is used, so internal stack will be maintained]
public void inOrderTraversal(){
// if binary max heap is empty
if(this.lastUsedIndex==0 || this.arr==null){
System.out.println("Binary heap is empty, can't traverse");
return;
}
// else if binary max heap is not empty
System.out.println("Inorder Traversal...");
inOrderTraversal(1);
System.out.println();
}
// overloaded method to do inOrderTraversal recursively
private void inOrderTraversal(int currentNodeIndex){
if(currentNodeIndex>this.lastUsedIndex){
return;
}
inOrderTraversal(2*currentNodeIndex);
System.out.print(this.arr[currentNodeIndex]+" ");
inOrderTraversal((2*currentNodeIndex)+1);
}
// wrapper method to do a preOrder traversal on binary max heap - ROOT LEFT RIGHT
// Time Complexity - O(n) , Space Complexity - O(n) [As recursion is used, so internal stack will be maintained]
public void preOrderTraversal(){
// if binary max heap is empty
if(this.lastUsedIndex==0 || this.arr==null){
System.out.println("Binary heap is empty, can't traverse");
return;
}
// else if binary max heap is not empty
System.out.println("preOrder Traversal...");
preOrderTraversal(1);
System.out.println();
}
// overloaded method to do preOrderTraversal recursively
private void preOrderTraversal(int currentNodeIndex){
if(currentNodeIndex>this.lastUsedIndex){
return;
}
System.out.print(this.arr[currentNodeIndex]+" ");
preOrderTraversal(2*currentNodeIndex);
preOrderTraversal((2*currentNodeIndex)+1);
}
// wrapper method to do a postOrder traversal on binary max heap - LEFT RIGHT ROOT
// Time Complexity - O(n) , Space Complexity - O(n) [As recursion is used, so internal stack will be maintained]
public void postOrderTraversal(){
// if binary max heap is empty
if(this.lastUsedIndex==0 || this.arr==null){
System.out.println("Binary heap is empty, can't traverse");
return;
}
// else if binary max heap is not empty
System.out.println("postOrder Traversal...");
postOrderTraversal(1);
System.out.println();
}
// overloaded method to do postOrderTraversal recursively
private void postOrderTraversal(int currentNodeIndex){
if(currentNodeIndex>this.lastUsedIndex){
return;
}
postOrderTraversal(2*currentNodeIndex);
postOrderTraversal((2*currentNodeIndex)+1);
System.out.print(this.arr[currentNodeIndex]+" ");
}
// method to get the root node of binary max heap, I am not returning the node here, just printing it
// Time Complexity - O(1) , Space Complexity - O(1)
public void peekBinaryHeap(){
// if binary max heap is empty
if(this.lastUsedIndex==0 || this.arr==null){
System.out.println("Binary heap is empty, can't peek");
return;
}
// else if binary max heap is not empty
System.out.println("Root node of binary heap: "+this.arr[1]);
}
// method to delete complete binary max heap
// Time Complexity - O(1) , Space Complexity - O(1)
public void deleteCompleteBinaryHeap(){
// if binary max heap is empty
if(this.lastUsedIndex==0 || this.arr==null){
System.out.println("Binary heap is empty, can't delete");
return;
}
// else if binary max heap is not empty
this.arr = null;
this.lastUsedIndex=0;
System.out.println("Binary heap deleted successfully");
}
public static void main(String[] args) throws Exception {
BinaryMaxHeap maxHeap = new BinaryMaxHeap(15);
maxHeap.initializeArray();
maxHeap.levelOrderTraversal();
maxHeap.addNode(50);
maxHeap.addNode(100);
maxHeap.addNode(200);
maxHeap.addNode(400);
maxHeap.addNode(300);
maxHeap.addNode(1000);
maxHeap.levelOrderTraversal();
maxHeap.extractMaxFromHeap();
maxHeap.levelOrderTraversal();
maxHeap.extractMaxFromHeap();
maxHeap.levelOrderTraversal();
maxHeap.deleteCompleteBinaryHeap();
maxHeap.levelOrderTraversal();
maxHeap.deleteCompleteBinaryHeap();
System.out.println("Reinserting and traversing...");
maxHeap=new Main(15);
maxHeap.addNode(50);
maxHeap.addNode(100);
maxHeap.addNode(200);
maxHeap.addNode(400);
maxHeap.addNode(300);
maxHeap.addNode(1000);
maxHeap.levelOrderTraversal();
maxHeap.peekBinaryHeap();
maxHeap.inOrderTraversal();
maxHeap.preOrderTraversal();
maxHeap.postOrderTraversal();
}
}
/*============================= OUTPUT ===================================
Binary heap is empty, can't traverse
Level Order Traversal...
1000 300 400 50 200 100
Extracted node from binary heap is: 1000
Level Order Traversal...
400 300 100 50 200
Extracted node from binary heap is: 400
Level Order Traversal...
300 200 100 50
Binary heap deleted successfully
Binary heap is empty, can't traverse
Binary heap is empty, can't delete
Reinserting and traversing...
Level Order Traversal...
1000 300 400 50 200 100
Root node of binary heap: 1000
Inorder Traversal...
50 300 200 1000 100 400
preOrder Traversal...
1000 300 50 200 400 100
postOrder Traversal...
50 200 300 100 400 1000
==================================================================*/