root of AVL tree java实现

An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. Figures 1-4 illustrate the rotation rules.

    

 

    

Now given a sequence of insertions, you are supposed to tell the root of the resulting AVL tree.

 

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (<=20) which is the total number of keys to be inserted. Then N distinct integer keys are given in the next line. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print ythe root of the resulting AVL tree in one line.

Sample Input 1:
5
88 70 61 96 120
Sample Output 1:
70
Sample Input 2:
7
88 70 61 96 120 90 65
Sample Output 2:
88

import java.util.Scanner;
//声明树的节点类型
class  Node
{
    int key;
    Node left;
    Node right;
    public Node()
    {
        key=-1;
        left=null;
        right=null;
    }
    public Node(int key)
    {
        this.key=key;
        left=null;
        right=null;
    }
}
//声明树类型
class tree
{
    Node root;
    //插入函数
    public    Node insert(Node r,Node node)
    {
        //如果r为空,则把node赋给r
        if (r==null)
        {
            r=new Node();
            r=node;
            return r;
        }
        else
        {
            //如果node的key比r的小,则把node放在r的左边子树上
            if (node.key<r.key)
            {
                r.left=insert(r.left,node);
                //判断加完新节点的树是否是平衡树,因为是加在左边子树上,所以只用判断是r旋转还是lr旋转
                if (getHeight(r.left)-getHeight(r.right)==2)
                {
                    if (node.key<r.left.key)
                    {
                        r=rRotation(r);
                    }
                    else
                    {
                        r=lrRotation(r);
                    }
                }
            }
            //如果node的key比r的大,则把node放在r的右边子树上
            if (node.key>r.key)
            {
                r.right=insert(r.right,node);
                //判断加完新节点的树是否是平衡树,因为是加在右边子树上,所以只用判断是l旋转还是rl旋转
                if (getHeight(r.left)-getHeight(r.right)==-2)
                {
                    if (node.key>r.right.key)
                    {
                        r=lRotation(r);
                    }
                    else
                    {
                        r=rlRotation(r);
                    }
                }
            }
        }
        return r;
    }
    //获取树高函数
    public int getHeight(Node r)
    {
        if (r==null)
        {
            return 0;
        }
            int ldep=getHeight(r.left);
            int rdep=getHeight(r.right);
            
            if (ldep>rdep)
            {
                return ldep+1;
            }
            else
                return rdep+1;    
    }
    //右旋转,返回的B为旋转之后的头结点
    public Node rRotation(Node r)
    {
        Node B=r.left;
        r.left=B.right;
        B.right=r;
        return B;
    }
    //左旋转
    public Node lRotation(Node r)
    {
        Node B=r.right;
        r.right=B.left;
        B.left=r;
        return B;
    }
    //lr旋转,先对r的左子树左旋转,然后对r右旋转,返回新的头节点
    public Node lrRotation(Node r)
    {
        r.left=lRotation(r.left);
        return (rRotation(r));
    }
    //rl旋转
    public Node rlRotation(Node r)
    {
        r.right=rRotation(r.right);
        return (lRotation(r));
    }
}
//测试类
class test
{
    public static void main(String[] args)
    {
        tree t=new tree();
        Scanner sc=new Scanner(System.in);
        int N=sc.nextInt();
        for (int i=0;i<N ;i++ )
        {
            Node node=new Node();
            node.key=sc.nextInt();
            t.root=t.insert(t.root,node);
        }
        System.out.print(t.root.key);1
    }
}

测试结果

 

posted on 2018-05-03 12:24  yfyfyf947  阅读(126)  评论(0编辑  收藏  举报