Finding the Predecessor and Successor Node of a Binary Search Tr

  • 时间:2020-09-07 12:03:44
  • 分类:网络文摘
  • 阅读:105 次

A Binary Search Tree (BST) is a commonly used data structure that can be used to search an item in O(LogN) time. A BST should have the following characteristics: its left nodes are smaller than the root and its right nodes are larger than the root.

If we perform an inorder traversal: left nodes first, current node, and then right nodes – we will have a fully sorted sequence.

inorder-traversal-of-a-bst Finding the Predecessor and Successor Node of a Binary Search Tree algorithms

inorder-traversal-of-a-bst

To find the Predecessor or Sucessor Node of a BST – we can perform the following algorithms:

predecessor-and-successor-of-a-bst Finding the Predecessor and Successor Node of a Binary Search Tree algorithms

predecessor-and-successor-of-a-bst

Find the Predecessor Node of a Binary Search Tree

The predecessor node is the largest node that is smaller than the root (current node) – thus it is on the left branch of the Binary Search Tree, and the rightmost leaf (largest on the left branch).

The C++ function to find the predecessor node of a BST node:

1
2
3
4
5
6
TreeNode* predecessor(TreeNode* root) {
  if (!root) return nullptr;
  root = root->left;
  while (root->right) root = root->right;
  return root;
}
TreeNode* predecessor(TreeNode* root) {
  if (!root) return nullptr;
  root = root->left;
  while (root->right) root = root->right;
  return root;
}

And below is the Java implementation to get the predecessor node of a Binary Search Tree:

1
2
3
4
5
6
public int predecessor(TreeNode root) {
  if (root == null) return null;
  root = root.left;
  while (root.right != null) root = root.right;
  return root;
} 
public int predecessor(TreeNode root) {
  if (root == null) return null;
  root = root.left;
  while (root.right != null) root = root.right;
  return root;
} 

Python function to get the predecessor of a BST:

1
2
3
4
5
6
7
def predecessor(root):
  if root is None:
    return None
  root = root.left
  while root.right:
    root = root.right
  return root
def predecessor(root):
  if root is None:
    return None
  root = root.left
  while root.right:
    root = root.right
  return root

Find the Successor Node of a Binary Search Tree

On the other hand, the successor node is the smallest node that is bigger than the root/current – thus it is on the right branch of the BST, and also on the leftmost leaf (smallest on the right branch).

The C++ function to get the successor node of a Binary Search Tree.

1
2
3
4
5
6
TreeNode* successor(TreeNode* root) {
  if (!root) return nullptr;
  root = root->right;
  while (root->left) root = root->left;
  return root;
}
TreeNode* successor(TreeNode* root) {
  if (!root) return nullptr;
  root = root->right;
  while (root->left) root = root->left;
  return root;
}

Java method to get the successor:

1
2
3
4
5
6
public int successor(TreeNode root) {
  if (root == null) return null;
  root = root.right;
  while (root.left != null) root = root.left;
  return root;
} 
public int successor(TreeNode root) {
  if (root == null) return null;
  root = root.right;
  while (root.left != null) root = root.left;
  return root;
} 

Finally, the below is the Python implementation of getting a sucessor node of a BST:

1
2
3
4
5
6
7
def successor(root):
  if root is None:
    return None
  root = root.right
  while root.left:
    root = root.left
  return root
def successor(root):
  if root is None:
    return None
  root = root.right
  while root.left:
    root = root.left
  return root

All implementation of finding successor or predecessor takes O(1) constant space and run O(N) time (when BST is just a degraded linked list) – however, on average, the complexity is O(LogN) where the binary tree is balanced.

Finding successor or predecessor is very useful – for example, we can use this to delete a node in a binary search tree.

–EOF (The Ultimate Computing & Technology Blog) —

推荐阅读:
百度算法经常更新要怎么解决?  讲一讲我这10年的站长经历  为什么企业网站不要用模板建站 模板建站有哪些弊端  网站安全渗透测试难度系数有多大  什么内容才是被百度肯定的优质内容?优质内容应该这样做!  seo-网站权重怎么提高?  万词霸屏与SEO优化合二为一才能给企业带来真实效益  熟知百度蜘蛛原理,按照优化规则才能做好seo优化  SEO关键词排名优化原理是什么?  织梦程序如何调用自定义字段? 
评论列表
添加评论