Skip to content

Commit 46f29d4

Browse files
committed
Fixes updateChild and the usage of updateChild
1 parent fe9fd72 commit 46f29d4

File tree

2 files changed

+113
-22
lines changed

2 files changed

+113
-22
lines changed

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

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,7 @@
3333

3434
'use strict';
3535

36-
var Nil = {
37-
parent: Nil,
38-
left: Nil,
39-
right: Nil,
40-
size: 0,
41-
};
4236

43-
exports.Nil = Nil;
4437

4538
/**
4639
* Node of the Size-Balanced tree.
@@ -72,21 +65,30 @@
7265
};
7366

7467
exports.Node = Node;
68+
var Nil = new Node(null, null, null, null, 0);
69+
Nil.parent = Nil;
70+
Nil.left = Nil;
71+
Nil.right = Nil;
72+
exports.Nil = Nil;
7573

7674
function updateChild(node, newChild) {
7775
let parent = node.parent;
76+
if (parent === Nil) {
77+
newChild.parent = parent;
78+
return newChild;
79+
}
7880
if (parent.right === node) {
7981
parent.right = newChild;
80-
newChild.parent = parent;
81-
} else if (parent.left === node) {
82+
} else {
8283
parent.left = newChild;
83-
newChild.parent = parent;
8484
}
85-
if (newChild !== Nil) {
86-
return newChild;
85+
if (newChild === Nil) {
86+
return parent;
8787
}
88-
return parent;
88+
newChild.parent = parent;
89+
return newChild;
8990
}
91+
exports.updateChild = updateChild;
9092

9193
function LeftRotate(node, childNode) {
9294
/*
@@ -107,8 +109,8 @@
107109
node.right = childNode.left;
108110
node.right.parent = node;
109111
childNode.left = node;
110-
childNode.left.parent = childNode;
111-
updateChild(node, childNode)
112+
updateChild(node, childNode) //Access node.parent
113+
node.parent = childNode;
112114
node.updateSize();
113115
return childNode;
114116
}
@@ -132,8 +134,8 @@
132134
node.left = childNode.right;
133135
node.left.parent = node;
134136
childNode.right = node;
135-
childNode.right.parent = childNode;
136-
updateChild(node, childNode)
137+
updateChild(node, childNode) //Access node.parent
138+
node.parent = childNode;
137139
node.updateSize();
138140
return childNode;
139141
}
@@ -156,14 +158,14 @@
156158
return node;
157159
}
158160

159-
function findRightMost = function(node) {
161+
function findRightMost(node) {
160162
while (node.right !== Nil) {
161163
node = node.right;
162164
}
163165
return node;
164166
}
165167

166-
function findNodeAtPos = function(node, pos) {
168+
function findNodeAtPos(node, pos) {
167169
while (pos != node.left.size) {
168170
if (pos < node.left.size) {
169171
node = node.left;
@@ -185,6 +187,13 @@
185187
this._root = Nil;
186188
};
187189

190+
191+
exports.SBTree.prototype = {
192+
get size() {
193+
return this._root.size;
194+
},
195+
}
196+
188197
/**
189198
* Push a value to the end of tree.<br><br>
190199
* Complexity: O(log N).
@@ -273,8 +282,5 @@
273282
return removedNode;
274283
};
275284

276-
exports.SBTree.prototype.size = function() {
277-
return this._root.size;
278-
}
279285

280286
})(typeof window === 'undefined' ? module.exports : window);
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
'use strict';
2+
3+
var mod = require('../../src/data-structures/size-balanced-tree.js');
4+
var Node = mod.Node;
5+
var Nil = mod.Nil;
6+
var SBTree = mod.SBTree;
7+
var updateChild = mod.updateChild;
8+
9+
describe('Node', function () {
10+
it('should be a constructor function', function () {
11+
expect(typeof Node).toBe('function');
12+
});
13+
it('should be a construct properly', function () {
14+
var node = new Node(10, Nil, Nil, Nil, 1);
15+
expect(node.value).toBe(10);
16+
expect(node.left).toBe(Nil);
17+
expect(node.right).toBe(Nil);
18+
expect(node.parent).toBe(Nil);
19+
expect(node.size).toBe(1);
20+
});
21+
it('should reference children/parent properly', function () {
22+
var root = new Node(10, Nil, Nil, Nil, 1);
23+
var left = new Node(5, root, Nil, Nil, 1);
24+
var right = new Node(15, root, Nil, Nil, 1);
25+
root.left = left;
26+
root.right = right;
27+
expect(root.value).toBe(10);
28+
expect(root.left).toBe(left);
29+
expect(root.right).toBe(right);
30+
expect(root.parent).toBe(Nil);
31+
expect(right.parent).toBe(root);
32+
expect(left.parent).toBe(root);
33+
expect(right.size).toBe(1);
34+
expect(left.size).toBe(1);
35+
expect(root.size).toBe(1);
36+
root.updateSize();
37+
expect(root.size).toBe(3);
38+
});
39+
});
40+
41+
describe('SBTree', function () {
42+
it('should be a constructor function', function () {
43+
expect(typeof SBTree).toBe('function');
44+
});
45+
it('should start with null root', function () {
46+
expect(new SBTree()._root).toBe(Nil);
47+
});
48+
it('should insert and remove correctly', function () {
49+
var sTree = new SBTree();
50+
expect(sTree.size).toBe(0);
51+
sTree.insert(0, 10);
52+
expect(sTree.size).toBe(1);
53+
sTree.remove(0);
54+
expect(sTree.size).toBe(0);
55+
expect(sTree._root).toBe(Nil);
56+
});
57+
58+
function checkNil() {
59+
expect(Nil.size).toBe(0);
60+
expect(Nil.left).toBe(Nil);
61+
expect(Nil.right).toBe(Nil);
62+
expect(Nil.parent).toBe(Nil);
63+
expect(Nil.value).toBe(null);
64+
}
65+
it('test updateChild', function() {
66+
var e = updateChild(Nil, Nil);
67+
checkNil();
68+
expect(e).toBe(Nil);
69+
var root = new Node(10, Nil, Nil, Nil, 1);
70+
var left = new Node(5, root, Nil, Nil, 1);
71+
var right = new Node(15, root, Nil, Nil, 1);
72+
var leftLeft = new Node(10, left, Nil, Nil, 1);
73+
left.left = leftLeft;
74+
root.left = left;
75+
root.right = right;
76+
updateChild(left, leftLeft);
77+
expect(leftLeft.parent).toBe(root);
78+
expect(root.left).toBe(leftLeft);
79+
updateChild(leftLeft, Nil);
80+
checkNil();
81+
expect(root.left).toBe(Nil);
82+
updateChild(Nil, right);
83+
expect(right.parent).toBe(Nil);
84+
});
85+
});

0 commit comments

Comments
 (0)