/
ArrayMaxHeap.java
183 lines (145 loc) · 4.8 KB
/
ArrayMaxHeap.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
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
package com.hrishikeshmishra.ns.heapandpriorityqueue;
/**
* Problem:
* Array based max heap
*
* @author hrishikesh.mishra
* @link http://hrishikeshmishra.com/array-based-max-heap-implementation/
*/
public class ArrayMaxHeap<E extends Comparable> implements IMaxHeap<E> {
protected E[] repository;
protected int size;
protected int capacity;
@SuppressWarnings("unchecked")
public ArrayMaxHeap(int capacity) {
this.capacity = capacity;
this.size = 0;
this.repository = (E[]) new Comparable[capacity];
}
public void insert(E data) {
if (isFull()) increaseCapacity();
repository[size++] = data;
percolateUp(size - 1);
}
public E deleteMax() {
if (isEmpty()) return null;
E data = repository[0];
size--;
if (size == 0) {
repository[0] = null;
return data;
}
repository[0] = repository[size];
repository[size] = null;
percolateDown(0);
return data;
}
@Override
public void destroy() {
this.size = 0;
this.repository = null;
}
@Override
public E getMax() {
if (isEmpty()) return null;
return repository[0];
}
@SuppressWarnings("unchecked")
protected void increaseCapacity() {
int newCapacity = capacity * 2;
E[] newRepo = (E[]) new Comparable[newCapacity];
for (int i = 0; i < size; i++)
newRepo[i] = repository[i];
this.repository = newRepo;
this.capacity = newCapacity;
}
protected void percolateDown(int index) {
if (index >= size) return;
int leftChildIndex = getLeftChild(index),
rightChildIndex = getRightChild(index),
maxChildIndex = index;
/** if left child exists and it has greater value than root **/
if (isValidNode(leftChildIndex) && hasLargerValue(leftChildIndex, index))
maxChildIndex = leftChildIndex;
/** if right child exists and it has greater value than root **/
if (isValid(rightChildIndex) && hasLargerValue(rightChildIndex, maxChildIndex))
maxChildIndex = rightChildIndex;
/** if root has smaller value than child **/
if (maxChildIndex != index)
swapValues(maxChildIndex, index);
else
return;
percolateDown(maxChildIndex);
}
protected void percolateUp(int index) {
/** Reached on top of tree **/
if (index <= 0) return;
int parentIndex = getParentIndex(index);
if (hasSmallerValue(parentIndex, index))
swapValues(parentIndex, index);
else
return;
percolateUp(parentIndex);
}
private boolean hasLargerValue(int index1, int index2) {
return (repository[index1].compareTo(repository[index2])) > 0;
}
private boolean hasSmallerValue(int index1, int index2) {
return (repository[index1].compareTo(repository[index2])) < 0;
}
private void swapValues(int index1, int index2) {
E temp = repository[index1];
repository[index1] = repository[index2];
repository[index2] = temp;
}
@Override
public int getParentIndex(int index) {
if (!isValid(index)) return -1;
return (index - 1) / 2;
}
@Override
public int getLeftChild(int index) {
int leftIndex = 2 * index + 1;
if (!isValid(leftIndex)) return -1;
return leftIndex;
}
@Override
public int getRightChild(int index) {
int rightIndex = 2 * index + 2;
if (!isValid(rightIndex)) return -1;
return rightIndex;
}
private boolean isValid(int index) {
return (index >= 0 && index < size);
}
private boolean isEmpty() {
return size == 0;
}
private boolean isValidNode(int index) {
return (index > -1);
}
private boolean isFull() {
return size == capacity;
}
}
class ArrayMinHeapTest {
public static void main(String[] args) {
IMaxHeap<Integer> maxHeap = new ArrayMaxHeap<>(4);
maxHeap.insert(13);
maxHeap.insert(2);
maxHeap.insert(12);
maxHeap.insert(24);
maxHeap.insert(34);
System.out.println("Max: " + maxHeap.getMax());
System.out.println("Delete Max : " + maxHeap.deleteMax());
System.out.println("Delete Max : " + maxHeap.deleteMax());
System.out.println("Delete Max : " + maxHeap.deleteMax());
System.out.println("Delete Max : " + maxHeap.deleteMax());
maxHeap.insert(100);
maxHeap.insert(23);
maxHeap.insert(17);
System.out.println("Delete Max : " + maxHeap.deleteMax());
System.out.println("Delete Max : " + maxHeap.deleteMax());
System.out.println("Delete Max : " + maxHeap.deleteMax());
}
}