computer-science-in-javascript icon indicating copy to clipboard operation
computer-science-in-javascript copied to clipboard

ES6 Tree Remove. Sibling to removed node is not added to BFS queue

Open tenthirtyone opened this issue 8 years ago • 0 comments

https://github.com/benoitvallon/computer-science-in-javascript/blob/master/data-structures-in-javascript/tree.es6.js#L33

Splice in the for loop shifts the elements in the array and skips the sibling of the node that was removed.

Fix PR:

You can test with:

const tree = new Tree();
tree.add('ceo');
tree.add('cto1', 'ceo');
tree.add('cto2', 'ceo');
tree.add('cto3', 'ceo');
tree.add('dev1', 'cto1');
tree.add('dev2', 'cto1');
tree.add('dev3', 'cto1');
tree.add('dev2', 'dev3');
tree.add('dev1', 'cto2');
tree.add('dev2', 'cto2');
tree.add('dev3', 'cto2');
tree.add('dev1', 'cto3');
tree.add('dev2', 'cto3');
tree.add('dev3', 'cto3');
tree.printByLevel();
tree.remove('dev2');
tree.printByLevel();

tenthirtyone avatar Dec 30 '16 23:12 tenthirtyone