Skip to content

Commit 6a4607e

Browse files
author
dafeng.xdf
committed
Change constructor
1 parent 3e0fcb9 commit 6a4607e

File tree

4 files changed

+90
-16
lines changed

4 files changed

+90
-16
lines changed

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,24 @@
99

1010
### BinaryHeap()
1111

12+
Constructs a new minimum binary heap, Minimum heap is default;
13+
14+
### BinaryHeap(Boolean isMinHeap)
15+
1216
Constructs a new minimum binary heap.
1317

18+
` Parameters: `
19+
20+
* isMinHeap - true to use the order imposed by the given comparator; false to reverse that order
1421

15-
### BinaryHeap(Array comparator)
22+
### BinaryHeap(Function comparator)
1623

1724
Constructs a new BinaryHeap that will use the given comparator to order its elements.
1825

26+
` Parameters: `
27+
28+
* capacity - the initial capacity for the heap
29+
1930
### BinaryHeap(Number capacity)
2031

2132
Constructs a new minimum binary heap with the specified initial capacity.
@@ -28,13 +39,14 @@ Constructs a new minimum binary heap with the specified initial capacity.
2839

2940
* IllegalArgumentException - if capacity is <= 0
3041

31-
### BinaryHeap(Number capacity, Array comparator)
42+
### BinaryHeap(Number capacity, Boolean isMinHeap, Function comparator)
3243

3344
Constructs a new BinaryHeap.
3445

3546
` Parameters: `
3647

3748
* capacity - the initial capacity for the heap
49+
* isMinHeap - true to use the order imposed by the given comparator; false to reverse that order
3850
* comparator - the comparator used to order the elements, null means use natural order
3951

4052
` Throws: `

lib/BinaryHeap.js

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,31 +24,54 @@
2424
/* browser */
2525
factory(root['BinaryHeap'] || (root['BinaryHeap'] = {}));
2626
}
27-
})(this, function(exports, undefined){
27+
})(this, function(exports, undefined) {
2828

2929
/**
3030
* @constructor BinaryHeap
3131
* @param {Number} capacity
32+
* @param {Boolean} isMinHeap
3233
* @param {Array} comparator
3334
*/
3435
function BinaryHeap() {
3536
var args = arguments;
3637
var capacity = Infinity;
37-
var comparator;
38+
var comparator = function(a, b) {
39+
return a - b;
40+
};
41+
var isMinHeap = true;
3842

39-
if(args[1]) {
43+
if(typeof args[2] !== 'undefined') {
4044
capacity = args[0];
41-
comparator = args[1];
45+
isMinHeap = args[1];
46+
comparator = args[2];
4247
}else {
43-
44-
if(typeof args[0] === 'number') {
45-
capacity = args[0];
46-
}else if(typeof args[0] === 'object'){
47-
comparator = args[0];
48+
if(typeof args[1] !== 'undefined') {
49+
50+
if(typeof args[0] === 'number' && typeof args[1] === 'boolean') {
51+
capacity = args[0];
52+
isMinHeap = args[1];
53+
}else if(typeof args[0] === 'boolean' && typeof args[1] === 'function') {
54+
isMinHeap = args[0];
55+
comparator = args[1];
56+
}else if(typeof args[0] === 'number' && typeof args[1] === 'function') {
57+
capacity = args[0];
58+
comparator = args[1];
59+
}
60+
}else {
61+
62+
if(typeof args[0] === 'number') {
63+
capacity = args[0];
64+
}else if (typeof args[0] === 'function'){
65+
comparator = args[0];
66+
}else if(typeof args[0] === 'boolean'){
67+
isMinHeap = args[0];
68+
}
4869
}
4970
}
71+
5072
// create heap
51-
this.list = comparator? comparator: [];
73+
this.list = [];
74+
this.isMinHeap = isMinHeap;
5275
this.capacity = capacity;
5376
this.comparator = comparator;
5477
}
@@ -127,7 +150,7 @@
127150
*/
128151
proto.peek = function() {
129152
if(this.isEmpty()) throw('Error: heap is empty.');
130-
return this.list[this.list.length - 1];
153+
return this.list[0];
131154
}
132155

133156
/**
@@ -173,6 +196,28 @@
173196
return !!~this.list.indexOf(object);
174197
}
175198

199+
/**
200+
* Increases the size of the heap to support additional elements
201+
* @method grow
202+
*/
203+
proto.grow = function() {
204+
this.capacity ++;
205+
}
206+
207+
/**
208+
* Percolates element up heap from from the position given by the index.
209+
* @method percolateDown
210+
*/
211+
proto.percolateUp = function(index) {
212+
}
213+
214+
/**
215+
* Percolates element down heap from the position given by the index.
216+
* @method percolateDown
217+
*/
218+
proto.percolateDown = function(index) {
219+
}
220+
176221
exports.BinaryHeap = BinaryHeap;
177222
exports.version = '0.1.0';
178223
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"should": "*"
1919
},
2020
"scripts": {
21-
"test": "mocha"
21+
"test": "mocha test/**/*.js"
2222
},
2323
"homepage": "https://github.com/xudafeng/BinaryHeap",
2424
"author": "xudafeng",

test/BinaryHeap.test.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ describe('get and peek', function(){
7070
binaryHeap.add(1);
7171
binaryHeap.add(2);
7272
binaryHeap.add(3);
73-
binaryHeap.get().should.equal(3);
74-
binaryHeap.peek().should.equal(3);
73+
binaryHeap.get().should.equal(1);
74+
binaryHeap.peek().should.equal(1);
7575
});
7676
});
7777

@@ -113,3 +113,20 @@ describe('iterator method', function() {
113113
});
114114
});
115115

116+
describe('grow method', function() {
117+
it('should increases after grow', function() {
118+
var binaryHeap = new BinaryHeap(3, [1, 2, 3]);
119+
try {
120+
}catch(e) {
121+
binaryHeap.add(4);
122+
}
123+
binaryHeap.size().should.equal(3);
124+
binaryHeap.grow();
125+
try {
126+
binaryHeap.add(4);
127+
binaryHeap.add(5);
128+
}catch(e) {
129+
}
130+
binaryHeap.size().should.equal(4);
131+
});
132+
});

0 commit comments

Comments
 (0)