What is your hint or solution suggestion?
var displayTree = tree => console.log(JSON.stringify(tree, null, 2));
function Node(value) {
this.value = value;
this.left = null;
this.right = null;
}
function BinarySearchTree() {
this.root = null;
// change code below this line
// recursively called function to traverse tree and insert new node
this.insertNode = (node, newNode) => {
if(newNode.value < node.value) {
if(!node.left) node.left = newNode
else this.insertNode(node.left, newNode)
}else{
if(!node.right) node.right = newNode;
else this.insertNode(node.right,newNode)
}
}
// add new node
this.add = (int) => {
const node = new Node(int);
if(!this.root) this.root = node;
else{
this.insertNode(this.root, node);
}
}
// change code above this line
}
Challenge: Add a New Element to a Binary Search Tree
Link to the challenge: