0

它们看起来正确吗?我实施了它们,并希望对其进行审查

Node predecessor(Node node) {
    if ((node.left == null) && (node.right==null)) {
        return node;
    }
    if (node.right != null) {
        return predecessor(node.right);
    }
    if (node.left != null) {
        return predecessor(node.left);
    }
}


Node successor(Node node) {
    if ((node.left == null) && (node.right==null)) {
        return node;
    }
    if (node.left != null) {
        return successor(node.left);
    }
    if (node.right != null) {
        return successor(node.right);
    }
}
4

3 回答 3

1

不,他们不正确。节点的前身不能在右子树中。节点的后继节点不能在左子树中。

于 2012-11-04T22:36:32.363 回答
0
    //O(h) time complexity.....
    public static TreeNode GetSuccessorInorder(TreeNode root, TreeNode toFind)
    {
        if (root == null) return null;

        //If right sub-tree is not null - get the min of right sub-tree...
        //If the right subtree of node x is nonempty, then the successor of x is just the left-most node in the right subtree
        if (toFind.right != null)
        {
            TreeNode rootRightSubtree = toFind.right;
            TreeNode curr = rootRightSubtree;

            //The left-most node in the right-subtree is always the minimum.....
            while (curr != null)
                curr = curr.left;

            return curr;
        }
        //If right sub-tree is null - Do in-order search and keep track of last traversed parent.. ;-)
        //If the right subtree of node x is empty and x has a successor y, then y is the lowest ancestor of x whose 
        //left child is also an ancestor of x. 
        else
        {
            TreeNode succ = null;

            // Start from root and search for successor down the tree
            while (root != null)
            {
                //in-order tree walk....                    
                if ((int)toFind.data < (int)root.data)
                {
                    succ = root;
                    root = root.left;
                }
                else if ((int)toFind.data > (int)root.data)
                    root = root.right;
                else
                    break;
            }

            return succ;
        }
    }
于 2013-02-23T19:36:21.833 回答
0
Inorder sucessor in BST..
private TreeNode treeInorderSuccessor(TreeNode root, int data) {
    System.out.println("called");
    TreeNode keyNode = searchTreeNode(root, data);
    if(keyNode == null){
        return keyNode;
    }else{
        if(keyNode.right != null){
            root = findMinFromRight(keyNode.right);
            return root;
        }else{
            TreeNode suc = null;
            TreeNode anc = root;
            while(anc != keyNode){
                if(anc.data > keyNode.data){
                    suc = anc;
                    anc = anc.left;
                }else{
                    anc = anc.right;
                }
            }
            return suc;
        }
    }
} 
private TreeNode searchTreeNode(TreeNode root, int data) {
    if(root == null){
        System.out.println("node not found");
    }else{
        if(root.data == data){
            System.out.println("node found");
        }else if(root.data >= data){
            root = searchTreeNode(root.left, data);
        }else if(root.data < data){
            root = searchTreeNode(root.right, data);
        }
    }
    return root;
}

private TreeNode findMinFromRight(TreeNode node) {
    while(node.left != null){
        node = node.left;
    }
    return node;
}
于 2016-03-24T10:59:34.087 回答