Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0079d4d

Browse files
committed
Consistence the delete operation.
1 parent628cc11 commit0079d4d

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
functionupdateChild(node,newChild){
7777
letparent=node.parent;
78-
if(parent===Nil){
79-
newChild.parent=parent;
80-
returnnewChild;
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-
returnparent;
9085
}
91-
newChild.parent=parent;
92-
returnnewChild;
86+
if(newChild!==Nil){
87+
newChild.parent=parent;
88+
}
9389
}
9490
exports.updateChild=updateChild;
9591

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

193+
functionfindLeftMost(node){
194+
while(node.left!==Nil){
195+
node=node.left;
196+
}
197+
returnnode;
198+
}
199+
197200
functionfindNodeAtPos(node,pos){
198201
while(pos!=node.left.size){
199202
if(pos<node.left.size){
@@ -270,46 +273,59 @@
270273
returnNil;// There is no element to remove
271274
}
272275
letnode=findNodeAtPos(this._root,pos);
273-
letremovedNode=node;
274276
letmaintainNode;
275-
if(node.right===Nil){
276-
maintainNode=updateChild(node,node.left)
277-
}elseif(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
letLRM=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+
}elseif(node.right!==Nil){
314+
letRLM=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-
returnremovedNode;
328+
returnnode;
313329
};
314330

315331

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp