Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0079d4d

Browse files
committed
Consistence the delete operation.
1 parent 628cc11 commit 0079d4d

File tree

1 file changed

+58
-42
lines changed

1 file changed

+58
-42
lines changed

src/data-structures/size-balanced-tree.js

Lines changed: 58 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -75,21 +75,17 @@
7575

7676
function updateChild(node, newChild) {
7777
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+
}
8884
parent.updateSize();
89-
return parent;
9085
}
91-
newChild.parent = parent;
92-
return newChild;
86+
if (newChild !== Nil) {
87+
newChild.parent = parent;
88+
}
9389
}
9490
exports.updateChild = updateChild;
9591

@@ -194,6 +190,13 @@
194190
return node;
195191
}
196192

193+
function findLeftMost(node) {
194+
while (node.left !== Nil) {
195+
node = node.left;
196+
}
197+
return node;
198+
}
199+
197200
function findNodeAtPos(node, pos) {
198201
while (pos != node.left.size) {
199202
if (pos < node.left.size) {
@@ -270,46 +273,59 @@
270273
return Nil; // There is no element to remove
271274
}
272275
let node = findNodeAtPos(this._root, pos);
273-
let removedNode = node;
274276
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
287294
\
288295
\
289296
LRM(Left-Rightmost)
290297
\
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
301299
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){
305304
let LRM = findRightMost(node.left);
306305
updateChild(node, node.left)
307306
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;
310326
}
311327
this._root = maintainSizeBalancedTree(maintainNode);
312-
return removedNode;
328+
return node;
313329
};
314330

315331

0 commit comments

Comments
 (0)