Skip to content
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

BST - remove root node error #46

Closed
JakeHP opened this issue Feb 24, 2015 · 1 comment
Closed

BST - remove root node error #46

JakeHP opened this issue Feb 24, 2015 · 1 comment

Comments

@JakeHP
Copy link
Collaborator

JakeHP commented Feb 24, 2015

The following test (that doesn't exist in codebase) fails with the current BST remove()

  it('should insert and remove single node properly', function () {
    var bTree = new BinaryTree();
    bTree.insert(15);
    var node = bTree.find(15);
    bTree.remove(node);
    expect(bTree._root).toBe(null);
  });

Here's the issue explained in comments

// this is called in .remove()
this._replaceChild(node._parent, node, null); // node._parent is null since the node is the root, node is valid
..
..
..
   function (parent, oldChild, newChild) {
    if (!parent) { //parent param is null, enter if
      this._root = newChild; //assigns null to root
      this._root._parent = null; // null._parent throws error
    } else {
      if (parent._left === oldChild) {
        parent._left = newChild;
      } else {
        parent._right = newChild;
      }

      if (newChild) {
        newChild._parent = parent;
      }
    }

I've got a fix in place, a bunch of tests for BST, and will submit a PR once #45 clears (changes are on my local master).

@JakeHP
Copy link
Collaborator Author

JakeHP commented Feb 24, 2015

Fixed as of #47 , closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant