-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathHeapsImplementation.java
101 lines (80 loc) Β· 2.1 KB
/
HeapsImplementation.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
package Lecture25;
import java.util.ArrayList;
public class HeapsImplementation<T extends Comparable<T>> {
private ArrayList<T> data;
boolean isMin;
HeapsImplementation(boolean isMin) {
this.data = new ArrayList<T>();
this.isMin = isMin;
}
// O(1) time
public int size() {
return this.data.size();
}
// O(1) time
public T getMaxPriority() {
T value = this.data.get(0);
return value;
}
// O(logN) time
public void add(T element) {
this.data.add(element);
this.upheapify(this.data.size() - 1);
}
private void upheapify(int childIndex) {
if (childIndex == 0) {
return;
}
int parentIndex = (childIndex - 1) / 2;
if (!isLarger(parentIndex, childIndex)) {
this.swap(parentIndex, childIndex);
this.upheapify(parentIndex);
}
}
private boolean isLarger(int pi, int ci) {
T parentValue = this.data.get(pi);
T childValue = this.data.get(ci);
if (this.isMin) {
return parentValue.compareTo(childValue) < 0;
} else {
return parentValue.compareTo(childValue) > 0;
}
}
public void swap(int parentIndex, int childIndex) {
T parentValue = this.data.get(parentIndex);
T childValue = this.data.get(childIndex);
this.data.set(parentIndex, childValue);
this.data.set(childIndex, parentValue);
}
public void display() throws Exception {
if (this.size() == 0) {
throw new Exception("Heap is emtpy!");
}
this.display(0);
}
private void display(int parentIndex) {
int leftchildIndex = (2 * parentIndex) + 1;
int rightchildIndex = (2 * parentIndex) + 2;
String result = "";
if (leftchildIndex < this.data.size()) {
T lvalue = this.data.get(leftchildIndex);
result = result + lvalue + "=>";
} else {
result = "END=>" + result;
}
result = result + this.data.get(parentIndex);
if (rightchildIndex < this.data.size()) {
T rvalue = this.data.get(rightchildIndex);
result = result + "<=" + rvalue;
} else {
result = result + "<=END ";
}
System.out.println(result);
if (leftchildIndex < this.data.size()) {
this.display(leftchildIndex);
}
if (rightchildIndex < this.data.size()) {
this.display(rightchildIndex);
}
}
}