1- import merge from " ./merge.js" ;
2- import mergepairs from " ./mergepairs.js" ;
3- import decreasekey from " ./decreasekey.js" ;
4- import Node from " ./Node.js" ;
1+ import merge from ' ./merge.js' ;
2+ import mergepairs from ' ./mergepairs.js' ;
3+ import decreasekey from ' ./decreasekey.js' ;
4+ import Node from ' ./Node.js' ;
55
66export default class PairingHeap {
7-
8- constructor ( compare ) {
9- this . compare = compare ; // comparison function
10- this . min = null ; // root node, must have .prev = .next = null at all times
7+ constructor ( compare ) {
8+ this . compare = compare ; // Comparison function
9+ this . min = null ; // Root node, must have .prev = .next = null at all times
1110 }
1211
1312 /**
14- * find -min: simply return the top element of the heap.
13+ * Find -min: simply return the top element of the heap.
1514 */
16- head ( ) {
17- if ( this . min === null ) return undefined ;
15+ head ( ) {
16+ if ( this . min === null ) return undefined ;
1817 return this . min . value ;
1918 }
2019
21-
22- headreference ( ) {
20+ headreference ( ) {
2321 return this . min ;
2422 }
2523
2624 /**
27- * delete -min: remove the root and merge its subtrees. Various strategies
25+ * Delete -min: remove the root and merge its subtrees. Various strategies
2826 * are employed.
2927 */
30- pop ( ) {
28+ pop ( ) {
3129 const min = this . popreference ( ) ;
32- return min === null ? undefined : min . value ;
30+ return min === null ? undefined : min . value ;
3331 }
3432
3533 /**
3634 */
37- popreference ( ) {
38- if ( this . min === null ) return null ;
35+ popreference ( ) {
36+ if ( this . min === null ) return null ;
3937 const min = this . min ;
40- this . min = mergepairs ( this . compare , min . children ) ; // min .children.next = null
38+ this . min = mergepairs ( this . compare , min . children ) ; // Min .children.next = null
4139 return min ;
4240 }
4341
4442 /**
45- * insert : create a new heap for the inserted element and merge into the
43+ * Insert : create a new heap for the inserted element and merge into the
4644 * original heap.
4745 */
48- push ( value ) {
49- const node = new Node ( value ) ;
46+ push ( value ) {
47+ const node = new Node ( value ) ;
5048 return this . pushreference ( node ) ;
5149 }
5250
@@ -56,88 +54,81 @@ export default class PairingHeap {
5654 * PairingHeap#pushreference with an internal reference from this tree or
5755 * another, except the root of another tree.
5856 */
59- pushreference ( ref ) {
57+ pushreference ( ref ) {
6058 if ( this . min === null ) this . min = ref ;
6159 else {
62- // this .min != null != ref
63- this . min = merge ( this . compare , this . min , ref ) ;
60+ // This .min != null != ref
61+ this . min = merge ( this . compare , this . min , ref ) ;
6462 }
63+
6564 return ref ;
6665 }
6766
68-
6967 /**
7068 * Supposes the same comparison function is used in both trees.
7169 * We can call pushreference since other.min.next = other.min.prev = null.
7270 */
73- merge ( other ) {
74- const ref = other . min ;
75- if ( ref !== null ) this . pushreference ( ref ) ;
71+ merge ( other ) {
72+ const ref = other . min ;
73+ if ( ref !== null ) this . pushreference ( ref ) ;
7674 }
7775
78-
7976 /**
8077 * @param {Node } ref Non-null internal node object.
8178 * @param {Object } value The new value for ref.
8279 */
83- update ( ref , value ) {
84-
85- const d = this . compare ( value , ref . value ) ;
86-
87- if ( d < 0 ) this . decreasekey ( ref , value ) ;
88- else if ( d > 0 ) this . increasekey ( ref , value ) ;
89- else ref . value = value ;
80+ update ( ref , value ) {
81+ const d = this . compare ( value , ref . value ) ;
9082
83+ if ( d < 0 ) this . decreasekey ( ref , value ) ;
84+ else if ( d > 0 ) this . increasekey ( ref , value ) ;
85+ else ref . value = value ;
9186 }
9287
9388 /**
94- * decrease -key
89+ * Decrease -key
9590 *
9691 * @param {Node } ref Non-null internal node object.
9792 * @param {Object } value The new value for ref.
9893 */
99- decreasekey ( ref , value ) {
100- if ( ref === this . min ) ref . value = value ;
94+ decreasekey ( ref , value ) {
95+ if ( ref === this . min ) ref . value = value ;
10196 else {
102- // this .min != null, ref != null
103- this . min = decreasekey ( this . compare , this . min , ref , value ) ;
97+ // This .min != null, ref != null
98+ this . min = decreasekey ( this . compare , this . min , ref , value ) ;
10499 }
105100 }
106101
107102 /**
108- * increase -key: remove the item at the key to be increased, replace
103+ * Increase -key: remove the item at the key to be increased, replace
109104 * the key with a larger key, then push the result back into the heap.
110105 *
111106 * @param {Node } ref Non-null internal node object.
112107 * @param {Object } value The new value for ref.
113108 *
114109 */
115- increasekey ( ref , value ) {
116-
110+ increasekey ( ref , value ) {
117111 this . delete ( ref ) ;
118112
119113 ref . value = value ;
120114
121- this . pushreference ( ref ) ;
122-
115+ this . pushreference ( ref ) ;
123116 }
124117
125118 /**
126- * ref must be internal
119+ * Ref must be internal
127120 * ref.prev and ref.next get reset to null
128121 */
129- delete ( ref ) {
130-
131- if ( ref === this . min ) {
132- this . popreference ( ) ;
133- return ;
122+ delete ( ref ) {
123+ if ( ref === this . min ) {
124+ this . popreference ( ) ;
125+ return ;
134126 }
135127
136- const successor = mergepairs ( this . compare , ref . children ) ; // ref .children.next = null
128+ const successor = mergepairs ( this . compare , ref . children ) ; // Ref .children.next = null
137129
138130 // ref has no children
139131 if ( successor === null ) {
140-
141132 // _ _ _
142133 // | | --> | | --> | |
143134 // |_| <-- |_| <-- |_|
@@ -152,7 +143,7 @@ export default class PairingHeap {
152143 // - |_| <-/
153144 // R
154145 //
155- ref . prev . next = ref . next ; // must be != null because ref != min
146+ ref . prev . next = ref . next ; // Must be != null because ref != min
156147
157148 if ( ref . next !== null ) {
158149 //
@@ -164,7 +155,7 @@ export default class PairingHeap {
164155 // |----- |_|
165156 // R
166157 //
167- ref . next . prev = ref . prev ;
158+ ref . next . prev = ref . prev ;
168159 //
169160 // _ _
170161 // | | ----------> | |
@@ -189,20 +180,16 @@ export default class PairingHeap {
189180 ref . prev = null ;
190181
191182 return ;
192-
193183 }
194184
195- successor . prev = ref . prev ; // must be != null because ref != min
196- successor . prev . next = successor ;
185+ successor . prev = ref . prev ; // Must be != null because ref != min
186+ successor . prev . next = successor ;
197187 ref . prev = null ;
198188
199189 if ( ref . next !== null ) {
200- successor . next = ref . next ; // might be null
201- successor . next . prev = successor ;
190+ successor . next = ref . next ; // Might be null
191+ successor . next . prev = successor ;
202192 ref . next = null ;
203193 }
204-
205194 }
206-
207-
208195}
0 commit comments