public static <T> BinaryTree<T> find_successor(BinaryTree<T> node) {
        BinaryTree<T> n = node;
        if(n.getRight() != null) {
            // Find the leftmost element in n's right subtree.
            n = n.getRight();
            while(n.getLeft() != null) {
                n = n.getLeft();
            }
            return n;
        }

        // Find the first parent whose left child contains n.
        while(n.getParent() != null && n.getParent().getRight() == n) {
            n = n.getParent();
        }
        // Return nullptr means n does not have successor.
        return n.getParent();
    }