1

Below is the code that I'm using to find the minimum height of a Binary Search Tree (BST). Original source is here. The definition of minimum height used here is the distance from the root node to the first leaf node that does not contain two children. Through my heavy use of console.logs, I've understood all but the second to last update of the heights.

If you run the code below, the fifth to last log indicates that: Right is -1 and left is -1, which I understand. However, the log after it indicates updating the value of the 'right' recursive call, setting the value of right to 'right + 1.' (Since right was -1. -1 + 1 = 0)

As such, I expect the following: Left = -1, Right = 0.

After the final recursive call, setting right to right + 1 again, I expect the final result to be Left: -1, right: 1.

However, the output is: Left 0, Right: 0. Then after the final update, Left: 0, Right: 1.

So, what am I missing? How are the left and right variables getting updated at the same time?

/* Binary Search Tree */

class Node {
  constructor(data, left = null, right = null) {
    this.data = data;
    this.left = left;
    this.right = right;
  }
}

class BST {
  constructor() {
    this.root = null;
  }
  add(data) {
    const node = this.root;
    if (node === null) {
      this.root = new Node(data);
      return;
    } else {
      const searchTree = function(node) {
        if (data < node.data) {
          if (node.left === null) {
            node.left = new Node(data);
            return;
          } else if (node.left !== null) {
            return searchTree(node.left);
          }
        } else if (data > node.data) {
          if (node.right === null) {
            node.right = new Node(data);
            return;
          } else if (node.right !== null) {
            return searchTree(node.right);
          }
        } else {
          return null;
        }
      };
      return searchTree(node);
    }
  }
  findMinHeight(node = this.root) {
      if (node == null) {
          console.log("Minus 1")
          return -1;
      };
      console.log("Direction: L on node ", node.data);
      let left = this.findMinHeight(node.left);
      console.log("Direction: R on node ", node.data);
      let right = this.findMinHeight(node.right);
      console.log("RIGHT", right, "left", left)
      if (left < right) {
          console.log('Result = the value of LEFT + 1');
          return left + 1;
      } else {
          console.log('Result = the value of right + 1');
          return right + 1;
      };
  }
}



const bst = new BST();

bst.add(9);
bst.add(4);
bst.add(17);
bst.add(3);
// bst.add(6);
// bst.add(22);
// bst.add(5);
// bst.add(7);
// bst.add(20);

console.log(bst.findMinHeight());
akb10
  • 83
  • 10

2 Answers2

1

To understand findMinHeight function(at below i simplified as f(node)), i think there is 2 key

  1. recursion function return value from bottom to top of the tree in this case, level by level.
  2. we can found that f(currentNode) return the min height of its branches(we can treat currentNode as root of its branches), following is detail computation(take attached img as example):

enter image description here

At most bottom of the BST, level (4,7,13)

case all: current node has no children at all.  
node.left=null => return -1
node.right=null => return -1
left!<right => return right+1=0 //(-1+1=0)
f(4)=f(7)=f(13)=0

Go up next level, (1,6,14)

case 1: current node has no children at all.
node.left=f(null) => return -1
node.right=f(null) => return -1
left!<right => return right+1=0 //(-1+1=0)
f(1)=0

case 6: current node has two children.
node.left=f(4) => return 0
node.right=f(7) => return 0
left!<right => return right+1=1 //(0+1=1)
f(6)=1

case 14: current node has no children on the right.
node.left=f(13) => return 0
node.right=null => return -1
left!<right => return right+1=0 //(-1+1=0)
f(14)=0

Go next level (3,10), vice versa, when passing up value to the next lvl, next lvl node return its current minHeight, until top of BST (8), root level If write down value return by each node just beside it on BST, it will be crystal clear. Hope it helps other who struggle to understand this.

Yu Hien Ng
  • 45
  • 6
0

You might solve this problem using DFS algorithm:

function minHeight(root) { // DFS algorithm
    if (!root) {
        return 0; // no level since there is not BST
    }

    if (root.left === null && root.right === null) {
        return 1;
    }

    if (!root.left) {
        return minHeight(root.right) + 1; 
    }

    if (!root.right) {
        return minHeight(root.left) + 1; 
    }

    return Math.min(minHeight(root.left), minHeight(root.right)) + 1; 
}

The idea is to traverse the given Binary Tree. For every node, check if it is a leaf node. If yes, then return 1. If not leaf node then if the left subtree is NULL, then recur for the right subtree. And if the right subtree is NULL, then recur for the left subtree. If both left and right subtrees are not NULL, then take the minimum of two heights.

Please read this article to fully understand DFS approach.

ricardoorellana
  • 2,270
  • 21
  • 35