henrylin03 / odin-binary-search-tree

Implementing balanced binary search tree (BST) from a sorted array in JavaScript, as part of The Odin Project's "Full Stack JavaScript" course
MIT License
0 stars 0 forks source link

add `.insert(value)` method that inserts a node with that value into the tree #8

Closed henrylin03 closed 3 months ago

henrylin03 commented 3 months ago

fixes #6 - we do not yet rebalance the tree

uses the recursive method (as opposed to iterative)

tests

const testArray = [1, 7, 4, 23, 8, 9, 4, 3, 5, 7, 9, 67, 6345, 324];
const tree = createTree(testArray);

tree.buildTree();

// inserting
console.log(`
Inserting...
`);
tree.insert(8); // expected: error due to duplicate
tree.insert(69);
tree.insert(420);
tree.insert(-6969);

output: before insertion: image

after insertion (incl error message for trying to insert 8): image