|
1 | 1 | "use strict";
|
2 | 2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | 3 | class BinaryHeap {
|
4 | | - constructor(type) { |
| 4 | + constructor(type,initialValues) { |
5 | 5 | this.values = [];
|
6 | 6 | this.type = type;
|
| 7 | + if (initialValues) { |
| 8 | + initialValues.forEach((value) => this.insert(value)); |
| 9 | + } |
7 | 10 | }
|
8 | 11 | insert(value) {
|
| 12 | + // recursive insert implementation, returns the heap with the value inserted |
9 | 13 | this.values.push(value);
|
10 | 14 | let valueIndex = this.values.length - 1;
|
11 | 15 | const helper = () => {
|
12 | 16 | let parentIndex = Math.floor((valueIndex - 1) / 2);
|
13 | 17 | if (this.type === 'max' && this.values[parentIndex] < value
|
14 | 18 | || this.type === 'min' && this.values[parentIndex] > value) {
|
15 | | - [this.values[valueIndex], this.values[parentIndex]] = [this.values[parentIndex], this.values[valueIndex]]; // swap parent and larger child |
| 19 | + [this.values[valueIndex], this.values[parentIndex]] = [this.values[parentIndex], this.values[valueIndex]]; // swap parent and child |
16 | 20 | valueIndex = parentIndex;
|
17 | 21 | helper();
|
18 | 22 | }
|
19 | 23 | };
|
20 | 24 | helper();
|
21 | 25 | return this.values;
|
22 | 26 | }
|
| 27 | + extract() { |
| 28 | + // badly in need of a refactor |
| 29 | + if (!this.values.length) |
| 30 | + return; |
| 31 | + const newRoot = this.values.pop(); |
| 32 | + const extractedNode = this.values[0]; |
| 33 | + this.values[0] = newRoot; |
| 34 | + let nodeIndex = 0; |
| 35 | + if (this.type === 'max' && this.values[0] > Math.max(this.values[1], this.values[2]) || this.type === 'min' && this.values[0] < Math.min(this.values[1], this.values[2])) |
| 36 | + return extractedNode; |
| 37 | + const helper = () => { |
| 38 | + let children = { left: [this.values[nodeIndex * 2 + 1], nodeIndex * 2 + 1], right: [this.values[nodeIndex * 2 + 2], nodeIndex * 2 + 2] }; |
| 39 | + if (nodeIndex * 2 + 1 >= this.values.length) |
| 40 | + return; |
| 41 | + if (this.type === 'max') { |
| 42 | + if ((children.left[0] <= this.values[nodeIndex] || children.left[0] === undefined) && (children.right[0] <= this.values[nodeIndex] || children.right[0] === undefined)) |
| 43 | + return; |
| 44 | + if (children.left[0] > children.right[0]) { |
| 45 | + [this.values[nodeIndex], this.values[children.left[1]]] = [this.values[children.left[1]], this.values[nodeIndex]]; // swap |
| 46 | + nodeIndex = children.left[1]; |
| 47 | + } |
| 48 | + else { |
| 49 | + [this.values[nodeIndex], this.values[children.right[1]]] = [this.values[children.right[1]], this.values[nodeIndex]]; // swap |
| 50 | + nodeIndex = children.right[1]; |
| 51 | + } |
| 52 | + } |
| 53 | + else if (this.type === 'min') { |
| 54 | + if ((children.left[0] >= this.values[nodeIndex] || children.left[0] === undefined) && (children.right[0] >= this.values[nodeIndex] || children.right[0] === undefined)) |
| 55 | + return; |
| 56 | + if (children.left[0] < children.right[0]) { |
| 57 | + [this.values[nodeIndex], this.values[children.left[1]]] = [this.values[children.left[1]], this.values[nodeIndex]]; // swap |
| 58 | + nodeIndex = children.left[1]; |
| 59 | + } |
| 60 | + else { |
| 61 | + [this.values[nodeIndex], this.values[children.right[1]]] = [this.values[children.right[1]], this.values[nodeIndex]]; // swap |
| 62 | + nodeIndex = children.right[1]; |
| 63 | + } |
| 64 | + } |
| 65 | + helper(); // recurse |
| 66 | + }; |
| 67 | + helper(); |
| 68 | + return extractedNode; |
| 69 | + } |
23 | 70 | }
|
| 71 | +// if ((this.type === 'max' && children.left[0] <= this.values[nodeIndex]) && (children.right[0] <= this.values[nodeIndex])) return; |
24 | 72 | exports.default = BinaryHeap;
|
| 73 | +// if (nodeIndex * 2 + 1 >= this.values.length || (this.type === 'max' && children.left[0] <= this.values[nodeIndex]) && (children.right[0] <= this.values[nodeIndex]) || (this.type === 'min' && children.left[0] >= this.values[nodeIndex]) && (children.right[0] >= this.values[nodeIndex])) |
0 commit comments