Skip to content

Commit cd6d9ba

Browse files
author
dafeng.xdf
committed
Add test case
1 parent 786d7b8 commit cd6d9ba

File tree

2 files changed

+29
-22
lines changed

2 files changed

+29
-22
lines changed

lib/BinaryHeap.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,6 @@
219219
proto.percolateUp = function(index) {
220220
// last element
221221
var current = this.list[index];
222-
var score = current;
223222

224223
while (index > 0) {
225224
/**
@@ -229,7 +228,7 @@
229228
var parentIndex = Math.floor((index + 1) / 2) - 1,
230229
parentNode = this.list[parentIndex];
231230

232-
if (score >= parentNode) break;
231+
if (current >= parentNode) break;
233232
// swap the parent node with the current
234233
this.list[parentIndex] = current;
235234
this.list[index] = parentNode;
@@ -244,23 +243,22 @@
244243
proto.percolateDown = function(index) {
245244
var length = this.size();
246245
var current = this.list[index];
247-
var score = current;
248246

249247
while(true) {
250248
var childRightIndex = (index + 1) * 2;
251249
var childLeftIndex = childRightIndex - 1;
252250
var swap = null;
253251

254252
if (childLeftIndex < length) {
255-
var child1 = this.list[childLeftIndex],
256-
child1Score = child1;
257-
if (child1Score < score) swap = childLeftIndex;
253+
var childLeft = this.list[childLeftIndex];
254+
255+
if (childLeft < current) swap = childLeftIndex;
258256
}
259257

260258
if (childRightIndex < length) {
261-
var child2 = this.list[childRightIndex];
262-
var child2Score = child2;
263-
if (child2Score < (swap == null ? score : child1Score)) swap = childRightIndex;
259+
var childRight = this.list[childRightIndex];
260+
261+
if (childRight < (swap === null ? current : childLeft)) swap = childRightIndex;
264262
}
265263
if (swap == null) break;
266264
this.list[index] = this.list[swap];

test/BinaryHeap.test.js

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ describe('add or insert to heap', function(){
5555
binaryHeap.insert(1);
5656
binaryHeap.insert(1);
5757
binaryHeap.insert(1);
58-
}catch (e){
59-
}
58+
}catch (e){}
6059
binaryHeap.size().should.equal(3);
6160
});
6261
});
@@ -93,14 +92,13 @@ describe('clear method, isEmpty method, isFull method', function() {
9392
binaryHeap.isFull().should.be.true;
9493
});
9594
});
96-
/*
9795
describe('pop method', function() {
9896
it('should success', function() {
9997
var binaryHeap = new BinaryHeap(3);
10098
binaryHeap.add(1);
10199
binaryHeap.add(2);
102100
binaryHeap.add(3);
103-
binaryHeap.pop().should.equal(3);
101+
binaryHeap.pop().should.equal(1);
104102
binaryHeap.size().should.equal(2);
105103
});
106104
});
@@ -109,11 +107,11 @@ describe('size method, toString method and contains method', function() {
109107
it('should be a number', function() {
110108
var binaryHeap = new BinaryHeap(3);
111109
binaryHeap.size().should.equal(0);
112-
binaryHeap.add(true);
113-
binaryHeap.add('xdf');
110+
binaryHeap.add(1);
111+
binaryHeap.add(2);
114112
binaryHeap.add(3);
115-
binaryHeap.toString().should.equal('[true,"xdf",3]');
116-
binaryHeap.contains(true).should.be.true;
113+
binaryHeap.toString().should.equal('[1,2,3]');
114+
binaryHeap.contains(1).should.be.true;
117115
binaryHeap.contains(0).should.be.false;
118116
});
119117
});
@@ -135,16 +133,27 @@ describe('grow method', function() {
135133
binaryHeap.add(2);
136134
binaryHeap.add(3);
137135
binaryHeap.add(4);
138-
}catch(e) {
139-
}
136+
}catch(e) {}
140137
binaryHeap.size().should.equal(3);
141138
binaryHeap.grow();
142139
try {
143140
binaryHeap.add(4);
144141
binaryHeap.add(5);
145-
}catch(e) {
146-
}
142+
}catch(e) {}
147143
binaryHeap.size().should.equal(4);
148144
});
149145
});
150-
*/
146+
147+
describe('main function', function() {
148+
it('pop should success', function() {
149+
var binaryHeap = new BinaryHeap();
150+
var arr = [5, 4, 3, 2, 1];
151+
arr.forEach(function(i) {
152+
binaryHeap.add(i);
153+
});
154+
binaryHeap.list.toString().should.equal('1,2,4,5,3');
155+
var result = [];
156+
while (binaryHeap.size() > 0) result.push(binaryHeap.pop());
157+
result.toString().should.equal(arr.reverse().toString());
158+
});
159+
});

0 commit comments

Comments
 (0)