Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d79eb7a

Browse files
committed
add heap extract method for min & max heaps, which returns the extracted node. + tests
1 parent e5e2a43 commit d79eb7a

File tree

7 files changed

+1099
-11
lines changed

7 files changed

+1099
-11
lines changed

‎babel.config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
presets: [
3+
['@babel/preset-env', {targets: {node: 'current'}}],
4+
'@babel/preset-typescript',
5+
],
6+
};

‎dist/datastructures/BinaryHeap.js

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,73 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
33
class BinaryHeap {
4-
constructor(type) {
4+
constructor(type,initialValues) {
55
this.values = [];
66
this.type = type;
7+
if (initialValues) {
8+
initialValues.forEach((value) => this.insert(value));
9+
}
710
}
811
insert(value) {
12+
// recursive insert implementation, returns the heap with the value inserted
913
this.values.push(value);
1014
let valueIndex = this.values.length - 1;
1115
const helper = () => {
1216
let parentIndex = Math.floor((valueIndex - 1) / 2);
1317
if (this.type === 'max' && this.values[parentIndex] < value
1418
|| 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
1620
valueIndex = parentIndex;
1721
helper();
1822
}
1923
};
2024
helper();
2125
return this.values;
2226
}
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+
}
2370
}
71+
// if ((this.type === 'max' && children.left[0] <= this.values[nodeIndex]) && (children.right[0] <= this.values[nodeIndex])) return;
2472
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]))

‎dist/datastructures/BinaryHeap.test.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,24 @@ describe('min binary heap insert method tests', () => {
5757
MinBH.insert(20);
5858
MinBH.insert(1);
5959
MinBH.insert(0);
60-
console.log(MinBH.values);
6160
expect(MinBH.values).toEqual([0, 1, 25, 20, 90, 95, 80, 100, 85]);
6261
});
62+
test('can initialize a binary heap with an array of values, which it will insert into binary heap format', () => {
63+
MaxBH = new BinaryHeap_1.default('max', [1, 2, 3, 100]);
64+
MinBH = new BinaryHeap_1.default('min', [1, 2, 3, 100]);
65+
expect(MaxBH.values).toEqual([100, 3, 2, 1]);
66+
expect(MinBH.values).toEqual([1, 2, 3, 100]);
67+
});
68+
test('max: can extract root node and heap will sink down a new root node from the end, and end up with a heap with proper form', () => {
69+
MaxBH = new BinaryHeap_1.default('max', [100, 200, 300, 400, 500]);
70+
const extractedMaxNode = MaxBH.extract();
71+
expect(extractedMaxNode).toBe(500);
72+
expect(MaxBH.values).toEqual([400, 300, 200, 100]);
73+
});
74+
test('min: can extract root node and heap will sink down a new root node from the end, and end up with a heap with proper form', () => {
75+
MinBH = new BinaryHeap_1.default('min', [500, 400, 300, 200, 100]);
76+
const extractedMinNode = MinBH.extract();
77+
expect(extractedMinNode).toBe(100);
78+
expect(MinBH.values).toEqual([200, 300, 400, 500]);
79+
});
6380
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /