-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryMinHeap.java
More file actions
306 lines (262 loc) · 11.1 KB
/
BinaryMinHeap.java
File metadata and controls
306 lines (262 loc) · 11.1 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
public class BinaryMinHeap {
// the array used to implement min 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 mathematical 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)
BinaryMinHeap(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 min heap
// Time Complexity - O(log n) , Space Complexity - O(1)
public void addNode(int x){
// if binary min 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 min 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 min 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 min 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 min heap is empty
if(this.lastUsedIndex==0 || this.arr==null){
System.out.println("Binary heap is empty, can't traverse");
return;
}
// else if binary min 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 minimum value from the binary min 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 extractMinFromHeap(){
// if binary min heap is empty
if(this.lastUsedIndex==0 || this.arr==null){
System.out.println("Binary heap is empty, can't extract");
return;
}
// else if binary min heap is not empty
System.out.println("Extracted node from binary heap is: "+this.arr[1]);
// copy data of ddepest 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 min-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 min 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 min heap is empty
if(this.lastUsedIndex==0 || this.arr==null){
System.out.println("Binary heap is empty, can't traverse");
return;
}
// else if binary min 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 min 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 min heap is empty
if(this.lastUsedIndex==0 || this.arr==null){
System.out.println("Binary heap is empty, can't traverse");
return;
}
// else if binary min 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 min 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 min heap is empty
if(this.lastUsedIndex==0 || this.arr==null){
System.out.println("Binary heap is empty, can't traverse");
return;
}
// else if binary min 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 min-heap, I am not returning the node here, just printing it
// Time Complexity - O(1) , Space Complexity - O(1)
public void peekBinaryHeap(){
// if binary min heap is empty
if(this.lastUsedIndex==0 || this.arr==null){
System.out.println("Binary heap is empty, can't peek");
return;
}
// else if binary min heap is not empty
System.out.println("Root node of binary heap: "+this.arr[1]);
}
// method to delete complete binary min heap
// Time Complexity - O(1) , Space Complexity - O(1)
public void deleteCompleteBinaryHeap(){
// if binary min heap is empty
if(this.lastUsedIndex==0 || this.arr==null){
System.out.println("Binary heap is empty, can't delete");
return;
}
// else if binary min 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 {
BinaryMinHeap binaryMinHeap = new BinaryMinHeap(15);
binaryMinHeap.initializeArray();
binaryMinHeap.addNode(3);
binaryMinHeap.addNode(5);
binaryMinHeap.addNode(8);
binaryMinHeap.addNode(17);
binaryMinHeap.addNode(19);
binaryMinHeap.addNode(36);
binaryMinHeap.addNode(40);
binaryMinHeap.addNode(25);
binaryMinHeap.addNode(100);
binaryMinHeap.addNode(1);
binaryMinHeap.levelOrderTraversal();
binaryMinHeap.extractMinFromHeap();
binaryMinHeap.levelOrderTraversal();
binaryMinHeap.extractMinFromHeap();
binaryMinHeap.levelOrderTraversal();
binaryMinHeap.inOrderTraversal();
binaryMinHeap.preOrderTraversal();
binaryMinHeap.postOrderTraversal();
binaryMinHeap.peekBinaryHeap();
binaryMinHeap.deleteCompleteBinaryHeap();
binaryMinHeap.levelOrderTraversal();
binaryMinHeap.deleteCompleteBinaryHeap();
}
}
/*===================================== OUTPUT ===========================================
Level Order Traversal...
1 3 8 17 5 36 40 25 100 19
Extracted node from binary heap is: 1
Level Order Traversal...
3 5 8 17 19 36 40 25 100
Extracted node from binary heap is: 3
Level Order Traversal...
5 17 8 25 19 36 40 100
Inorder Traversal...
100 25 17 19 5 36 8 40
preOrder Traversal...
5 17 25 100 19 8 36 40
postOrder Traversal...
100 25 19 17 36 40 8 5
Root node of binary heap: 5
Binary heap deleted successfully
Binary heap is empty, can't traverse
Binary heap is empty, can't delete
=================================================================*/