freeCodeCamp Challenge Guide: Add a New Element to a Binary Search Tree

Add a New Element to a Binary Search Tree

This is a stub. Help the community by making a suggestion of a hint and/or solution. We may use your suggestions to update this stub.


Solutions

Solution 1 (Click to Show/Hide)
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;
  // Only change code below this line
  this.add = function(value) {
    const newNode = new Node(value);

    if (!this.root) {
      this.root = newNode;
      return undefined;
    }

    let currentNode = this.root;
    while (currentNode) {
      if (currentNode.value === newNode.value) {
        return null;
      }

      const direction = currentNode.value > newNode.value ? "left" : "right";
      if (!currentNode[direction]) {
        currentNode[direction] = newNode;
        return undefined;
      }

      currentNode = currentNode[direction];
    }
  }
  // Only change code above this line
}
4 Likes