-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxHeap.java
More file actions
139 lines (113 loc) · 3.49 KB
/
MaxHeap.java
File metadata and controls
139 lines (113 loc) · 3.49 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
public class MaxHeap {
int[] Heap;
int size;
int maxsize;
MaxHeap(int maxsize) {
this.maxsize = maxsize;
this.size = 0;
Heap = new int[this.maxsize];
}
private int parent(int pos) {
return (pos - 1) / 2;
}
private int leftChild(int pos) {
return (2 * pos) + 1;
}
private int rightChild(int pos) {
return (2 * pos) + 2;
}
private boolean isLeaf(int pos) {
if (pos > (size / 2) && pos <= size) {
return true;
}
return false;
}
private void swap(int fpos, int spos) {
int tmp;
tmp = Heap[fpos];
Heap[fpos] = Heap[spos];
Heap[spos] = tmp;
}
private void maxHeapify(int pos) {
if (isLeaf(pos))
return;
if (Heap[pos] < Heap[leftChild(pos)]
|| Heap[pos] < Heap[rightChild(pos)]) {
if (Heap[leftChild(pos)] > Heap[rightChild(pos)]) {
swap(pos, leftChild(pos));
maxHeapify(leftChild(pos));
} else {
swap(pos, rightChild(pos));
maxHeapify(rightChild(pos));
}
}
}
public void Delete(int i) {
Heap[i] = Heap[size];
size--;
int current = i;
while (Heap[current] > Heap[parent(current)]) {
swap(current, parent(current));
current = parent(current);
}
int cur = current * 2;
while (Heap[cur] <= Heap[cur + 1]) {
swap(current, parent(current));
}
}
public int extractMax() {
int popped = Heap[0];
int current = size;
while (Heap[current] > Heap[parent(current)]) {
swap(current, parent(current));
current = parent(current);
}
Heap[0] = Heap[--size];
maxHeapify(0);
return popped;
}
public void insert(int element) {
Heap[size] = element;
int current = size;
while (Heap[current] > Heap[parent(current)]) {
swap(current, parent(current));
current = parent(current);
}
size++;
}
public void print() {
for (int i = 0; i < size / 2; i++) {
System.out.print("Parent Node : " + Heap[i]);
if (leftChild(i) < size) // if the child is out of the bound
// of the array
System.out.print(" Left Child Node: "
+ Heap[leftChild(i)]);
if (rightChild(i) < size) // if the right child index must not
// be out of the index of the array
System.out.print(" Right Child Node: "
+ Heap[rightChild(i)]);
System.out.println(); // for new line
}
}
public static void main(String[] arg) {
// Display message for better readability
System.out.println("The Max Heap is ");
MaxHeap maxHeap = new MaxHeap(15);
// Inserting nodes
// Custom inputs
maxHeap.insert(5);
maxHeap.insert(3);
maxHeap.insert(17);
maxHeap.insert(10);
maxHeap.insert(84);
maxHeap.insert(19);
maxHeap.insert(6);
maxHeap.insert(22);
maxHeap.insert(9);
// Calling maxHeap() as defined above
maxHeap.print();
// Print and display the maximum value in heap
System.out.println("The max val is "
+ maxHeap.extractMax());
}
}