Abstract


  • Also known as BST
  • Nodes have a value attached to it. If the left sub-tree isn’t empty, all nodes on that sub-tree is smaller than the value of Root Node. If the right sub-tree isn’t empty, all nodes on that sub-tree is bigger than the root node value. Its left sub-tree and right sub-tree are also binary search tree

Access previous node from current node

Use a variable prevNode to keep track of the previous node. Handy in solving some problems.

Practice questions

BST Node Traversal


  • We can use In-Order Traversal to print the nodes inside a BST from smallest to the biggest or from biggest to smallest with

From smallest to the biggest

Go to the left subtree then right subtree. Useful in finding the minimum absolute difference among the nodes

From biggest to the smallest

Go to the right subtree then left subtree. Useful in converting the BST to Greater Tree.


Attention

We can only achieve if the BST is Height-Balanced. A not height-balanced BST can be degraded into a Degenerate Binary Tree.

  • If the current node isn’t the node we are finding, select the node.left when target value is smaller than the current node, otherwise select node.right
  • This allows us to choose only one side of the Binary Search Tree (二叉搜索树) at each Level, achieving speed

Practice questions

BST Node Insertion


Attention

We can only achieve if the BST is Height-Balanced. A not height-balanced BST can be degraded into a Degenerate Binary Tree.

Practice questions

BST Node Deletion


  • When the Degree of the deleted node is , we can just remove it without any other modifications

/* 
pre is the parent node of the deleted node
root is the root node of the binary search tree
cur is the deleted node
*/
 
TreeNode child = cur.left != null ? cur.left : cur.right;
// delete node
if (cur != root) {
  if (pre.left == cur)
    pre.left = child;
  else pre.right = child;
} else {
  // if the deleted node is the root node,we assign its child node as the new root node
  root = child;
}
  • When the Degree of the deleted node is , we can just need to smallest node from its right subtree without any other modifications

/* 
pre is the parent node of the deleted node
root is the root node of the binary search tree
cur is the deleted node
*/
 
TreeNode child = cur.left != null ? cur.left : cur.right;
// delete node
if (cur != root) {
  if (pre.left == cur)
    pre.left = child;
  else pre.right = child;
} else {
  // if the deleted node is the root node,we assign its child node as the new root node
  root = child;
}
  • When the Degree of the deleted node is , we can just need to replace it with smallest node from its right subtree or the biggest node from its left subtree. Below shows replacing the deleted node with the smallest node from its right subtree

// cur is the deleted node
 
// finding the smallest node from deleted node's right subtree, and assign it to tmp
TreeNode tmp = cur.right;
while (tmp.left != null) {
  tmp = tmp.left;
}
 
// Delete tmp from the right subtree of the deleted node, the remove() implementation is same as delete node that has a degree of 0
remove(tmp.val);
// replace the deleted node with the smallest node from its right subtree
cur.val = tmp.val;
  • We can encapsulate the logic of BST node deletion in the three situations described above into a single remove() function
/*
pre is the parent node of the deleted node
root is the root node of the binary search tree
cur is the deleted node
*/
 
void remove(int num) {
  // if tree is empty, no node to delete, terminate the remove() operation
  if (root == null) return;
  
  TreeNode cur = root, pre = null;
  
  // Find the deleted node - curr
  while (cur != null) {
    // Deleted node found! Proceed to deletion
    if (cur.val == num) break;
    
    pre = cur;
    // Since the value of deleted node is bigger, go to right subtree to find the deleted node
    if (cur.val < num)
      cur = cur.right;
    // Since the value of deleted node is smaller, go to left subtree to find the deleted node
    else cur = cur.left;
  }
  
  // Deleted node not found, terinate the remove() operation
  if (cur == null) return;
  
  // The degree of deleted node is 0 or 1
  if (cur.left == null || cur.right == null) {
    TreeNode child = cur.left != null ? cur.left : cur.right;
    // delete node
    if (cur != root) {
      if (pre.left == cur)
        pre.left = child;
      else
        pre.right = child;
    } else {
      // if the deleted node is the root node,we assign its child node as the
      // new root node
      root = child;
    }
  }
  // The degree of deleted node is 2
  else {
    // finding the smallest node from deleted node's right subtree, and assign
    // it to tmp
    TreeNode tmp = cur.right;
    while (tmp.left != null) {
      tmp = tmp.left;
    }
 
    // Delete tmp from the right subtree of the deleted node, the remove()
    // implementation is same as delete node that has a degree of 0
    remove(tmp.val);
    // replace the deleted node with the smallest node from its right subtree
    cur.val = tmp.val;
  }

Practice questions

Leetcode Question


Properties

Modification & Structure

Common Ancestor

Terminologies


Greater Tree

  • Every value of node of the original BST is changed to the original value of node plus the sum of all value of nodes greater than the value of the original node of in BST

Practice questions

References