Add a New Element to a Binary Search Tree

Tell us what’s happening:
I don’t understand what’s the problem here ?

the error says ’ The add method adds elements according to the binary search tree rules.’

Your code so far


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
    this.add = function(data){
        var node = new Node(data);
        let currNode = this.root;
        
        if(!this.root) {this.root = node; return undefined;}
        
            while(!currNode){
                if(data == currNode.value) return null;
                else if(ele>currNode.value){
                    if(currNode.right) currNode = currNode.right;
                    else {currNode.right = node;return undefined};
                }
                else {
                    if(currNode.left) currNode = currNode.left;
                    else {currNode.left = node;return undefined;}
                }
            }  
        }
    }

Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:67.0) Gecko/20100101 Firefox/67.0.

Link to the challenge:
https://learn.freecodecamp.org/coding-interview-prep/data-structures/add-a-new-element-to-a-binary-search-tree

Try adding more than one element to your tree and use displayTree. I think you will be surprised at what you have.

1 Like

Can you figure out what is wrong with my code then ?
displayTree() works completely fine and I receive null for every duplicate value
What could go wrong?

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
  var timescalled = 0;
  var allvalues = []
  this.add = function(int){
    var node = new Node(int);
    if(timescalled == 0){
      this.root = node
      timescalled++;
      allvalues.push(node.value)
      return undefined
    } else {
      var currentnode = this.root;
      if(allvalues.includes(int)){
        return null
      }
      var availablenode = addinto(currentnode);
      if(int>availablenode.value){
        availablenode.right = node
        timescalled++;
        allvalues.push(node.value)
        return undefined;
      }

      if(int<availablenode.value){
        availablenode.left = node
        timescalled++;
        allvalues.push(node.value)
        return undefined
      }

      function addinto(current,called = ''){
        
        if(called == 'left' && current.left == null){
            return current
        }else if(called == 'right' && current.right == null){
          return current
        }
        else{
            
            if(int<current.value){
                if(current.left == null){
                  var result = current
                } else{
                  var result = addinto(current.left,called = 'left');
                }
            }
            if(int>current.value){
                if(current.right == null){
                  var result = current
                } else{
                    var result = addinto(current.right,called = 'right');
                }
            }
        }
        return result
      }
    }
  }
  // change code above this line
}
var a = new BinarySearchTree();
a.add(6)
a.add(7)
a.add(5)
a.add(3)
a.add(2)
a.add(4)
a.add(8)
a.add(9)
a.add(10)
displayTree(a)