Skip to content

Commit e2f6095

Browse files
author
dafeng.xdf
committed
Add demo
1 parent 8f17d9e commit e2f6095

File tree

5 files changed

+80
-12
lines changed

5 files changed

+80
-12
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Class BinaryHeap
22

33
> JavaScript Implementation of the Binary Heap.
4+
45
> Author: xdf
56
67
## Constructor Detail

index.html

+2-12
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<script src="page/javascript/jquery.min.js"></script>
88
<script src="page/javascript/pillow.min.js"></script>
99
<script src="page/javascript/markdown.min.js"></script>
10+
<script src="lib/BinaryHeap.js"></script>
1011
<base target="_blank"/>
1112
</head>
1213
<body>
@@ -17,17 +18,6 @@
1718
<canvas id="screen"></canvas>
1819
</div>
1920
<div id="docs"></div>
20-
<script>
21-
;(function($){
22-
$.ajax({
23-
url : './README.md',
24-
dataType : 'html',
25-
scriptCharset: 'utf-8',
26-
success : function(d){
27-
$('#docs').html(marked(d));
28-
}
29-
});
30-
})(jQuery);
31-
</script>
21+
<script src="page/javascript/index.js"></script>
3222
</body>
3323
</html>

page/javascript/index.js

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
;(function($, P, BinaryHeap, undefined){
2+
// fetch docs
3+
$.ajax({
4+
url : './README.md',
5+
dataType : 'html',
6+
scriptCharset: 'utf-8',
7+
success : function(d) {
8+
$('#docs').html(marked(d));
9+
}
10+
});
11+
// init heap
12+
var TOTAL = 400;
13+
var binaryHeap = new BinaryHeap();
14+
var result;
15+
var total;
16+
function initHeap() {
17+
result = [];
18+
total = TOTAL;
19+
binaryHeap.clear();
20+
while(total --) {
21+
binaryHeap.add(total);
22+
}
23+
total = TOTAL;
24+
}
25+
initHeap();
26+
// init demonstrate
27+
var capacity = 2 / 3;
28+
var WIDTH = TOTAL;
29+
var HEIGHT = TOTAL * capacity;
30+
var Screen = P.Screen;
31+
var Timer = P.Timer;
32+
var screen = new Screen({
33+
container: 'screen',
34+
width: WIDTH,
35+
height: HEIGHT,
36+
x:0,
37+
y:0
38+
});
39+
screen.update(function() {
40+
if(!total) initHeap();
41+
result.push(binaryHeap.pop());
42+
var current = [];
43+
var ctx = this.context;
44+
ctx.lineWidth = 1;
45+
current = current.concat(result);
46+
current = current.concat(binaryHeap.list);
47+
current.forEach(function(i, k) {
48+
ctx.beginPath();
49+
ctx.moveTo(k, i * capacity);
50+
ctx.lineTo(k, HEIGHT);
51+
ctx.stroke();
52+
});
53+
total --;
54+
});
55+
var timer = new Timer(screen);
56+
timer.start();
57+
})(jQuery, pillow, BinaryHeap.BinaryHeap);

page/stylesheet/index.css

+7
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,10 @@
55
right:0;
66
border: 0;
77
}
8+
#screen{
9+
background: #ebebeb;
10+
}
11+
#demonstrate {
12+
text-align: center;
13+
padding: 20px 0;
14+
}

test/BinaryHeap.test.js

+13
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,17 @@ describe('main function', function() {
156156
while (binaryHeap.size() > 0) result.push(binaryHeap.pop());
157157
result.toString().should.equal(arr.reverse().toString());
158158
});
159+
it('huge number', function() {
160+
var binaryHeap = new BinaryHeap();
161+
var total = 100;
162+
while(total --) {
163+
binaryHeap.add(total);
164+
}
165+
var result = [];
166+
while (binaryHeap.size() > 0) result.push(binaryHeap.pop());
167+
total = 100;
168+
while(total --) {
169+
result[total].should.equal(total);
170+
}
171+
});
159172
});

0 commit comments

Comments
 (0)