-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeapMin.java
More file actions
145 lines (117 loc) · 3.2 KB
/
HeapMin.java
File metadata and controls
145 lines (117 loc) · 3.2 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
public class HeapMin<T extends Comparable<T>> {
// Attributes
private ArrayDynamic<T> heap;
// Constructors
public HeapMin() {
this.heap = new ArrayDynamic<>();
}
// Methods
public boolean isEmpty() {
return heap.isEmpty();
}
public int size() {
return heap.size();
}
public T get(int index) {
return heap.get(index);
}
public int findIndex(T data) {
for (int i = 0; i < size(); i++) {
if (get(i).equals(data)) {
return i;
}
}
return -1;
}
public void add(T data) {
heap.add(data);
heapifyUp(size() - 1);
}
public T peek() {
if (isEmpty()) {
return null;
}
return get(0);
}
public T poll() {
if (isEmpty()) {
return null;
}
T data = get(0);
heap.set(0, get(size() - 1));
heap.remove(size() - 1);
heapifyDown(0);
return data;
}
public void remove(T data) {
int index = findIndex(data);
if (index == -1) {
return;
}
heap.set(index, get(size() - 1));
heap.remove(size() - 1);
if (get(index).compareTo(getParent(index)) < 0) {
heapifyUp(index);
} else {
heapifyDown(index);
}
}
// Heapify methods
public void heapifyUp(int index) {
while (hasParent(index) && getParent(index).compareTo(get(index)) > 0) {
swap(getParentIndex(index), index);
index = getParentIndex(index);
}
}
public void heapifyDown(int index) {
while (hasLeftChild(index)) {
int smallerChildIndex = getLeftChildIndex(index);
if (hasRightChild(index) && getRightChild(index).compareTo(getLeftChild(index)) < 0) {
smallerChildIndex = getRightChildIndex(index);
}
if (get(index).compareTo(get(smallerChildIndex)) < 0) {
break;
} else {
swap(index, smallerChildIndex);
}
index = smallerChildIndex;
}
}
// Helper methods
public int getParentIndex(int index) {
return (index - 1) / 2;
}
public int getLeftChildIndex(int index) {
return (index * 2) + 1;
}
public int getRightChildIndex(int index) {
return (index * 2) + 2;
}
public boolean hasParent(int index) {
return getParentIndex(index) >= 0;
}
public boolean hasLeftChild(int index) {
return getLeftChildIndex(index) < size();
}
public boolean hasRightChild(int index) {
return getRightChildIndex(index) < size();
}
public T getParent(int index) {
return get(getParentIndex(index));
}
public T getLeftChild(int index) {
return get(getLeftChildIndex(index));
}
public T getRightChild(int index) {
return get(getRightChildIndex(index));
}
public void swap(int index1, int index2) {
T temp = get(index1);
heap.set(index1, get(index2));
heap.set(index2, temp);
}
@Override
public String toString() {
return heap.toString();
}
}