Skip to content

Commit

Permalink
Add demo
Browse files Browse the repository at this point in the history
  • Loading branch information
dafeng.xdf committed Aug 26, 2014
1 parent 8f17d9e commit e2f6095
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Class BinaryHeap

> JavaScript Implementation of the Binary Heap.
> Author: xdf
## Constructor Detail
Expand Down
14 changes: 2 additions & 12 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<script src="page/javascript/jquery.min.js"></script>
<script src="page/javascript/pillow.min.js"></script>
<script src="page/javascript/markdown.min.js"></script>
<script src="lib/BinaryHeap.js"></script>
<base target="_blank"/>
</head>
<body>
Expand All @@ -17,17 +18,6 @@
<canvas id="screen"></canvas>
</div>
<div id="docs"></div>
<script>
;(function($){
$.ajax({
url : './README.md',
dataType : 'html',
scriptCharset: 'utf-8',
success : function(d){
$('#docs').html(marked(d));
}
});
})(jQuery);
</script>
<script src="page/javascript/index.js"></script>
</body>
</html>
57 changes: 57 additions & 0 deletions page/javascript/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
;(function($, P, BinaryHeap, undefined){
// fetch docs
$.ajax({
url : './README.md',
dataType : 'html',
scriptCharset: 'utf-8',
success : function(d) {
$('#docs').html(marked(d));
}
});
// init heap
var TOTAL = 400;
var binaryHeap = new BinaryHeap();
var result;
var total;
function initHeap() {
result = [];
total = TOTAL;
binaryHeap.clear();
while(total --) {
binaryHeap.add(total);
}
total = TOTAL;
}
initHeap();
// init demonstrate
var capacity = 2 / 3;
var WIDTH = TOTAL;
var HEIGHT = TOTAL * capacity;
var Screen = P.Screen;
var Timer = P.Timer;
var screen = new Screen({
container: 'screen',
width: WIDTH,
height: HEIGHT,
x:0,
y:0
});
screen.update(function() {
if(!total) initHeap();
result.push(binaryHeap.pop());
var current = [];
var ctx = this.context;
ctx.lineWidth = 1;
current = current.concat(result);
current = current.concat(binaryHeap.list);
current.forEach(function(i, k) {
ctx.beginPath();
ctx.moveTo(k, i * capacity);
ctx.lineTo(k, HEIGHT);
ctx.stroke();
});
total --;
});
var timer = new Timer(screen);
timer.start();
})(jQuery, pillow, BinaryHeap.BinaryHeap);
7 changes: 7 additions & 0 deletions page/stylesheet/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,10 @@
right:0;
border: 0;
}
#screen{
background: #ebebeb;
}
#demonstrate {
text-align: center;
padding: 20px 0;
}
13 changes: 13 additions & 0 deletions test/BinaryHeap.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,17 @@ describe('main function', function() {
while (binaryHeap.size() > 0) result.push(binaryHeap.pop());
result.toString().should.equal(arr.reverse().toString());
});
it('huge number', function() {
var binaryHeap = new BinaryHeap();
var total = 100;
while(total --) {
binaryHeap.add(total);
}
var result = [];
while (binaryHeap.size() > 0) result.push(binaryHeap.pop());
total = 100;
while(total --) {
result[total].should.equal(total);
}
});
});

0 comments on commit e2f6095

Please sign in to comment.