In Order Successor in Binary Search Tree

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

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

17条回答
  •  离开以前
    2020-11-27 04:47

    Here's an implementation without the need for parent links or intermediate structures (like a stack). This in-order successor function is a bit different to what most might be looking for since it operates on the key as opposed to the node. Also, it will find a successor of a key even if it is not present in the tree. Not too hard to change if you needed to, however.

    public class Node> {
    
    private T data;
    private Node left;
    private Node right;
    
    public Node(T data, Node left, Node right) {
        this.data = data;
        this.left = left;
        this.right = right;
    }
    
    /*
     * Returns the left-most node of the current node. If there is no left child, the current node is the left-most.
     */
    private Node getLeftMost() {
        Node curr = this;
        while(curr.left != null) curr = curr.left;
        return curr;
    }
    
    /*
     * Returns the right-most node of the current node. If there is no right child, the current node is the right-most.
     */
    private Node getRightMost() {
        Node curr = this;
        while(curr.right != null) curr = curr.right;
        return curr;
    }
    
    /**
     * Returns the in-order successor of the specified key.
     * @param key The key.
     * @return
     */
    public T getSuccessor(T key) {
        Node curr = this;
        T successor = null;
        while(curr != null) {
            // If this.data < key, search to the right.
            if(curr.data.compareTo(key) < 0 && curr.right != null) {
                curr = curr.right;
            }
            // If this.data > key, search to the left.
            else if(curr.data.compareTo(key) > 0) { 
                // If the right-most on the left side has bigger than the key, search left.
                if(curr.left != null && curr.left.getRightMost().data.compareTo(key) > 0) {
                    curr = curr.left;
                }
                // If there's no left, or the right-most on the left branch is smaller than the key, we're at the successor.
                else {
                    successor = curr.data;
                    curr = null;
                }
            }
            // this.data == key...
            else {
                // so get the right-most data.
                if(curr.right != null) {
                    successor = curr.right.getLeftMost().data;
                }
                // there is no successor.
                else {
                    successor = null;
                }
                curr = null;
            }
        }
        return successor;
    }
    
    public static void main(String[] args) {
        Node one, three, five, seven, two, six, four;
        one = new Node(Integer.valueOf(1), null, null);
        three = new Node(Integer.valueOf(3), null, null);
        five = new Node(Integer.valueOf(5), null, null);
        seven = new Node(Integer.valueOf(7), null, null);
        two = new Node(Integer.valueOf(2), one, three);
        six = new Node(Integer.valueOf(6), five, seven);
        four = new Node(Integer.valueOf(4), two, six);
        Node head = four;
        for(int i = 0; i <= 7; i++) {
            System.out.println(head.getSuccessor(i));
        }
    }
    }
    

提交回复
热议问题