-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBinaryHeap.java
More file actions
161 lines (144 loc) · 3.18 KB
/
BinaryHeap.java
File metadata and controls
161 lines (144 loc) · 3.18 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package dataStructures.heap;
public class BinaryHeap {
// The heap binary tree
private int[] heap;
// The size of the heap
private int size;
// Constructor
public BinaryHeap(int capacity) {
heap = new int[capacity];
}
// Get the size of the heap
public int getSize() {
return size;
}
// Set the size of the heap
public void setSize(int size) {
this.size = size;
}
/**
* Insert a value in the end of the heap
* @param value
*
* @complexity time O(log n)
* @complexity space O(1)
*/
public void insert(int value) {
if (isFull()) {
throw new IndexOutOfBoundsException("Heap is full");
}
heap[size] = value;
fixHeapAbove(size);
size++;
}
/**
* Delete a value from the heap by index
*
* @param index
* @return the deleted value
*
* @complexity time O(log n)
* @complexity space O(1)
*/
public int delete(int index) {
if (isEmpty()) {
throw new IndexOutOfBoundsException("Heap is empty");
}
int parent = getParent(index);
int deletedValue = heap[index];
heap[index] = heap[size - 1];
if (index == 0 || heap[index] < heap[parent]) {
fixHeapBelow(index, size - 1);
} else {
fixHeapAbove(index);
}
size--;
return deletedValue;
}
/**
* Sort the heap with order by ascending
*
* @complexity time O(n log n)
* @complexity space O(1)
*/
public void sort() {
int lastHeapIndex = size - 1;
for (int i = 0; i < lastHeapIndex; i++) {
int temp = heap[0];
heap[0] = heap[lastHeapIndex - i];
heap[lastHeapIndex - i] = temp;
fixHeapBelow(0, lastHeapIndex - i - 1);
}
}
/**
* Fix Heap Above
* @param index
*
* @complexity time O(log n)
* @complexity space O(1)
*/
private void fixHeapAbove(int index) {
int newValue = heap[index];
while (index > 0 && newValue > heap[getParent(index)]) {
heap[index] = heap[getParent(index)];
index = getParent(index);
}
heap[index] = newValue;
}
/**
* Fix Heap Below
* @param index
* @param lastHeapIndex
*
* @complexity time O(log n)
* @complexity space O(1)
*/
private void fixHeapBelow(int index, int lastHeapIndex) {
int childToSwap;
while (index <= lastHeapIndex) {
int leftChild = getChild(index, true);
int rightChild = getChild(index, false);
if (leftChild <= lastHeapIndex) {
if (rightChild > lastHeapIndex) {
childToSwap = leftChild;
} else {
childToSwap = (heap[leftChild] > heap[rightChild] ? leftChild : rightChild);
}
if (heap[index] < heap[childToSwap]) {
int temp = heap[index];
heap[index] = heap[childToSwap];
heap[childToSwap] = temp;
} else {
break;
}
index = childToSwap;
} else {
break;
}
}
}
// Get the parent of a node
private int getParent(int index) {
return (index - 1) / 2;
}
// Get the child of a node
private int getChild(int index, boolean left) {
return 2 * index + (left ? 1 : 2);
}
// Check if the heap is empty
public boolean isEmpty() {
return size == 0;
}
// Check if the heap is full
public boolean isFull() {
return size == heap.length;
}
// Print the heap
public String printHeap() {
String out = "";
for (int i = 0; i < size; i++) {
System.out.print(out += heap[i] + " ");
}
return out;
}
}