Skip to content

Commit f5a1d9e

Browse files
author
dafeng.xdf
committed
Merge master
2 parents bb9e045 + c25f52c commit f5a1d9e

File tree

15 files changed

+725
-0
lines changed

15 files changed

+725
-0
lines changed

.npmignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
coverage.html
2+
lib-cov/
3+
node_modules

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
language: node_js
2+
node_js:
3+
- "0.8"
4+
- "0.10"

HISTORY

Whitespace-only changes.

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2013 xdf
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Makefile

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
TESTS = test/*.test.js
2+
TIMEOUT = 1000
3+
MOCHA_OPTS =
4+
5+
install:
6+
@npm install --registry=http://r.cnpmjs.org --disturl=http://dist.cnpmjs.org
7+
8+
jshint: install
9+
@./node_modules/.bin/jshint .
10+
11+
test: install
12+
@NODE_ENV=test ./node_modules/.bin/mocha \
13+
--harmony \
14+
--reporter $(REPORTER) \
15+
--timeout $(TIMEOUT) \
16+
$(MOCHA_OPTS) \
17+
$(TESTS)
18+
19+
test-cov cov: install
20+
@NODE_ENV=test node --harmony \
21+
node_modules/.bin/istanbul cover --preserve-comments \
22+
./node_modules/.bin/_mocha \
23+
-- -u exports \
24+
--reporter $(REPORTER) \
25+
--timeout $(TIMEOUT) \
26+
$(MOCHA_OPTS) \
27+
$(TESTS)
28+
@./node_modules/.bin/cov coverage

README.md

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
# Class BinaryHeap
2+
3+
> JavaScript Implementation of the Binary Heap.
4+
> Author: xdf
5+
6+
## Constructor Detail
7+
8+
### BinaryHeap()
9+
10+
Constructs a new minimum binary heap, Minimum heap is default;
11+
12+
### BinaryHeap(Boolean isMinHeap)
13+
14+
Constructs a new minimum binary heap.
15+
16+
` Parameters: `
17+
18+
* isMinHeap - true to use the order imposed by the given comparator; false to reverse that order
19+
20+
### BinaryHeap(Function comparator)
21+
22+
Constructs a new BinaryHeap that will use the given comparator to order its elements.
23+
24+
` Parameters: `
25+
26+
* capacity - the initial capacity for the heap
27+
28+
### BinaryHeap(Number capacity)
29+
30+
Constructs a new minimum binary heap with the specified initial capacity.
31+
32+
` Parameters: `
33+
34+
* capacity - The initial capacity for the heap. This value must be greater than zero.
35+
36+
` Throws: `
37+
38+
* IllegalArgumentException - if capacity is <= 0
39+
40+
### BinaryHeap(Number capacity, Boolean isMinHeap, Function comparator)
41+
42+
Constructs a new BinaryHeap.
43+
44+
` Parameters: `
45+
46+
* capacity - the initial capacity for the heap
47+
* isMinHeap - true to use the order imposed by the given comparator; false to reverse that order
48+
* comparator - the comparator used to order the elements, null means use natural order
49+
50+
` Throws: `
51+
52+
* IllegalArgumentException - if capacity is <= 0
53+
54+
55+
## Method Detail
56+
57+
### void clear()
58+
59+
Clears all elements from queue.
60+
61+
### boolean isEmpty()
62+
63+
Tests if queue is empty.
64+
65+
` Returns: `
66+
67+
* true if queue is empty; false otherwise.
68+
69+
### boolean isFull()
70+
71+
Tests if queue is full.
72+
73+
` Returns: `
74+
75+
* true if queue is full; false otherwise.
76+
77+
### void insert(Object element)
78+
79+
Inserts an element into queue.
80+
81+
` Parameters: `
82+
83+
* element - the element to be inserted
84+
85+
### object peek()
86+
87+
Returns the element on top of heap but don't remove it.
88+
89+
` Returns: `
90+
91+
* the element at top of heap
92+
93+
` Throws: `
94+
95+
* NoSuchElementException - if isEmpty() == true
96+
97+
### object pop()
98+
99+
Returns the element on top of heap and remove it.
100+
101+
` Returns: `
102+
103+
* the element at top of heap
104+
105+
` Throws: `
106+
107+
* NoSuchElementException - if isEmpty() == true
108+
109+
110+
### string toString()
111+
112+
Returns a string representation of this heap.
113+
114+
` Returns: `
115+
116+
* a string representation of this heap
117+
118+
### object iterator()
119+
120+
Returns an iterator over this heap's elements.
121+
122+
` Returns: `
123+
124+
* an iterator over this heap's elements
125+
126+
### boolean add(Object object)
127+
128+
Adds an object to this heap. Same as insert(Object).
129+
130+
` Parameters: `
131+
132+
* object - the object to add
133+
134+
` Returns: `
135+
136+
* true, always
137+
138+
### object get()
139+
140+
Returns the priority element. Same as peek().
141+
142+
` Returns: `
143+
144+
* the priority element
145+
146+
` Throws: `
147+
148+
* BufferUnderflowException - if this heap is empty
149+
150+
### object remove()
151+
152+
Removes the priority element. Same as pop().
153+
154+
` Returns: `
155+
156+
* the removed priority element
157+
158+
` Throws: `
159+
160+
* BufferUnderflowException - if this heap is empty
161+
162+
### number size()
163+
164+
Returns the number of elements in this heap.
165+
166+
` Returns: `
167+
168+
* the number of elements in this heap
169+
170+
## License
171+
172+
The MIT License (MIT)
173+
174+
Copyright (c) 2013 xdf
175+
176+
Permission is hereby granted, free of charge, to any person obtaining a copy of
177+
this software and associated documentation files (the "Software"), to deal in
178+
the Software without restriction, including without limitation the rights to
179+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
180+
the Software, and to permit persons to whom the Software is furnished to do so,
181+
subject to the following conditions:
182+
183+
The above copyright notice and this permission notice shall be included in all
184+
copies or substantial portions of the Software.
185+
186+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
187+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
188+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
189+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
190+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
191+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

bin/BinaryHeap

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env node
2+
/* ================================================================
3+
* BinaryHeap by xdf(xudafeng[at]126.com)
4+
*
5+
* first created at : Wed Aug 20 2014 10:35:14 GMT+0800 (CST)
6+
*
7+
* ================================================================
8+
* Copyright 2013 xdf
9+
*
10+
* Licensed under the MIT License
11+
* You may not use this file except in compliance with the License.
12+
*
13+
* ================================================================ */
14+
15+
'use strict';

index.html

Whitespace-only changes.

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict';
2+
3+
module.exports = require('./lib/BinaryHeap');

0 commit comments

Comments
 (0)