File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change @@ -58,4 +58,38 @@ describe('Heap', function () {
58
58
res = heap . extract ( ) ;
59
59
expect ( res ) . toBe ( 66 ) ;
60
60
} ) ;
61
+ it ( 'should update top node properly' , function ( ) {
62
+ var heap = new Heap ( function ( a , b ) {
63
+ return a . val - b . val ;
64
+ } ) ;
65
+ var objectToUpdate = { val : 66 } ;
66
+ heap . add ( objectToUpdate ) ;
67
+ heap . add ( { val : 11 } ) ;
68
+ heap . add ( { val : 55 } ) ;
69
+ objectToUpdate . val = 0 ;
70
+ heap . update ( objectToUpdate ) ;
71
+ var res = heap . extract ( ) ;
72
+ expect ( res . val ) . toBe ( 55 ) ;
73
+ res = heap . extract ( ) ;
74
+ expect ( res . val ) . toBe ( 11 ) ;
75
+ res = heap . extract ( ) ;
76
+ expect ( res . val ) . toBe ( 0 ) ;
77
+ } ) ;
78
+ it ( 'should update bottom node properly' , function ( ) {
79
+ var heap = new Heap ( function ( a , b ) {
80
+ return a . val - b . val ;
81
+ } ) ;
82
+ var objectToUpdate = { val : 0 } ;
83
+ heap . add ( objectToUpdate ) ;
84
+ heap . add ( { val : 11 } ) ;
85
+ heap . add ( { val : 55 } ) ;
86
+ objectToUpdate . val = 66 ;
87
+ heap . update ( objectToUpdate ) ;
88
+ var res = heap . extract ( ) ;
89
+ expect ( res . val ) . toBe ( 66 ) ;
90
+ res = heap . extract ( ) ;
91
+ expect ( res . val ) . toBe ( 55 ) ;
92
+ res = heap . extract ( ) ;
93
+ expect ( res . val ) . toBe ( 11 ) ;
94
+ } ) ;
61
95
} ) ;
You can’t perform that action at this time.
0 commit comments