Skip to content

Fixed remove bug where node has 2 children. #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions data-structures/binary-search-tree/binary-search-tree-tests.htm
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ <h1>Binary Search Tree Tests</h1>

setUp: function(){
this.tree = new BinarySearchTree();
this.tree.add(5);
this.tree.add(4);
this.tree.add(10);
this.tree.add(6);
},
Expand All @@ -121,18 +121,18 @@ <h1>Binary Search Tree Tests</h1>
//---------------------------------------------------------------------

testRemoveFirstItem: function(){
this.tree.remove(5);
this.tree.remove(4);
assert.areEqual(2, this.tree.size(), "There should only be two items left.");
assert.areEqual(10, this.tree._root.value, "Root value should now be 10.");
assert.isFalse(this.tree.contains(5));
assert.isFalse(this.tree.contains(4));
},

testRemoveFirstItemToo: function(){
this.tree.remove(10);
this.tree.remove(5);
this.tree.remove(4);
assert.areEqual(1, this.tree.size(), "There should only be one item left.");
assert.areEqual(6, this.tree._root.value, "Root value should now be 6.");
assert.isFalse(this.tree.contains(5));
assert.isFalse(this.tree.contains(4));
assert.isFalse(this.tree.contains(10));
},

Expand All @@ -148,15 +148,34 @@ <h1>Binary Search Tree Tests</h1>
assert.isFalse(this.tree.contains(6));
},

testRemoveAnItemWithTwoChildren: function(){
this.tree.add(20); // Add another child to 10.
this.tree.add(5);
this.tree.add(7);
this.tree.remove(10);

assert.areEqual(5, this.tree.size(), "There should only be 5 items left.");
assert.isFalse(this.tree.contains(10));
},

testRemoveAnItemWithTwoChildrenWhereLeftSubTreeHasNoRightNodes: function(){
this.tree.add(20); // Add another child to 10.
this.tree.remove(10);

assert.areEqual(3, this.tree.size(), "There should only be 3 items left.");
assert.isFalse(this.tree.contains(10));
},

testRemoveLastAll: function(){
this.tree.remove(6);
this.tree.remove(5);
this.tree.remove(4);
this.tree.remove(10);
assert.areEqual(0, this.tree.size(), "There should only be two items left.");
assert.isFalse(this.tree.contains(6));
assert.isFalse(this.tree.contains(5));
assert.isFalse(this.tree.contains(4));
assert.isFalse(this.tree.contains(10));
}
}

}));

//-------------------------------------------------------------------------
Expand Down
10 changes: 9 additions & 1 deletion data-structures/binary-search-tree/binary-search-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,14 +265,22 @@ BinarySearchTree.prototype = {
//reset pointers for new traversal
replacement = current.left;
replacementParent = current;


//find the right-most node
while(replacement.right !== null){
replacementParent = replacement;
replacement = replacement.right;
}

replacementParent.right = replacement.left;

if (replacementParent.right === replacement) {
replacementParent.right = replacement.left;
} else {
//replacement will be on the left when the left most subtree
//of the node to remove has no children to the right
replacementParent.left = replacement.left;
}

//assign children to the replacement
replacement.right = current.right;
Expand Down