AVL Tree for Java

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-15 03:51:06

问题


I'm not sure if I'm doing this correctly as this is my very first time coding with nodes. But here's my code so far, if someone can look over it and help me out with understanding if I'm doing anything wrong. Also my insert/remove methods are giving me a hard time. The professor gave us the pseudocode for that but I can't seem to grasp how to interpret it into Java code as I've never done this type of code before. Mostly because there's a if statement that checks the heights to see if the tree is balanced, how would i implement height in this? Hints or any help is greatly appreciated, I've been stuck on this for a while. Thanks!

I also don't think I did my constructor correctly and I'm unsure about it. The return in insert/remove can be ignored, it was just put in there to make sure the rest of the code will compile.

public class AvlNode{

    public static void main(String[]args){

    }
    //constructor
    public class AvlTreeNode{
        private int num;
        private AvlTreeNode left;
        private AvlTreeNode right;

        public AvlTreeNode left(){
            return this.left;
        }

        public AvlTreeNode right(){
            return this.right;
        }

        public int value(){
            return this.num;
        }
    }
    //method to find the number specified on the node
    public AvlTreeNode find(AvlTreeNode t, int x){
        if(t == null){
            return null;
        }
        if( t.value() == x){
            return t;
        }
        else if(x < t.value()){
            return find(t.left(), x);
        }
        else{
            return find(t.right(), x);
        }
    }
    //method to insert a new node and number to a tree
    public AvlTreeNode insert(AvlTreeNode t, int x){
        if(t == null){
            t = new AvlTreeNode(x, null, null);
            return t;
        }
        if(x < t.value()){
            t.left = insert(t.left(), x);
        }
        return t;
    }
    //method to remove a node and number from the tree
    public AvlTreeNode remove(AvlTreeNode t, int x){
        return t;
    }
    //Inorder traversal method, should print out numbers in ascending order if correct
    public void inOrder(AvlTreeNode t){
        if(t != null){
            inOrder(t.left());
            System.out.print(t.value() + " ");
            inOrder(t.right());
        }
    }
    //single rotation of nodes to balance tree, rotating leftwards
    public static AvlTreeNode singleRotateWithLeft( AvlTreeNode k1){
        AvlTreeNode k2 = k1.left;
        k1.left = k2.right;
        k2.right = k1;
        return k2;
    }
    //single rotation of nodes to balance tree, rotating rightwards
    public static AvlTreeNode singleRotateWithRight( AvlTreeNode k2){
        AvlTreeNode k1 = k2.right;
        k2.right = k1.left;
        k1.left = k2;
        return k1;
    }
    //double rotation of nodes towards the left
    public static AvlTreeNode doubleRotateWithLeft( AvlTreeNode k3){
        k3.left = doubleRotateWithRight(k3.left);
        return doubleRotateWithLeft(k3);
    }
    //double rotation of nodes towards the right
    public static AvlTreeNode doubleRotateWithRight( AvlTreeNode k2){
        k2.right = doubleRotateWithLeft(k2.right);
        return doubleRotateWithRight(k2);
    }
}

回答1:


Regarding the constructor: I think the thing that is wrong, is that you mistake the inner class you use to describe a AvlTreeNode for a constructor. In all probability, you don't need to write an explicit constructor, because the default (empty) one will do for you.

The construction of a Tree can be viewed as the insertion of all its nodes to an empty tree.

Regarding the height, you should probably view the height of a tree as a property of each AvlTreeNode (so, next to num you need a height variable). The next thing will be to implement insert and remove so that the correct local transformations / rotations are used and so that the heights of the inserted node and its children are increased or decreased appropriately.

edit: I now see that your code uses a constructor with three arguments. You can use a constructor like the one in this example code.

//inner class for the node
public class AvlTreeNode{
    private int num;
    private int height;

    private AvlTreeNode left;
    private AvlTreeNode right;

    //this is the constructor!
    public AvlTreeNode(int value, AvlTreeNode left, AvlTreeNode right){
        this.num = value;
        this.left = left;
        this.right = right;
        this.height = 1;

        if (left != null && left.height() >= height){
            height = left.height() + 1;
        }
        if (right != null && right.height() >= height){
            height = right.height() + 1;
        }
    }

    public AvlTreeNode left(){
        return this.left;
    }

    public AvlTreeNode right(){
        return this.right;
    }

    public int value(){
        return this.num;
    }

    public int height(){
        return height;
    }
}


来源:https://stackoverflow.com/questions/19481815/avl-tree-for-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!