[Algorithm] Inorder Successor in a binary search tree

For the given tree, in order traverse is:

  • visit left side
  • root
  • visit right side
// 6,8,10,11,12,15,16,17,20,25,27

The successor is the one right next to the target:

// target 8 --> succossor is 10

So, given the tree and target node, to find its successor.

Require knowledge how recurise call work, mainly it should reach the leaf node, then print it from bottom to top. For the node which has both right and left side, it visits left first, then itself, then right side.

function findCurrent(root, val) {
  let current = root;
  let found = null;
  while (current !== null) {
    if (current.val === val) {
      found = current;
      break;
    } else if (current.val < val) {
      current = current.right;
    } else {
      current = current.left;
    }
  }

  return found;
}

function findMin(root) {
  // min should be on the left side
  while (root.left != null) {
    root = root.left;
  }

  return root;
}

function findSuccsor(root, val) {
  const current = findCurrent(root, val);
  if (current == null) {
    return null;
  }
  // case 1: Node has right subtree
  // Find min on the right part of the node
  // the min should be on the left most
  if (current.right != null) {
    return findMin(current.right);
  }
  // case 2: Node has no right subtree
  // --> A. target node can be on the left side like 8
  // --> B. target node can be on the right side like 12
  else {
    let parent = root;
    let found = null;
    while (parent !== current) {
      // A: if target is smaller than parent, means on the left side
      // then we keep going down to find target, its parent should be
      // the successor
      if (current.val < parent.val) {
        found = parent;
        parent = parent.left;
      }
      // B: it target is larger than parent, means on the right side
      // then it means the parent node should already been visited
      // so we should not set successor in this case
      else {
        parent = parent.right;
      }
    }

    return found;
  }
}

function Node(val) {
  return {
    val,
    left: null,
    right: null
  };
}

function Tree() {
  return {
    root: null,
    addLeft(val, root) {
      const node = Node(val);
      root.left = node;
      return root.left;
    },
    addRight(val, root) {
      const node = Node(val);
      root.right = node;
      return root.right;
    }
  };
}

const tree = new Tree();
const root = Node(15);
tree.root = root;
const n1 = tree.addLeft(10, tree.root);
const n2 = tree.addRight(20, tree.root);

const n3 = tree.addLeft(8, n1);
tree.addLeft(6, n3);

const n4 = tree.addRight(12, n1);
tree.addLeft(11, n4);

const n5 = tree.addLeft(17, n2);
const n6 = tree.addLeft(16, n5);
const n7 = tree.addRight(25, n2);
tree.addRight(27, n7);
// inorder
// 6,8,10,11,12,15,16,17,20,25,27
// successor is the one next to the target
// target 8 --> succossor is 10
console.log(findSuccsor(tree.root, 27));

原文地址:https://www.cnblogs.com/Answer1215/p/10643484.html

时间: 2024-11-07 22:13:37

[Algorithm] Inorder Successor in a binary search tree的相关文章

Verify Preorder/Inorder/Postorder Sequence in Binary Search Tree

Verify Preorder Sequence in Binary Search Tree \Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary search tree. You may assume each number in the sequence is unique. Follow up: Could you do it using on

[Algorithm] Delete a node from Binary Search Tree

The solution for the problem can be divided into three cases: case 1: if the delete node is leaf node, then we can simply remove it case 2: if the delete node is has single side or substree case 3: it has two children, then to keep it as a valid bina

Inorder Successor in Binary Search Tree

Given a binary search tree and a node in it, find the in-order successor of that node in the BST. 1 public class Solution { 2 3 public TreeNode inorderSuccessor(TreeNode root, TreeNode n) { 4 if (root == null) return null; 5 6 if (n.right != null) {

LintCode : Inorder Successor in Binary Search Tree

Very interesting problem! http://www.lintcode.com/zh-cn/problem/inorder-successor-in-binary-search-tree/ Description: Given a binary search tree (See Definition) and a node in it, find the in-order successor of that node in the BST. Example: Given tr

[GeeksForGeeks] Populate inorder successor for all nodes in a binary search tree

Given a binary search tree with the following tree node definition. next points to a node's inorder successor. Populate inorder successor for all nodes in this bst. 1 class TreeNodeWithNext { 2 int val; 3 TreeNodeWithNext left, right, next; 4 public

[email&#160;protected] [173] Binary Search Tree Iterator (InOrder traversal)

https://leetcode.com/problems/binary-search-tree-iterator/ Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. Calling next() will return the next smallest number in the BST. Note: nex

[Leetcode][JAVA] Recover Binary Search Tree (Morris Inorder Traversal)

Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Note:A solution using O(n) space is pretty straight forward. Could you devise a constant space solution? 使用O(n)空间的话可以直接中序遍历来找问题节点. 如果是

A1064. Complete Binary Search Tree (30)

A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys greate

PAT1064. Complete Binary Search Tree

A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys greate