Skip to content

Commit b201487

Browse files
committed
Fixes updateChild with updateSize when the newChild is Nil and the parent is not Nil.
1 parent 19a12da commit b201487

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
parent.left = newChild;
8484
}
8585
if (newChild === Nil) {
86+
parent.updateSize();
8687
return parent;
8788
}
8889
newChild.parent = parent;
@@ -277,7 +278,7 @@
277278
updateChild(node, node.left)
278279
LRM.right = node.right
279280
LRM.right.parent = LRM;
280-
maintainNode = node.right;
281+
maintainNode = LRM.right;
281282
}
282283
this._root = maintainSizeBalancedTree(maintainNode);
283284
return removedNode;

test/data-structures/size-balanced-tree.spec.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,25 @@ describe('SBTree', function () {
7171
var right = new Node(15, root, Nil, Nil, 1);
7272
var leftLeft = new Node(10, left, Nil, Nil, 1);
7373
left.left = leftLeft;
74+
left.updateSize();
7475
root.left = left;
7576
root.right = right;
77+
root.updateSize();
78+
expect(root.size).toBe(4);
79+
7680
updateChild(left, leftLeft);
7781
expect(leftLeft.parent).toBe(root);
7882
expect(root.left).toBe(leftLeft);
7983
updateChild(leftLeft, Nil);
8084
checkNil();
8185
expect(root.left).toBe(Nil);
86+
expect(root.size).toBe(2);
8287
updateChild(Nil, right);
8388
expect(right.parent).toBe(Nil);
8489
checkNil();
8590
});
8691

87-
it('push and get 100000 elements', function () {
92+
it('push and get 100000 elements, remove the array by always remove the first/last element', function () {
8893
var sTree = new SBTree();
8994
for (var i = 0; i < 100000; ++i) {
9095
sTree.push(i);
@@ -93,5 +98,22 @@ describe('SBTree', function () {
9398
for (var i = 0; i < 100000; ++i) {
9499
expect(sTree.get(i).value).toBe(i);
95100
}
101+
for (var i = 0; i < 100000; ++i) {
102+
expect(sTree.get(0).value).toBe(i);
103+
var node = sTree.remove(0); // Always remove the first element;
104+
expect(node.value).toBe(i);
105+
}
106+
checkNil();
107+
expect(sTree._root).toBe(Nil);
108+
var count = 10000;
109+
for (var i = 0; i < count; ++i) {
110+
sTree.push(i);
111+
}
112+
for (var i = 0; i < count; ++i) {
113+
var node = sTree.remove(count - i - 1); // Always remove the last element;
114+
expect(node.value).toBe(count - i - 1);
115+
expect(sTree.size).toBe(count - i - 1);
116+
}
117+
checkNil();
96118
});
97119
});

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy