Java BinaryTree:如何在insert方法中平衡树?

时间:2015-08-29 23:47:26

标签: java avl-tree tree-balancing

我正在尝试使用二叉搜索树来平衡,我知道它为什么不起作用,但我不知道如何修复它。 我直接在insert方法中进行平衡。 我用了一些斜线来说明平衡应该发生在哪里。 像这样代码不起作用,我得到这个例外:

Exception in thread "main" java.lang.NullPointerException
at Baueme.Treee.insert(Treee.java:54)
at Baueme.Treee.insert(Treee.java:18)
at Baueme.TestTheTree.main(TestTheTree.java:12)

当我插入没有平衡everthing是好的。

public class Node {

public Node left, right, parent;
public int value;

public Node (Node parent, int i) {
    this.parent = parent;
    this.value = i;
}

public int height() {
    int l = 0;
    int r = 0;

    if (this.left  != null) {
        l = this.left.height() +1;
    }

    if (this.right  != null) {
        r = this.right.height() +1;
    }

    return Math.max(l,r);
}   
}
public class Tree {
Node root;

public Tree() {
    root = null;
}

public void insert (int value) {

    if (root == null) {
        root = new Node(null, value);
    }
    else {
        insert(root, value);
    }
}

private void insert (Node parent, int value) {

    if (parent.value >= value) {

        if (parent.left == null) {
            parent.left = new Node (parent, value);
        }
        else {
            insert(parent.left, value);

       /////////////////////////    
            if ( parent.left.height() - parent.right.height() == 2) {

                if (value - parent.left.value < 0) {
                    parent = rotateWithLeftChild(parent);
                }
                else {
                    parent = doubleRotateWithRightChild(parent);
                }
            }           
      /////////////////////////
        }
    }

    else {

        if (parent.right == null) {
            parent.right = new Node (parent, value);
        }
        else {
            insert(parent.right, value);
      /////////////////////////
            if ( parent.left.height() - parent.right.height() == -2) {

                if (value - parent.right.value > 0) {
                    parent = rotateWithRightChild(parent);
                }
                else {
                    parent = doubleRotateWithLeftChild(parent);
                }
            }
     /////////////////////////
    }
     }
     }
    public int quantity() {

if (root == null) {
    return 0;
}
else {
    return 1 + quantity(root.left) + quantity(root.right);          
}
 }
public int quantity (Node parent) {

if (parent == null) {
    return 0;
}
else {
    return 1 + quantity(parent.left) + quantity(parent.right);
}
}
public int height () {

int l = 0;
int r = 0;

if ( root.left != null) {
    l = height(root.left) + 1;
}

if (root.right != null) {
    r = height(root.right) +1;
}

return (Math.max(l,r));
}
private int height (Node parent) {

int l = 0;
int r = 0;

if ( parent.left != null) {
    l = height(parent.left) + 1;
}

if (parent.right != null)   {
    r = height(parent.right) +1;
}

return (Math.max(l,r));
}
public String toString () {

    if (root == null) {
        return "empty tree";
    }
    else {
        return toString(root.left) + " ; " + root.value + " ; " +  toString(root.right);
    }
}


private String toString (Node parent) {

    if (parent == null) {
        return "";
    }
    else {
        return toString(parent.left) + " ; " + parent.value
        + " ; " + toString(parent.right);
    }
}
private String toString (Node parent) {

if (parent == null) {
    return "";
}
else {
    return toString(parent.left) + " ; " + parent.value
    + " ; " +     toString(parent.right);
  }
 }
private Node rotateWithLeftChild (Node k2) {
Node k1 = k2.left;
k2.left = k1.right;
k1.right = k2;

return k1;
}
private Node rotateWithRightChild (Node k1) {
Node k2 = k1.right;
k1.right = k2.left;
k2.left = k1;

return k2;
}
private Node doubleRotateWithRightChild (Node k3) {
k3.left = rotateWithRightChild(k3.left);

return rotateWithLeftChild(k3);
}
private Node doubleRotateWithLeftChild (Node k1) {
k1.right = rotateWithLeftChild(k1.right);
return rotateWithRightChild(k1);
}

}
public class TestTheTree {
public static void main(String[] args) {


    Treee temp = new Treee();

    temp.insert(10);
    temp.insert(20);
    temp.insert(30);


    System.out.println(temp.toString());


  }
  }

1 个答案:

答案 0 :(得分:1)

您正在访问空节点。插入第三个节点(值为30)没有任何问题,但是此时只有正确的子节点存在于树中(您添加了一个值为10的节点,它成为根,然后您添加了一个节点值为20,它将成为根的右子节点,然后添加值为30的节点,该节点将成为节点的右子节点,值为20)。

因此,由于只有正确的子代,当您尝试访问parent.left时,它将为null。因此,当您尝试平衡树时,应添加一些逻辑以确保parent.left和parent.right不为null。我在Tree.java文件中添加了一些代码以帮助您入门。但是,看起来您可能会遇到一些其他问题需要处理,因为在我下面粘贴的代码的第67行中,您尝试访问'parent.right.value&#39;没有任何代码,以确保'parent.right&#39;不是空的。我还留下了用于调试代码的打印语句,以帮助您自己调试它。

public class Tree {
    Node root;

    public Tree() {
        root = null;
    }

    public void insert (int value) {

        if (root == null) {
            root = new Node(null, value);
        }
        else {
            insert(root, value);
        }
    }

    private void insert (Node parent, int value) {

        if (parent.value >= value) {

            if (parent.left == null) {
                parent.left = new Node (parent, value);
            }
            else {
                insert(parent.left, value);

           /////////////////////////    
                if ( parent.left.height() - parent.right.height() == 2) {

                    if (value - parent.left.value < 0) {
                        parent = rotateWithLeftChild(parent);
                    }
                    else {
                        parent = doubleRotateWithRightChild(parent);
                    }
                }           
          /////////////////////////
            }
        }

        else {

            if (parent.right == null) {
                parent.right = new Node (parent, value);
                System.out.println("Node inserted as a right child.");
            }
            else {
                System.out.println("We are in the insert method, before the recursive call");
                insert(parent.right, value);
                System.out.println("We are in the insert method, after the recursive call");
          /////////////////////////
                System.out.println("parent" + parent);
                System.out.println("parent.value " + parent.value);
                System.out.println("parent.left " + parent.left);
                System.out.println("parent.right " + parent.right);
                int leftHeight = 0;
                int rightHeight = 0;
                if( parent.left != null )
                    leftHeight = parent.left.height();

                if( parent.right != null )
                    rightHeight = parent.right.height();

                if ( leftHeight - rightHeight == -2) {

                    if (value - parent.right.value > 0) {
                        parent = rotateWithRightChild(parent);
                    }
                    else {
                        parent = doubleRotateWithLeftChild(parent);
                    }
                }
         /////////////////////////
        }
         }
         }
        public int quantity() {

    if (root == null) {
        return 0;
    }
    else {
        return 1 + quantity(root.left) + quantity(root.right);          
    }
     }
    public int quantity (Node parent) {

    if (parent == null) {
        return 0;
    }
    else {
        return 1 + quantity(parent.left) + quantity(parent.right);
    }
    }
    public int height () {

    int l = 0;
    int r = 0;

    if ( root.left != null) {
        l = height(root.left) + 1;
    }

    if (root.right != null) {
        r = height(root.right) +1;
    }

    return (Math.max(l,r));
    }
    private int height (Node parent) {

    int l = 0;
    int r = 0;

    if ( parent.left != null) {
        l = height(parent.left) + 1;
    }

    if (parent.right != null)   {
        r = height(parent.right) +1;
    }

    return (Math.max(l,r));
    }
    public String toString () {

        if (root == null) {
            return "empty tree";
        }
        else {
            return toString(root.left) + " ; " + root.value + " ; " +  toString(root.right);
        }
    }

    private String toString (Node parent) {

    if (parent == null) {
        return "";
    }
    else {
        return toString(parent.left) + " ; " + parent.value
        + " ; " +     toString(parent.right);
      }
     }
    private Node rotateWithLeftChild (Node k2) {
    Node k1 = k2.left;
    k2.left = k1.right;
    k1.right = k2;

    return k1;
    }
    private Node rotateWithRightChild (Node k1) {
    Node k2 = k1.right;
    k1.right = k2.left;
    k2.left = k1;

    return k2;
    }
    private Node doubleRotateWithRightChild (Node k3) {
    k3.left = rotateWithRightChild(k3.left);

    return rotateWithLeftChild(k3);
    }
    private Node doubleRotateWithLeftChild (Node k1) {
    k1.right = rotateWithLeftChild(k1.right);
    return rotateWithRightChild(k1);
    }

}