File tree 5 files changed +80
-12
lines changed
5 files changed +80
-12
lines changed Original file line number Diff line number Diff line change 1
1
# Class BinaryHeap
2
2
3
3
> JavaScript Implementation of the Binary Heap.
4
+
4
5
> Author: xdf
5
6
6
7
## Constructor Detail
Original file line number Diff line number Diff line change 7
7
< script src ="page/javascript/jquery.min.js "> </ script >
8
8
< script src ="page/javascript/pillow.min.js "> </ script >
9
9
< script src ="page/javascript/markdown.min.js "> </ script >
10
+ < script src ="lib/BinaryHeap.js "> </ script >
10
11
< base target ="_blank "/>
11
12
</ head >
12
13
< body >
17
18
< canvas id ="screen "> </ canvas >
18
19
</ div >
19
20
< 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 >
32
22
</ body >
33
23
</ html >
Original file line number Diff line number Diff line change
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 ) ;
Original file line number Diff line number Diff line change 5
5
right : 0 ;
6
6
border : 0 ;
7
7
}
8
+ # screen {
9
+ background : # ebebeb ;
10
+ }
11
+ # demonstrate {
12
+ text-align : center;
13
+ padding : 20px 0 ;
14
+ }
Original file line number Diff line number Diff line change @@ -156,4 +156,17 @@ describe('main function', function() {
156
156
while ( binaryHeap . size ( ) > 0 ) result . push ( binaryHeap . pop ( ) ) ;
157
157
result . toString ( ) . should . equal ( arr . reverse ( ) . toString ( ) ) ;
158
158
} ) ;
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
+ } ) ;
159
172
} ) ;
You can’t perform that action at this time.
0 commit comments