In Order Successor in Binary Search Tree

前端 未结 17 1870
孤独总比滥情好
孤独总比滥情好 2020-11-27 04:11

Given a node in a BST, how does one find the next higher key?

17条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-27 04:45

    With Binary Search Tree, the algorithm to find the next highest node of a given node is basically finding the lowest node of the right sub-tree of that node.

    The algorithm can just be simply:

    1. Start with the right child of the given node (make it the temporary current node)
    2. If the current node has no left child, it is the next highest node.
    3. If the current node has a left child, make it the current node.

    Repeat 2 and 3 until we find next highest node.

提交回复
热议问题