@@ -91,13 +91,12 @@ var MaxHeap = /** @class */ (function () {
9191 this . sink ( k , n ) ;
9292 } ;
9393 MaxHeap . prototype . exch = function ( number , number2 ) {
94- if ( this . array [ number ] === undefined || this . array [ number2 ] === undefined || number > this . currIndex || number > this . currIndex )
94+ if ( this . array [ number ] === undefined || this . array [ number2 ] === undefined )
9595 return ;
96- var tmp = this . array [ number ] ;
97- this . array [ number ] = this . array [ number2 ] ;
98- control . setValueAtIndex ( number , this . array [ number ] , tmp ) ;
99- this . array [ number2 ] = tmp ;
100- control . setValueAtIndex ( number2 , this . array [ number2 ] , this . array [ number ] ) ;
96+ control . highlightTwoNodes ( number , number2 , "orange" ) ;
97+ control . removeHighlightTwoNodes ( number , number2 , "orange" ) ;
98+ control . exchangeElemAndNodes ( number , this . array [ number ] , number2 , this . array [ number2 ] ) ;
99+ this . backEndExch ( number , number2 ) ;
101100 } ;
102101 MaxHeap . prototype . add = function ( a ) {
103102 // Add to array and start frontendevents
@@ -112,58 +111,48 @@ var MaxHeap = /** @class */ (function () {
112111 } ;
113112 MaxHeap . prototype . remove = function ( ) {
114113 // Remove root element, set last element to root and start frontendevents
115- this . currIndex -- ;
116- var oldVal = this . array [ 0 ] ;
117- this . exch ( 0 , this . currIndex ) ;
118- control . swapNode ( this . currIndex , 0 ) ;
114+ this . exch ( 0 , -- this . currIndex ) ;
119115 control . removeElem ( this . currIndex , false ) ;
120- control . setValueAtIndex ( this . currIndex , " " , oldVal ) ;
121- this . sink ( 0 , this . currIndex - 1 ) ;
116+ this . sink ( 0 , this . currIndex ) ;
122117 } ;
123118 MaxHeap . prototype . sink = function ( index , length ) {
124119 var left = index * 2 + 1 ;
125120 var right = index * 2 + 2 ;
126- if ( right > length )
121+ if ( left >= length )
127122 return ;
128123 if ( this . array [ index ] >= this . array [ left ] && this . array [ index ] >= this . array [ right ] )
129124 return ;
130125 // Sink
131126 var other ;
132- if ( this . array [ right ] > this . array [ left ] ) {
127+ if ( right >= length )
128+ other = left ;
129+ else if ( this . array [ right ] > this . array [ left ] ) {
133130 other = right ;
134131 }
135132 else {
136133 other = left ;
137134 }
138- control . highlightNode ( index , "orange" ) ;
139- control . highlightNode ( other , "orange" ) ;
140- control . swapNode ( index , other ) ;
135+ // Check once more before exchange
136+ if ( this . array [ index ] >= this . array [ other ] ) {
137+ control . highlightTwoNodes ( index , other , "green" ) ;
138+ control . removeHighlightTwoNodes ( index , other , "green" ) ;
139+ return ;
140+ }
141141 this . exch ( index , other ) ;
142- control . highlightNode ( index , "green" ) ;
143- control . highlightNode ( other , "green" ) ;
144- control . removeHighlight ( index ) ;
145- control . removeHighlight ( other ) ;
142+ control . highlightTwoNodes ( index , other , "green" ) ;
143+ control . removeHighlightTwoNodes ( index , other , "green" ) ;
146144 this . sink ( other , length ) ;
147145 } ;
148146 MaxHeap . prototype . swim = function ( index ) {
149147 var other = Math . floor ( ( index - 1 ) / 2 ) ;
150148 while ( other >= 0 && this . array [ index ] > this . array [ other ] ) {
151- control . highlightNode ( index , "orange" ) ;
152- control . highlightNode ( other , "orange" ) ;
153- control . swapNode ( index , other ) ;
154149 this . exch ( index , other ) ;
155- control . highlightNode ( index , "green" ) ;
156- control . highlightNode ( other , "green" ) ;
157- control . removeHighlight ( index ) ;
158- control . removeHighlight ( other ) ;
159150 index = Math . floor ( ( index - 1 ) / 2 ) ;
160151 other = Math . floor ( ( index - 1 ) / 2 ) ;
161152 }
162153 if ( index !== 0 ) {
163- control . highlightNode ( index , "green" ) ;
164- control . highlightNode ( other , "green" ) ;
165- control . removeHighlight ( index ) ;
166- control . removeHighlight ( other ) ;
154+ control . highlightTwoNodes ( index , other , "green" ) ;
155+ control . removeHighlightTwoNodes ( index , other , "green" ) ;
167156 }
168157 } ;
169158 MaxHeap . prototype . getName = function ( ) {
0 commit comments