|
75 | 75 |
|
76 | 76 | function updateChild(node, newChild) { |
77 | 77 | let parent = node.parent; |
78 | | - if (parent === Nil) { |
79 | | - newChild.parent = parent; |
80 | | - return newChild; |
81 | | - } |
82 | | - if (parent.right === node) { |
83 | | - parent.right = newChild; |
84 | | - } else { |
85 | | - parent.left = newChild; |
86 | | - } |
87 | | - if (newChild === Nil) { |
| 78 | + if (parent !== Nil) { |
| 79 | + if (parent.right === node) { |
| 80 | + parent.right = newChild; |
| 81 | + } else { |
| 82 | + parent.left = newChild; |
| 83 | + } |
88 | 84 | parent.updateSize(); |
89 | | - return parent; |
90 | 85 | } |
91 | | - newChild.parent = parent; |
92 | | - return newChild; |
| 86 | + if (newChild !== Nil) { |
| 87 | + newChild.parent = parent; |
| 88 | + } |
93 | 89 | } |
94 | 90 | exports.updateChild = updateChild; |
95 | 91 |
|
|
194 | 190 | return node; |
195 | 191 | } |
196 | 192 |
|
| 193 | + function findLeftMost(node) { |
| 194 | + while (node.left !== Nil) { |
| 195 | + node = node.left; |
| 196 | + } |
| 197 | + return node; |
| 198 | + } |
| 199 | + |
197 | 200 | function findNodeAtPos(node, pos) { |
198 | 201 | while (pos != node.left.size) { |
199 | 202 | if (pos < node.left.size) { |
|
270 | 273 | return Nil; // There is no element to remove |
271 | 274 | } |
272 | 275 | let node = findNodeAtPos(this._root, pos); |
273 | | - let removedNode = node; |
274 | 276 | let maintainNode; |
275 | | - if (node.right === Nil) { |
276 | | - maintainNode = updateChild(node, node.left) |
277 | | - } else if (node.left === Nil) { |
278 | | - maintainNode = updateChild(node, node.right) |
279 | | - } else { |
280 | | - /* |
281 | | - Before remove: |
282 | | - P(node's parent, be notices, N either be left child or right child of P) |
283 | | - | |
284 | | - N(node) |
285 | | - / \ |
286 | | - L R |
| 277 | + |
| 278 | + /* |
| 279 | + Before remove: |
| 280 | + P(node's parent, be notices, N either be left child or right child of P) |
| 281 | + | |
| 282 | + N(node) |
| 283 | + / \ |
| 284 | + L R |
| 285 | + \ |
| 286 | + \ |
| 287 | + LRM(Left-Rightmost) |
| 288 | + \ |
| 289 | + Nil |
| 290 | + After remove node N: |
| 291 | + P(node's parent) |
| 292 | + / |
| 293 | + L |
287 | 294 | \ |
288 | 295 | \ |
289 | 296 | LRM(Left-Rightmost) |
290 | 297 | \ |
291 | | - Nil |
292 | | - After remove node N: |
293 | | - P(node's parent) |
294 | | - / |
295 | | - L |
296 | | - \ |
297 | | - \ |
298 | | - LRM(Left-Rightmost) |
299 | | - \ |
300 | | - R |
| 298 | + R |
301 | 299 |
|
302 | | - N(node) is wild node that was removed |
303 | | - |
304 | | - */ |
| 300 | + N(node) is wild node that was removed |
| 301 | + |
| 302 | + */ |
| 303 | + if (node.left !== Nil){ |
305 | 304 | let LRM = findRightMost(node.left); |
306 | 305 | updateChild(node, node.left) |
307 | 306 | LRM.right = node.right |
308 | | - LRM.right.parent = LRM; |
309 | | - maintainNode = LRM.right; |
| 307 | + if (LRM.right === Nil) { |
| 308 | + maintainNode = LRM; |
| 309 | + } else { |
| 310 | + LRM.right.parent = LRM; |
| 311 | + maintainNode = LRM.right; |
| 312 | + } |
| 313 | + } else if (node.right !== Nil) { |
| 314 | + let RLM = findLeftMost(node.right); |
| 315 | + updateChild(node, node.right) |
| 316 | + RLM.left = node.left |
| 317 | + if (RLM.left === Nil) { |
| 318 | + maintainNode = RLM; |
| 319 | + } else { |
| 320 | + RLM.left.parent = RLM; |
| 321 | + maintainNode = RLM.left; |
| 322 | + } |
| 323 | + } else { |
| 324 | + updateChild(node, Nil) |
| 325 | + maintainNode = node.parent; |
310 | 326 | } |
311 | 327 | this._root = maintainSizeBalancedTree(maintainNode); |
312 | | - return removedNode; |
| 328 | + return node; |
313 | 329 | }; |
314 | 330 |
|
315 | 331 |
|
|
0 commit comments