AVL 树

AVL 树

        前些天学习委员要收学术论文,让我也交一篇,于是乎我就一个晚上水了一篇,因为当晚截止。
        我的论文是讲平衡树的。因为我们上学期的《数据结构》教材里面的平衡树太丑陋了,丑陋就算了,还搞得很复杂,存心不让人学嘛!!
        我那天晚上写的 AVL 树,代码如下,只是简单测试了下,毕竟时间仓促而我得留出时间做我非常不擅长的事情,写文章。

将AVL树封装为一个C++模板类,方便使用。
具体分析见注释。
抽象类型 T 需具有默认构造函数,可以 = 赋值,可以 < 比较大小。
为方便处理,增加虚拟节点 null,作为空节点。

AVL.cpp
  1#include <iostream>
  2#include <cstdio>
  3
  4using namespace std;
  5
  6template< class T >
  7class AVL
  8{
  9public : 
 10        struct Node
 11        {
 12                Node() {
 13                        h = 0;
 14                        lc = rc = NULL;
 15                }

 16                void upH() {
 17                        int a = ( (lc==NULL) ? (-1) : (lc->h) );
 18                        int b = ( (rc==NULL) ? (-1) : (rc->h) );
 19                        h = ( a < b ? b : a ) + 1;
 20                }

 21                int cmpLR() {
 22                        return ((lc!=NULL)&&(rc!=NULL)) ? ( (lc->h) - (rc->h) ) : 0;
 23                }

 24                T data;
 25                int h;
 26                Node *lc, *rc;
 27        }
;
 28
 29        AVL() {
 30                root = null = new Node();
 31        }

 32        ~AVL() {
 33                clear();
 34                delete null;
 35        }

 36        void clear() {
 37                clear( &root );
 38        }

 39        bool find( const T &d ) {
 40                Node *= root;
 41                while ( p != null ) {
 42                        if ( p->data < d ) {
 43                                p = p->rc;
 44                        }

 45                        else if ( d < p->data ) {
 46                                p = p->lc;
 47                        }

 48                        else {
 49                                return true;
 50                        }

 51                }

 52                return false;
 53        }

 54        bool insert( const T &d ) {
 55                return insert( &root, d );
 56        }

 57        void output() {
 58                output( root );
 59        }

 60private : 
 61        void rotL( Node **pp ) {
 62                Node *tl = (*pp)->lc;
 63                (*pp)->lc = tl->rc;
 64                tl->rc = (*pp);
 65                (*pp) = tl;
 66                tl->rc->upH();
 67                tl->upH();
 68        }

 69        void rotR( Node **pp ) {
 70                Node *tr = (*pp)->rc;
 71                (*pp)->rc = tr->lc;
 72                tr->lc = (*pp);
 73                (*pp) = tr;
 74                tr->lc->upH();
 75                tr->upH();
 76        }

 77        void clear( Node **pp ) {
 78                if ( (*pp) == null ) return;
 79                clear( &((*pp)->lc) );
 80                clear( &((*pp)->rc) );
 81                delete (*pp);
 82                (*pp) = null;
 83        }

 84        bool insert( Node **pp, const T &d ) {
 85                if ( (*pp) == null ) {
 86                        (*pp) = new Node();
 87                        (*pp)->data = d;
 88                        (*pp)->lc = (*pp)->rc = null;
 89                        (*pp)->upH();
 90                        return true;
 91                }

 92                // if ( (*pp)->data < d ) {  // 不允许重复
 93                if ( (*pp)->data <= d ) {    // 允许重复
 94                        if ( insert( &((*pp)->rc), d ) ) {
 95                                (*pp)->upH();
 96                                if ( (*pp)->cmpLR() < -1 ) {
 97                                        if ( d < (*pp)->rc->data ) {
 98                                                rotL( &((*pp)->rc) );
 99                                        }

100                                        rotR( pp );
101                                }

102                                return true;
103                        }

104                }

105                if ( d < (*pp)->data ) {
106                        if ( insert( &((*pp)->lc), d ) ) {
107                                (*pp)->upH();
108                                if ( (*pp)->cmpLR() > 1 ) {
109                                        if ( (*pp)->lc->data < d ) {
110                                                rotR( &((*pp)->lc) );
111                                        }

112                                        rotL( pp );
113                                }

114                                return true;
115                        }

116                }

117                return false// 已经存在
118        }

119        void output( Node *p ) {
120                if ( p == null ) return;
121                output( p->lc );
122                // output p data
123                output( p->rc );
124        }

125
126        Node *root, *null;
127}
;
128
129


 

你可能感兴趣的:(AVL 树)