Find the Minimum and Maximum Value in a Binary Search Tree help

Find the Minimum and Maximum Value in a Binary Search Tree

Without values I cannot to test the return value. I needed to write “blindly” this code…
and somethings are wrong because fails on the test. I have no idea how can I test the code without values…

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.findMin = function() {
    if (!(this.root)) return null;
    const node = this.root;
    while (node.left) {
      node = node.left;
    }
    return node.value;
  };

  this.findMax = function() {
    if (!(this.root)) return null;
    const node = this.value;
    while (node.right) {
      node = node.right;
    }
    return node.value;
  };
  // change code above this line
}

You will need to create an add method to be able to create a Binary Search Tree and then add values to it. Then, you can test your two methods (findMin and findMax) to see if they do indeed work.

We probably should make Add a New Element to a Binary Search Tree the first challenge instead of the second.

Thank you, I made the add method and I see what the matter.
let node = ... I declared a constant instead of variable…
And in the findMax a silly mistake: this.root and not this.value

1 Like

I went ahead and created this pull request to switch the first two BST challenges.