-
Notifications
You must be signed in to change notification settings - Fork 10
/
_utils.js
223 lines (197 loc) · 5.13 KB
/
_utils.js
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
class Heap {
constructor(comparator, array = []) {
this._array = [null, ...array];
/**
* `_comparator` here is used to compare `a` and `b` to decide their priority.
* If `_comparator` returns a value < 0, it means that `b` has higher priority
* than `a`, `b` should be in the parents level of `a`; if `_comparator` returns
* a value > 0, it means that `a` has higher priority than `b`, `a` should be in
* the parents level of `b`.
* So for maxHeap, we should pass `a - b`, cause larger `a` should have higher
* priority; for minHeap, we should pass `b - a`, cause smaller `a` should have
* higher priority.
*/
this._comparator = comparator;
const halfLength = Math.floor(array.length / 2);
for (let i = halfLength; i > 0; i--) {
this._heapifyDown(i);
}
}
_debug() {
console.log(this._array.slice(1));
}
size() {
return this._array.length - 1;
}
peek() {
return this._array[1];
}
toArray() {
return this._array.slice(1);
}
_getParentIndex(index) {
return Math.floor(index / 2);
}
_hasParent(index) {
return this._getParentIndex(index) > 0;
}
_getLeftChildIndex(index) {
return index * 2;
}
_hasLeftChild(index) {
return this._getLeftChildIndex(index) < this._array.length;
}
_getRightChildIndex(index) {
return index * 2 + 1;
}
_hasRightChild(index) {
return this._getRightChildIndex(index) < this._array.length;
}
_heapifyUp(index) {
while (this._hasParent(index)) {
const parentIndex = this._getParentIndex(index);
if (this._comparator(this._array[parentIndex], this._array[index]) < 0) {
_swap(this._array, parentIndex, index);
index = parentIndex;
} else {
break;
}
}
}
_heapifyDown(index) {
while (this._hasLeftChild(index)) {
let highPriorityChildIndex = this._getLeftChildIndex(index);
if (this._hasRightChild(index)) {
const rightChildIndex = this._getRightChildIndex(index);
if (
this._comparator(
this._array[highPriorityChildIndex],
this._array[rightChildIndex]
) < 0
) {
highPriorityChildIndex = rightChildIndex;
}
}
if (
this._comparator(
this._array[index],
this._array[highPriorityChildIndex]
) < 0
) {
_swap(this._array, index, highPriorityChildIndex);
index = highPriorityChildIndex;
} else {
break;
}
}
}
push(item) {
this._array.push(item);
this._heapifyUp(this._array.length - 1);
}
pop() {
return this._removeIndex(1);
}
remove(item) {
const index = this._array.indexOf(item); // O(n)
return this._removeIndex(index);
}
_removeIndex(index) {
if (index === this._array.length - 1) {
return this._array.pop();
} else {
const lastItem = this._array.pop();
const itemToRemove = this._array[index];
this._array[index] = lastItem;
if (
this._hasParent(index) &&
this._comparator(this._array[this._getParentIndex(index)], lastItem) < 0
) {
this._heapifyUp(index);
} else {
this._heapifyDown(index);
}
return itemToRemove;
}
}
}
function _swap(array, i, j) {
[array[i], array[j]] = [array[j], array[i]];
}
function ListNode(val) {
this.val = val;
this.next = null;
}
function printLinkedList(head) {
const listArray = [];
let current = head;
while (current) {
listArray.push(current.val);
current = current.next;
}
console.log(listArray.join(' -> '));
}
function buildLinkedList(array) {
const head = new ListNode(array[0]);
let current = head;
for (let i = 1; i < array.length; i++) {
const node = new ListNode(array[i]);
current.next = node;
current = node;
}
return head;
}
function TreeNode(val) {
this.val = val;
this.left = null;
this.right = null;
}
function buildTreeBFS(array) {
const root = new TreeNode(array[0]);
const parentQueue = [root];
let i = 1;
while (i < array.length) {
const parent = parentQueue.shift();
if (array[i] !== null) {
parent.left = new TreeNode(array[i]);
parentQueue.push(parent.left);
}
i++;
if (i < array.length && array[i] !== null) {
parent.right = new TreeNode(array[i]);
parentQueue.push(parent.right);
}
i++;
}
return root;
}
function printTreeBFS(root) {
const array = [root.val];
const parentQueue = [root];
while (parentQueue.length > 0) {
const parent = parentQueue.shift();
if (parent.left) {
array.push(parent.left.val);
parentQueue.push(parent.left);
} else {
array.push(null);
}
if (parent.right) {
array.push(parent.right.val);
parentQueue.push(parent.right);
} else {
array.push(null);
}
}
while (array[array.length - 1] === null) {
array.pop();
}
console.log(array);
}
exports.Heap = Heap;
exports.ListNode = ListNode;
exports.printLinkedList = printLinkedList;
exports.buildLinkedList = buildLinkedList;
exports.TreeNode = TreeNode;
exports.printTreeBFS = printTreeBFS;
exports.buildTreeBFS = buildTreeBFS;