Skip to content

Commit f562911

Browse files
committed
bst remove root node fix. bst specs.
1 parent f164e7e commit f562911

File tree

2 files changed

+91
-11
lines changed

2 files changed

+91
-11
lines changed

src/data-structures/binary-search-tree.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@
181181
* Average time complexity: O(log N).
182182
*
183183
* @public
184-
* @param {Number|String} Value of the node which should be found.
184+
* @param {Number|String} value of the node which should be found.
185185
*/
186186
exports.BinaryTree.prototype.find = function (value) {
187187
return this._find(value, this._root);
@@ -192,8 +192,8 @@
192192
* Average time complexity: O(log N).
193193
*
194194
* @private
195-
* @param {Number|String} Value of the node which should be found.
196-
* @param {Node} Current node to be checked.
195+
* @param {Number|String} value of the node which should be found.
196+
* @param {Node} current node to be checked.
197197
*/
198198
exports.BinaryTree.prototype._find = function (value, current) {
199199
if (!current) {
@@ -232,7 +232,6 @@
232232
} else {
233233
parent._right = newChild;
234234
}
235-
236235
if (newChild) {
237236
newChild._parent = parent;
238237
}
@@ -244,7 +243,7 @@
244243
* Average runtime complexity: O(log N).
245244
*
246245
* @public
247-
* @param {Node} Node to be removed
246+
* @param {Node} node to be removed
248247
* @returns {Boolean} True/false depending
249248
* on whether the given node is removed.
250249
*/
@@ -261,12 +260,16 @@
261260
min.value = temp;
262261
return this.remove(min);
263262
} else {
264-
if (node._left) {
265-
this._replaceChild(node._parent, node, node._left);
266-
} else if (node._right) {
267-
this._replaceChild(node._parent, node, node._right);
268-
} else {
269-
this._replaceChild(node._parent, node, null);
263+
if (node._parent !== null) {
264+
if (node._left) {
265+
this._replaceChild(node._parent, node, node._left);
266+
} else if (node._right) {
267+
this._replaceChild(node._parent, node, node._right);
268+
} else {
269+
this._replaceChild(node._parent, node, null);
270+
}
271+
}else {
272+
this._root = null;
270273
}
271274
return true;
272275
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
'use strict';
2+
3+
var mod = require('../../src/data-structures/binary-search-tree.js');
4+
var Node = mod.Node;
5+
var BinaryTree = mod.BinaryTree;
6+
7+
describe('Node', function () {
8+
it('should be a constructor function', function () {
9+
expect(typeof Node).toBe('function');
10+
});
11+
});
12+
13+
describe('Binary Tree', function () {
14+
it('should be a constructor function', function () {
15+
expect(typeof BinaryTree).toBe('function');
16+
});
17+
it('should start with null root', function () {
18+
expect(new BinaryTree()._root).toBe(null);
19+
});
20+
it('should insert and remove single node properly', function () {
21+
var bTree = new BinaryTree();
22+
bTree.insert(15);
23+
var node = bTree.find(15);
24+
bTree.remove(node);
25+
expect(bTree._root).toBe(null);
26+
});
27+
it('should insert multiple nodes properly', function () {
28+
var bTree = new BinaryTree();
29+
bTree.insert(10);
30+
bTree.insert(5);
31+
bTree.insert(15);
32+
bTree.insert(4);
33+
bTree.insert(6);
34+
bTree.insert(14);
35+
bTree.insert(16);
36+
var leftRootChild = bTree._root._left;
37+
var rightRootChild = bTree._root._right;
38+
expect(bTree._root.value).toBe(10);
39+
expect(leftRootChild.value).toBe(5);
40+
expect(rightRootChild.value).toBe(15);
41+
expect(leftRootChild._left.value).toBe(4);
42+
expect(leftRootChild._right.value).toBe(6);
43+
expect(rightRootChild._left.value).toBe(14);
44+
expect(rightRootChild._right.value).toBe(16);
45+
});
46+
it('should remove multiple nodes properly', function () {
47+
var bTree = new BinaryTree();
48+
bTree.insert(10);
49+
bTree.insert(5);
50+
bTree.insert(15);
51+
bTree.insert(4);
52+
bTree.insert(6);
53+
bTree.insert(7);
54+
bTree.insert(14);
55+
bTree.insert(16);
56+
var leftRootChild = bTree._root._left;
57+
var rightRootChild = bTree._root._right;
58+
var sixteen = bTree.find(16);
59+
bTree.remove(sixteen);
60+
expect(bTree._root.value).toBe(10);
61+
expect(leftRootChild.value).toBe(5);
62+
expect(rightRootChild.value).toBe(15);
63+
expect(leftRootChild._left.value).toBe(4);
64+
expect(leftRootChild._right.value).toBe(6);
65+
expect(leftRootChild._right._right.value).toBe(7);
66+
expect(rightRootChild._left.value).toBe(14);
67+
expect(rightRootChild._right).toBe(null);
68+
var fourteen = bTree.find(14);
69+
bTree.remove(fourteen);
70+
expect(rightRootChild._left).toBe(null);
71+
var five = bTree.find(5);
72+
bTree.remove(five);
73+
expect(leftRootChild.value).toBe(6);
74+
expect(leftRootChild._left.value).toBe(4);
75+
expect(leftRootChild._right.value).toBe(7);
76+
});
77+
});

0 commit comments

Comments
 (0)