Delete a Leaf Node in a Binary Search Tree - Test failure

Tell us what’s happening:
Hi, I am not able to pass the last test. Can someone take a look and give me some hint, thank you.

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;
// case 1: target has no children, change code below this line
this.remove = function(node, data) {
    if(node === null) {      
      return null;
    }else if (node.value == data && node.left == null && node.right == null) {
      this.root = null;
      return null;
    } else if (data < node.value) {
      node.left = this.remove(node.left, data)
      return node;
    } else if (data > node.value) {
      node.right = this.remove(node.right, data)
      return node;
    }
  }
  
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36.

Challenge: Delete a Leaf Node in a Binary Search Tree

Link to the challenge:
https://www.freecodecamp.org/learn/coding-interview-prep/data-structures/delete-a-leaf-node-in-a-binary-search-tree

1 Like

@laufmartin Try just adding some data to the tree and see what happens. You can add in the add method from a previous challenge.

var test = new BinarySearchTree();
test.add(5);
test.add(3);
test.add(7);
test.add(6);
test.add(10);
test.add(12);
console.log(JSON.stringify(test.root, null, 2))
test.remove(3);
console.log(JSON.stringify(test.root, null, 2)) 

What do you expect test to look like after just removing 3?

1 Like

Hi I would expect something liek that (but I am not entirely sure):

{
  "value": 5,
  "left": null,
  "right": {
    "value": 7,
    "left": {
      "value": 6,
      "left": null,
      "right": null
    },
    "right": {
      "value": 10,
      "left": null,
      "right": {
        "value": 12,
        "left": null,
        "right": null
      }
    }
  }
}

But I have no idea why it is not working. Any suggestions??

I suggest adding some console.log statements at various points, so you can see exactly why your tree ends up with the value null after just removing the 3.

FYI - The tests expect the first argument of the remove method to be the value you are supposed to remove. You have defined your remove method to have node to be the first argument, so that is a big problem in itself.

I changed my code as you suggested but now I have different problem now. I dont know how to change root to null from inner function. I have this error: TypeError: Cannot set property ‘root’ of undefined

    this.remove = function (data){
      
        var removeNode = function (node, data) {
            if (node == null){

                return null;
            }
            if (node.value == data) {
                if (node.left==null && node.right == null){
                  this.root= null;
                    return null;
                }
            } else if (data < node.value) {
                node.left = removeNode(node.left, data);
                return node;
            } else {
                node.right = removeNode(node.right, data);
                return node;
            }
        }
        return removeNode(this.root, data);
    }

Problem solved:

    this.remove = function (data){
        var removeNode = function (node, data) {
            if (node == null){
                return null;
            }
            if (node.value == data) {
                if (node.left==null && node.right == null){
                    return null;
                }
            } else if (data < node.value) {
                node.left = removeNode(node.left, data);
                return node;
            } else {
                node.right = removeNode(node.right, data);
                return node;
            }
        }
        this.root = removeNode(this.root, data);
        return this.root;
    }
2 Likes