-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinIntHeap.java
More file actions
136 lines (99 loc) · 3.59 KB
/
Copy pathMinIntHeap.java
File metadata and controls
136 lines (99 loc) · 3.59 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
package techQuestions;
import java.util.Arrays;
public class MinIntHeap {
private int capacity = 10;
private int size = 0;
// items integer array initialized to the size set by capacity
int[] items = new int[capacity];
// methods to get parent right or left index
private int getParentIndex(int index) { return (index-2)/2; }
private int getRightIndex(int index) {return (index*2) + 2; }
private int getLeftIndex(int index) {return (index*2) + 1; }
// methods to check if index even exists!
private boolean hasParent(int index) {return getParentIndex(index) >= 0; }
private boolean hasLeft(int index) {return getLeftIndex(index) < size; }
private boolean hasRight(int index) {return getRightIndex(index) < size; }
// methods to retrieve integer values from an index
private int parentValue(int index) {return items[getParentIndex(index)];}
private int leftValue(int index) {return items[getLeftIndex(index)];}
private int rightValue(int index) {return items[getRightIndex(index)];}
// swap a newly inserted node with its parent if it is greater than its parent
// private void swap(int index) {
// // if current node is greater than parent node, then return
// if (items[index] > items[getParentIndex(index)]) {
// // must be smaller than parent, so swap
// int temp = items[getParentIndex(index)];
// items[getParentIndex(index)] = items[index];
// items[index] = temp;
// // send this new parent node recursively to swap method to check its new parent
// return swap(getParentIndex(index));
// } else {
// return;
// }
// }
// true implementation of swap above
private void swap(int index1, int index2) {
int temp = items[index2];
items[index2] = items[index1];
items[index1] = temp;
}
private void ensureExtraCapacity() {
// this is a basic idea of how array lists operate
if (size == capacity) {
items = Arrays.copyOf(items, capacity * 2);
capacity *= 2;
}
}
// peek method will return minimum element in array, which is always first
private int peek() {
if (size == 0) throw new IllegalStateException("size is 0!");
return items[0];
}
// poll method of a heap removes the top element and returns it
private int poll() {
if (size == 0) throw new IllegalStateException("size is 0!");
int top = items[0];
// replace top item / root node with last inserted element
items[0] = items[size-1];
// array / heap size just got one smaller
size--;
// we must replace top element with smaller of two childs again and again
heapifyDown();
return top;
}
// now to add a new element to the heap
private void add(int value) {
// first step is to make sure there is enough capacity in array
ensureExtraCapacity();
items[size] = value;
// increase size of course!
size++;
// heapify up does not take parameters since we know we are always moving up
// from size - 1 index
heapifyUp();
return;
}
// an item has just been inserted, time to check its parents
private void heapifyUp() {
int lastIndex = size - 1;
while (hasParent(lastIndex) && parentValue(lastIndex) > items[lastIndex]) {
swap(getParentIndex(lastIndex), lastIndex);
lastIndex = getParentIndex(lastIndex);
}
}
private void heapifyDown() {
int topIndex = 0;
while (hasLeft(topIndex)) {
int smallerChildIndex = getLeftIndex(topIndex);
if (hasRight(topIndex) && rightValue(topIndex) < leftValue(topIndex)) {
smallerChildIndex = getRightIndex(topIndex);
}
if (items[topIndex] > items[smallerChildIndex]) {
swap(topIndex, smallerChildIndex);
topIndex = smallerChildIndex;
} else {
break;
}
}
}
}