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

BinarySearchTree can't remove a root node correctly #310

Open
hifizz opened this issue Feb 16, 2019 · 3 comments
Open

BinarySearchTree can't remove a root node correctly #310

hifizz opened this issue Feb 16, 2019 · 3 comments

Comments

@hifizz
Copy link

hifizz commented Feb 16, 2019

In the test file BinarySearchTree.test.js add a test as below:

it('should remove root node correctly', () => {
    const bst = new BinarySearchTree();
    bst.insert(1).insert(2);
    expect(bst.toString()).toBe('1,2');
    bst.remove(1);
    bst.remove(2);
    expect(bst.toString()).toBe('');
    bst.insert(5);
    expect(bst.toString()).toBe('5');
});

It will report an error:

    Expected: "5"
    Received: ",5"

      15 |     expect(bst.toString()).toBe('');
      16 |     bst.insert(5);
    > 17 |     expect(bst.toString()).toBe('5');
         |                            ^
      18 |   });
      19 | });
      20 | 

Before bst.insert(5) bst is:

      BinarySearchTree {
        root: 
         BinarySearchTreeNode {
           left: null,
           right: null,
           parent: null,
           value: undefined,
           meta: HashTable { buckets: [Array], keys: {} },
           nodeComparator: Comparator { compare: [Function: defaultCompareFunction] },
           compareFunction: undefined,
           nodeValueComparator: Comparator { compare: [Function: defaultCompareFunction] } },
        nodeComparator: Comparator { compare: [Function: defaultCompareFunction] } }

after that, bst is:

      BinarySearchTree {
        root: 
         BinarySearchTreeNode {
           left: null,
           right: 
            BinarySearchTreeNode {
              left: null,
              right: null,
              parent: [Circular],
              value: 5,
              meta: [Object],
              nodeComparator: [Object],
              compareFunction: undefined,
              nodeValueComparator: [Object] },
           parent: null,
           value: undefined,
           meta: HashTable { buckets: [Array], keys: {} },
           nodeComparator: Comparator { compare: [Function: defaultCompareFunction] },
           compareFunction: undefined,
           nodeValueComparator: Comparator { compare: [Function: defaultCompareFunction] } },
        nodeComparator: Comparator { compare: [Function: defaultCompareFunction] } }

This looks like a bug in design, we should remove the node rather than just set the node value to be undefined.

@pbrahmbhatt3
Copy link

Can I work on it? @hifizz

@nikhilskul7
Copy link

Can i work on this?@hifizz

@hifizz
Copy link
Author

hifizz commented May 19, 2020

Can i work on this?@hifizz

Sure

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

3 participants