没有父母的AVL树继承人

时间:2016-10-20 23:38:56

标签: java algorithm tree avl-tree

我需要一些帮助。我有一项非常重要的任务。我有AVL树,但我需要找到没有父母的继任者。 这是我的AVL树

public class AVL<T extends Comparable<T>> {

    private TreeNodee<T> root;
    private int size;
    private TreeNodee<T> currrent = null;

    public void add(T data) {
        if (contains(data)){
            currrent.counter++;
            return;
        }
        TreeNodee<T> newNode = new TreeNodee<T>(data);
        root = add(root,newNode);
        size++;
    }
    public TreeNodee<T> successor(TreeNodee<T> node){
        if( node.right == null){
            if(compare(root.data,node.data) < 0){
                TreeNodee<T> node2;
                TreeNodee<T> node3;
                TreeNodee<T> node4;
                node4 = root;
                node2 = root;
                node3 = node2;
                while(compare(node2.left.data, node.data) != 0 && compare(node2.right.data, node.data) != 0
                                                && compare(node3.left.data, node.data) != 0 && compare(node3.right.data, node.data) != 0){
                    node2 = node2.right;
                    node3 = node2.left;
                }
                if(compare(node2.left.data, node.data) == 0){
                    return node2;
                }
                if (compare(node2.right.data, node.data) == 0){
                    while (compare(node4.right.data, node2.data) != 0 ){
                        node4 = node4.right;
                    }
                    return node4;
                }

                if(compare(node3.left.data, node.data) == 0){
                    return node3;
                }
                if(compare(node3.right.data, node.data) == 0){
                    return node2;
                }
            }
            if(compare(root.data,node.data) > 0){
                TreeNodee<T> node2;
                TreeNodee<T> node3;
                TreeNodee<T> node4;
                node4 = root;
                node2 = root;
                node3 = root;
                while(compare(node2.left.data, node.data) != 0 && compare(node2.right.data, node.data) != 0
                        && compare(node3.right.data, node.data) != 0 && compare(node3.left.data, node.data) != 0 ){
                    node2 = node2.left;
                    node3 = node2.right;

                }

                if (compare(node2.right.data, node.data) == 0){
                    while (compare(node4.left.data, node2.data) != 0 ){
                        node4 = node4.left;
                    }
                    return node4;
                }

                if(compare(node2.left.data, node.data) == 0){
                    return node2;
                }

                if(compare(node3.left.data, node.data) == 0){
                    return node3;
                }
                if(compare(node3.right.data, node.data) == 0){
                    return node2;
                }
            }
        }

        if(node.right != null && node.right.left == null)
            return node.right;
        if(node.right.right == null && node.right.left != null){
            TreeNodee min;
            min = node.right.left;
            while(min.left != null)
                min = min.left;
            return min;
        }

            return null;
    }
    public void traverse(java.util.function.BiConsumer visitor) {
        TreeNodee node = min();
        while (node != null) {
            visitor.accept(node.data, node.counter);
           // node = sucessor(node);
        }
    }
    public TreeNodee<T> getRoot(){
        return root;
    }


    public boolean contains(T data) {
        if (isEmpty())return false;
        return contains(root,data);
    }

    private boolean contains(TreeNodee<T> current, T n){
        if(current==null)return false;
        if(compare(current.data,n) == 0){
            currrent = current;
            return true;
        }
        else{
            if(contains(current.right,n)){return true;}
            else if(contains(current.left,n)){return true;}
            return false;
        }
    }


    private TreeNodee<T> add(TreeNodee<T> current, TreeNodee<T> n){
        if (current == null){
            n.bf = 0;
            n.height = 0;
            return n;
        }
        if (compare(n.data,current.data)>0){
            current.right = rotate(add(current.right,n));
        }
        else{
            current.left = rotate(add(current.left,n));
        }
        current = rotate(current);

        return current;
    }


    public T remove(T data) {
        if(!contains(data)){
            return null;
        }
        root = rotate(remove(root,data));
        size--;
        return data;
    }
    public TreeNodee min(){
        TreeNodee min;
        min = root;
        while(min.left != null)
            min = min.left;
        return min;
    }

    private TreeNodee<T> remove(TreeNodee<T> current, T n){

        if (compare(current.data,n)==0){
            if(current.right == null && current.left== null){
                return null;
            }
            else if(current.right == null){
                return rotate(current.left);
            }
            else if(current.left == null){
                return rotate(current.right);
            }
            else{
                TreeNodee<T> pre = current.left;
                TreeNodee<T> predecessor;
                if (pre.right==null){
                    predecessor = pre;
                    predecessor.right = current.right;
                }
                else{
                    while(pre.right.right!=null){
                        pre = pre.right;
                    }
                    predecessor = pre.right;
                    pre.right = predecessor.left;
                    predecessor.left = current.left;
                    predecessor.right = current.right;
                }
                return predecessor;
            }
        }
        else{
            if (compare(n,current.data)>0){
                current.right = rotate(remove(current.right,n));
            }
            else{
                current.left = rotate(remove(current.left,n));
            }
            return rotate(current);
        }
    }



    private TreeNodee<T> updateHeightAndBF(TreeNodee<T> n) {

        int left,right;
        left = n.left!=null ? n.left.height : -1;
        right = n.right!=null ? n.right.height : -1;
        n.bf = left-right;
        n.height = (right>left ? right : left) +1;
        return n;
    }


    private TreeNodee<T> rotate(TreeNodee<T> n) {
        if(n == null)return n;
        n = updateHeightAndBF(n);
        if(n.bf<-1){
            if(n.right.bf>0){
                n = rightLeft(n);
            }
            else{
                n = left(n);
            }
        }
        else if(n.bf>1){
            if(n.left.bf<0){
                n = leftRight(n);
            }
            else{
                n = right(n);
            }
        }
        return n;
    }


    private TreeNodee<T> left(TreeNodee<T> n) {
        TreeNodee<T> newRoot = n.right;
        TreeNodee<T> temp = n.right.left;
        n.right.left = n;
        n.right = temp;
        n = updateHeightAndBF(n);
        return newRoot;
    }


    private TreeNodee<T> right(TreeNodee<T> n) {
        TreeNodee<T> newRoot = n.left;
        TreeNodee<T> temp = n.left.right;
        n.left.right = n;
        n.left = temp;
        n = updateHeightAndBF(n);
        return newRoot;
    }


    private TreeNodee<T> leftRight(TreeNodee<T> n) {
        n.left = left(n.left);
        n = right(n);
        return n;
    }


    private TreeNodee<T> rightLeft(TreeNodee<T> n) {
        n.right = right(n.right);
        n = left(n);
        return n;
    }


    public boolean isEmpty() {
        if (size==0) return true;
        return false;
    }


    private int compare(T d1,T d2){
        if (d1==null && d2 == null){
            return 0;
        }
        else if(d1==null){
            return 1;
        }
        else if(d2==null){
            return -1;
        }
        else{
            return d1.compareTo(d2);
        }
    }
}

我的TreeNode

public class TreeNodee<K extends Comparable<K>> {
    public  K data;
    int counter;
    public  TreeNodee<K> left, right;
    public  int height;
    public int bf;


    public TreeNodee(K data) {
        this.data = data;
        counter = 1;
    }
}

像所有正确放置的情况一样,但我找不到错误。在此先感谢您的帮助!

0 个答案:

没有答案