AVL树的 java 代码实现

代码

public class AvlTree<Type extends Comparable<? super Type>>{
    private static class AvlNode<Type> {
        private Type data;
        private int height;
        private AvlNode<Type> leftChild;
        private AvlNode<Type> rightChild;

        AvlNode ( Type x ) {
            this( x, null, null);
        }

        AvlNode( Type x, AvlNode<Type> l, AvlNode<Type> r ) {
            data = x; leftChild = l; rightChild = r; height = 0;
        }
    }

    //根节点
    private AvlNode<Type> root;

    private void print( AvlNode<Type> r) {
        if ( r != null ) {
            for ( int i = r.height; i > 0; i--) 
                System.out.print(" ");
            System.out.println( r.data.toString() );

            print( r.leftChild );
            print( r.rightChild );
        }
    }

    private int height( AvlNode<Type> t ) {
        return t == null ? -1 : t.height;
    }

    //private AvlNode<Type> remove( Type x, AvlNode<Type> r) {}

    private AvlNode<Type> insert( Type x, AvlNode<Type> r ) {
        //此节点为空,也是数据该插入的地方,返回一个新建节点
        if ( r == null ) return new AvlNode<Type> ( x );

        if ( r.data.compareTo(x) > 0 ) {
            r.leftChild = insert(x, r.leftChild );

            if ( height(r.leftChild) - height(r.rightChild) == 2 )
                if ( x.compareTo(r.leftChild.data) < 0 ) 
                    r = rotateLeftChild( r );
                else 
                    r = doubleLeftChild( r );
        }
        else if ( r.data.compareTo(x) < 0 ) {
            r.rightChild = insert(x, r.rightChild );

            if ( height( r.rightChild ) - height( r.leftChild ) == 2) 
                if ( x.compareTo( r.rightChild.data) > 0 ) 
                    r = rotateRightChild( r );
                else 
                    r = doubleRightChild( r );
        }
        else ;          //do nothing

        r.height = Math.max( height( r.leftChild ), height( r.rightChild ) ) + 1;
        //将插入后的新树赋给旧树
        return r;
    }

    //右旋
    private AvlNode<Type> rotateLeftChild( AvlNode<Type> r ) {
        AvlNode<Type> c = r.leftChild;
        r.leftChild = c.rightChild;
        c.rightChild = r;
        c.height = Math.max( height(c.leftChild), height(c.rightChild)) + 1;
        r.height = Math.max( height(r.leftChild), height(r.rightChild)) + 1;

        return c;
    }


    //左旋
    private AvlNode<Type> rotateRightChild( AvlNode<Type> r ) {
        AvlNode<Type> c = r.rightChild;
        r.rightChild = c.leftChild;
        c.leftChild = r;
        c.height = Math.max( height(c.leftChild), height(c.rightChild)) + 1;
        r.height = Math.max(height(r.leftChild), height(r.rightChild)) + 1;

        return c;
    }

    //左——右双旋
    private AvlNode<Type> doubleLeftChild( AvlNode<Type> r ) {
        r.leftChild = rotateRightChild( r.leftChild );
        return rotateLeftChild(r);
    }

    //右——左双旋
    private AvlNode<Type> doubleRightChild( AvlNode<Type> r ) {
        r.rightChild = rotateLeftChild( r.rightChild );
        return rotateRightChild(r);
    }

    public AvlTree() { root = null; }

    //清空树
    public void makeEmpty() { root = null; }

    //判断树是否为空
    public boolean isEmpty() { return root == null; }

    public void print() throws Exception {
        if ( isEmpty() ) 
            throw new Exception(); 
        print(root);
        System.out.println();
    }

    //删除节点
    /*public void remove( Type x ) throws Exception {}*/

    //插入节点
    public void insert( Type x ) throws Exception { root = insert( x, root ); print(); }

    public static void main( String [] args ) throws Exception {
        AvlTree<Integer> a = new AvlTree<>(); 
        a.insert(1);
        a.insert(2);
        a.insert(0);
        a.insert(3);
        a.insert(4);
        a.insert(5);
        a.insert(6);
        a.insert(7);
        a.insert(8);
        a.insert(9);
        a.insert(10);
    }
}

输出

1

 1
2

 1
0
2

  1
0
 2
3

  1
0
 3
2
4

  3
 1
0
2
 4
5

  3
 1
0
2
 5
4
6

   3
 1
0
2
  5
4
 6
7

   3
 1
0
2
  5
4
 7
6
8

   3
 1
0
2
  7
 5
4
6
 8
9

   3
 1
0
2
  7
 5
4
6
 9
8
10

分析

可以看出AVL树相当的平衡,由于AVL树太复杂(我很菜),所以删除操作以后再更。

    原文作者:AVL树
    原文地址: https://blog.csdn.net/one_of_a_kind/article/details/75208486
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞