diff --git a/README.md b/README.md index 05fcaa1..16867eb 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # Class BinaryHeap > JavaScript Implementation of the Binary Heap. + > Author: xdf ## Constructor Detail diff --git a/index.html b/index.html index 151c155..9249ef9 100644 --- a/index.html +++ b/index.html @@ -7,6 +7,7 @@ + @@ -17,17 +18,6 @@
- + diff --git a/page/javascript/index.js b/page/javascript/index.js index e69de29..df334c9 100644 --- a/page/javascript/index.js +++ b/page/javascript/index.js @@ -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); diff --git a/page/stylesheet/index.css b/page/stylesheet/index.css index acb6ae5..7eb4bda 100644 --- a/page/stylesheet/index.css +++ b/page/stylesheet/index.css @@ -5,3 +5,10 @@ right:0; border: 0; } +#screen{ + background: #ebebeb; +} +#demonstrate { + text-align: center; + padding: 20px 0; +} diff --git a/test/BinaryHeap.test.js b/test/BinaryHeap.test.js index d11e132..20b33df 100644 --- a/test/BinaryHeap.test.js +++ b/test/BinaryHeap.test.js @@ -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); + } + }); });