首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >BST : inOrder后继者和前任者

BST : inOrder后继者和前任者
EN

Stack Overflow用户
提问于 2012-06-28 06:12:25
回答 3查看 3K关注 0票数 0

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

代码语言:javascript
复制
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);
    }
}
EN

回答 3

Stack Overflow用户

发布于 2012-11-05 06:36:32

不,它们是不正确的。节点的前置节点不能在右子树中。节点的后继节点不能在左子树中。

票数 1
EN

Stack Overflow用户

发布于 2013-02-24 03:36:22

代码语言:javascript
复制
    //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;
        }
    }
票数 0
EN

Stack Overflow用户

发布于 2016-03-24 18:59:34

代码语言:javascript
复制
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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11235533

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档