skywang12345

导航

 

 

概要

上一章通过C语言实现了AVL树,本章将介绍AVL树的C++版本,算法与C语言版本的一样。

目录

1. AVL树的介绍
2. AVL树的C++实现
3. AVL树的C++测试程序

转载请注明出处:http://www.cnblogs.com/skywang12345/p/3577360.html


更多内容数据结构与算法系列 目录 

(01) AVL树(一)之 图文解析 和 C语言的实现
(02) AVL树(二)之 C++的实现
(03) AVL树(三)之 Java的实现

 

AVL树的介绍

AVL树是高度平衡的而二叉树。它的特点是:AVL树中任何节点的两个子树的高度最大差别为1。 

上面的两张图片,左边的是AVL树,它的任何节点的两个子树的高度差别都<=1;而右边的不是AVL树,因为7的两颗子树的高度相差为2(以2为根节点的树的高度是3,而以8为根节点的树的高度是1)。

 

AVL树的C++实现

1. 节点

1.1 AVL树节点

template <class T>
class AVLTreeNode{
    public:
        T key;                // 关键字(键值)
        int height;         // 高度
        AVLTreeNode *left;    // 左孩子
        AVLTreeNode *right;    // 右孩子

        AVLTreeNode(T value, AVLTreeNode *l, AVLTreeNode *r):
            key(value), height(0),left(l),right(r) {}
};

AVLTreeNode是AVL树的节点类,它包括的几个组成对象:
(01) key -- 是关键字,是用来对AVL树的节点进行排序的。
(02) left -- 是左孩子。
(03) right -- 是右孩子。
(04) height -- 是高度。

 

1.2 AVL树

template <class T>
class AVLTree {
    private:
        AVLTreeNode<T> *mRoot;    // 根结点

    public:
        AVLTree();
        ~AVLTree();

        // 获取树的高度
        int height();
        // 获取树的高度
        int max(int a, int b);

        // 前序遍历"AVL树"
        void preOrder();
        // 中序遍历"AVL树"
        void inOrder();
        // 后序遍历"AVL树"
        void postOrder();

        // (递归实现)查找"AVL树"中键值为key的节点
        AVLTreeNode<T>* search(T key);
        // (非递归实现)查找"AVL树"中键值为key的节点
        AVLTreeNode<T>* iterativeSearch(T key);

        // 查找最小结点:返回最小结点的键值。
        T minimum();
        // 查找最大结点:返回最大结点的键值。
        T maximum();

        // 将结点(key为节点键值)插入到AVL树中
        void insert(T key);

        // 删除结点(key为节点键值)
        void remove(T key);

        // 销毁AVL树
        void destroy();

        // 打印AVL树
        void print();
    private:
        // 获取树的高度
        int height(AVLTreeNode<T>* tree) ;

        // 前序遍历"AVL树"
        void preOrder(AVLTreeNode<T>* tree) const;
        // 中序遍历"AVL树"
        void inOrder(AVLTreeNode<T>* tree) const;
        // 后序遍历"AVL树"
        void postOrder(AVLTreeNode<T>* tree) const;

        // (递归实现)查找"AVL树x"中键值为key的节点
        AVLTreeNode<T>* search(AVLTreeNode<T>* x, T key) const;
        // (非递归实现)查找"AVL树x"中键值为key的节点
        AVLTreeNode<T>* iterativeSearch(AVLTreeNode<T>* x, T key) const;

        // 查找最小结点:返回tree为根结点的AVL树的最小结点。
        AVLTreeNode<T>* minimum(AVLTreeNode<T>* tree);
        // 查找最大结点:返回tree为根结点的AVL树的最大结点。
        AVLTreeNode<T>* maximum(AVLTreeNode<T>* tree);

        // LL:左左对应的情况(左单旋转)。
        AVLTreeNode<T>* leftLeftRotation(AVLTreeNode<T>* k2);

        // RR:右右对应的情况(右单旋转)。
        AVLTreeNode<T>* rightRightRotation(AVLTreeNode<T>* k1);

        // LR:左右对应的情况(左双旋转)。
        AVLTreeNode<T>* leftRightRotation(AVLTreeNode<T>* k3);

        // RL:右左对应的情况(右双旋转)。
        AVLTreeNode<T>* rightLeftRotation(AVLTreeNode<T>* k1);

        // 将结点(z)插入到AVL树(tree)中
        AVLTreeNode<T>* insert(AVLTreeNode<T>* &tree, T key);

        // 删除AVL树(tree)中的结点(z),并返回被删除的结点
        AVLTreeNode<T>* remove(AVLTreeNode<T>* &tree, AVLTreeNode<T>* z);

        // 销毁AVL树
        void destroy(AVLTreeNode<T>* &tree);

        // 打印AVL树
        void print(AVLTreeNode<T>* tree, T key, int direction);
};

AVLTree是AVL树对应的类。它包含AVL树的根节点mRoot和AVL树的基本操作接口。需要说明的是:AVLTree中重载了许多函数。重载的目的是区分内部接口和外部接口,例如insert()函数而言,insert(tree, key)是内部接口,而insert(key)是外部接口。

 

1.2 树的高度

/*
 * 获取树的高度
 */
template <class T>
int AVLTree<T>::height(AVLTreeNode<T>* tree) 
{
    if (tree != NULL)
        return tree->height;

    return 0;
}

template <class T>
int AVLTree<T>::height() 
{
    return height(mRoot);
}

关于高度,有的地方将"空二叉树的高度是-1",而本文采用维基百科上的定义:树的高度为最大层次。即空的二叉树的高度是0,非空树的高度等于它的最大层次(根的层次为1,根的子节点为第2层,依次类推)。

 

1.3 比较大小

/*
 * 比较两个值的大小
 */
template <class T>
int AVLTree<T>::max(int a, int b) 
{
    return a>b ? a : b;
}

 

2. 旋转

如果在AVL树中进行插入或删除节点后,可能导致AVL树失去平衡。这种失去平衡的可以概括为4种姿态:LL(左左),LR(左右),RR(右右)和RL(右左)。下面给出它们的示意图:

上图中的4棵树都是"失去平衡的AVL树",从左往右的情况依次是:LL、LR、RL、RR。除了上面的情况之外,还有其它的失去平衡的AVL树,如下图:

上面的两张图都是为了便于理解,而列举的关于"失去平衡的AVL树"的例子。总的来说,AVL树失去平衡时的情况一定是LL、LR、RL、RR这4种之一,它们都由各自的定义:

(1) LL:LeftLeft,也称为"左左"。插入或删除一个节点后,根节点的左子树的左子树还有非空子节点,导致"根的左子树的高度"比"根的右子树的高度"大2,导致AVL树失去了平衡。
     例如,在上面LL情况中,由于"根节点(8)的左子树(4)的左子树(2)还有非空子节点",而"根节点(8)的右子树(12)没有子节点";导致"根节点(8)的左子树(4)高度"比"根节点(8)的右子树(12)"高2。

 

(2) LR:LeftRight,也称为"左右"。插入或删除一个节点后,根节点的左子树的右子树还有非空子节点,导致"根的左子树的高度"比"根的右子树的高度"大2,导致AVL树失去了平衡。
     例如,在上面LR情况中,由于"根节点(8)的左子树(4)的左子树(6)还有非空子节点",而"根节点(8)的右子树(12)没有子节点";导致"根节点(8)的左子树(4)高度"比"根节点(8)的右子树(12)"高2。

 

(3) RL:RightLeft,称为"右左"。插入或删除一个节点后,根节点的右子树的左子树还有非空子节点,导致"根的右子树的高度"比"根的左子树的高度"大2,导致AVL树失去了平衡。
     例如,在上面RL情况中,由于"根节点(8)的右子树(12)的左子树(10)还有非空子节点",而"根节点(8)的左子树(4)没有子节点";导致"根节点(8)的右子树(12)高度"比"根节点(8)的左子树(4)"高2。

 

(4) RR:RightRight,称为"右右"。插入或删除一个节点后,根节点的右子树的右子树还有非空子节点,导致"根的右子树的高度"比"根的左子树的高度"大2,导致AVL树失去了平衡。
     例如,在上面RR情况中,由于"根节点(8)的右子树(12)的右子树(14)还有非空子节点",而"根节点(8)的左子树(4)没有子节点";导致"根节点(8)的右子树(12)高度"比"根节点(8)的左子树(4)"高2。

 

前面说过,如果在AVL树中进行插入或删除节点后,可能导致AVL树失去平衡。AVL失去平衡之后,可以通过旋转使其恢复平衡,下面分别介绍"LL(左左),LR(左右),RR(右右)和RL(右左)"这4种情况对应的旋转方法。

 

2.1 LL的旋转

LL失去平衡的情况,可以通过一次旋转让AVL树恢复平衡。如下图:

图中左边是旋转之前的树,右边是旋转之后的树。从中可以发现,旋转之后的树又变成了AVL树,而且该旋转只需要一次即可完成。
对于LL旋转,你可以这样理解为:LL旋转是围绕"失去平衡的AVL根节点"进行的,也就是节点k2;而且由于是LL情况,即左左情况,就用手抓着"左孩子,即k1"使劲摇。将k1变成根节点,k2变成k1的右子树,"k1的右子树"变成"k2的左子树"。

 

LL的旋转代码

/*
 * LL:左左对应的情况(左单旋转)。
 *
 * 返回值:旋转后的根节点
 */
template <class T>
AVLTreeNode<T>* AVLTree<T>::leftLeftRotation(AVLTreeNode<T>* k2)
{
    AVLTreeNode<T>* k1;

    k1 = k2->left;
    k2->left = k1->right;
    k1->right = k2;

    k2->height = max( height(k2->left), height(k2->right)) + 1;
    k1->height = max( height(k1->left), k2->height) + 1;

    return k1;
}

 

2.2 RR的旋转

理解了LL之后,RR就相当容易理解了。RR是与LL对称的情况!RR恢复平衡的旋转方法如下:

图中左边是旋转之前的树,右边是旋转之后的树。RR旋转也只需要一次即可完成。

 

RR的旋转代码

/*
 * RR:右右对应的情况(右单旋转)。
 *
 * 返回值:旋转后的根节点
 */
template <class T>
AVLTreeNode<T>* AVLTree<T>::rightRightRotation(AVLTreeNode<T>* k1)
{
    AVLTreeNode<T>* k2;

    k2 = k1->right;
    k1->right = k2->left;
    k2->left = k1;

    k1->height = max( height(k1->left), height(k1->right)) + 1;
    k2->height = max( height(k2->right), k1->height) + 1;

    return k2;
}

 

2.3 LR的旋转

LR失去平衡的情况,需要经过两次旋转才能让AVL树恢复平衡。如下图:

第一次旋转是围绕"k1"进行的"RR旋转",第二次是围绕"k3"进行的"LL旋转"。

 

LR的旋转代码

/*
 * LR:左右对应的情况(左双旋转)。
 *
 * 返回值:旋转后的根节点
 */
template <class T>
AVLTreeNode<T>* AVLTree<T>::leftRightRotation(AVLTreeNode<T>* k3)
{
    k3->left = rightRightRotation(k3->left);

    return leftLeftRotation(k3);
}

 

2.4 RL的旋转

RL是与LR的对称情况!RL恢复平衡的旋转方法如下:

第一次旋转是围绕"k3"进行的"LL旋转",第二次是围绕"k1"进行的"RR旋转"。


RL的旋转代码

/*
 * RL:右左对应的情况(右双旋转)。
 *
 * 返回值:旋转后的根节点
 */
template <class T>
AVLTreeNode<T>* AVLTree<T>::rightLeftRotation(AVLTreeNode<T>* k1)
{
    k1->right = leftLeftRotation(k1->right);

    return rightRightRotation(k1);
}

 

3. 插入

插入节点的代码

/* 
 * 将结点插入到AVL树中,并返回根节点
 *
 * 参数说明:
 *     tree AVL树的根结点
 *     key 插入的结点的键值
 * 返回值:
 *     根节点
 */
template <class T>
AVLTreeNode<T>* AVLTree<T>::insert(AVLTreeNode<T>* &tree, T key)
{
    if (tree == NULL) 
    {
        // 新建节点
        tree = new AVLTreeNode<T>(key, NULL, NULL);
        if (tree==NULL)
        {
            cout << "ERROR: create avltree node failed!" << endl;
            return NULL;
        }
    }
    else if (key < tree->key) // 应该将key插入到"tree的左子树"的情况
    {
        tree->left = insert(tree->left, key);
        // 插入节点后,若AVL树失去平衡,则进行相应的调节。
        if (height(tree->left) - height(tree->right) == 2)
        {
            if (key < tree->left->key)
                tree = leftLeftRotation(tree);
            else
                tree = leftRightRotation(tree);
        }
    }
    else if (key > tree->key) // 应该将key插入到"tree的右子树"的情况
    {
        tree->right = insert(tree->right, key);
        // 插入节点后,若AVL树失去平衡,则进行相应的调节。
        if (height(tree->right) - height(tree->left) == 2)
        {
            if (key > tree->right->key)
                tree = rightRightRotation(tree);
            else
                tree = rightLeftRotation(tree);
        }
    }
    else //key == tree->key)
    {
        cout << "添加失败:不允许添加相同的节点!" << endl;
    }

    tree->height = max( height(tree->left), height(tree->right)) + 1;

    return tree;
}

template <class T>
void AVLTree<T>::insert(T key)
{
    insert(mRoot, key);
}

 

4. 删除

删除节点的代码

/* 
 * 删除结点(z),返回根节点
 *
 * 参数说明:
 *     tree AVL树的根结点
 *     z 待删除的结点
 * 返回值:
 *     根节点
 */
template <class T>
AVLTreeNode<T>* AVLTree<T>::remove(AVLTreeNode<T>* &tree, AVLTreeNode<T>* z)
{
    // 根为空 或者 没有要删除的节点,直接返回NULL。
    if (tree==NULL || z==NULL)
        return NULL;

    if (z->key < tree->key)        // 待删除的节点在"tree的左子树"中
    {
        tree->left = remove(tree->left, z);
        // 删除节点后,若AVL树失去平衡,则进行相应的调节。
        if (height(tree->right) - height(tree->left) == 2)
        {
            AVLTreeNode<T> *r =  tree->right;
            if (height(r->left) > height(r->right))
                tree = rightLeftRotation(tree);
            else
                tree = rightRightRotation(tree);
        }
    }
    else if (z->key > tree->key)// 待删除的节点在"tree的右子树"中
    {
        tree->right = remove(tree->right, z);
        // 删除节点后,若AVL树失去平衡,则进行相应的调节。
        if (height(tree->left) - height(tree->right) == 2)
        {
            AVLTreeNode<T> *l =  tree->left;
            if (height(l->right) > height(l->left))
                tree = leftRightRotation(tree);
            else
                tree = leftLeftRotation(tree);
        }
    }
    else    // tree是对应要删除的节点。
    {
        // tree的左右孩子都非空
        if ((tree->left!=NULL) && (tree->right!=NULL))
        {
            if (height(tree->left) > height(tree->right))
            {
                // 如果tree的左子树比右子树高;
                // 则(01)找出tree的左子树中的最大节点
                //   (02)将该最大节点的值赋值给tree。
                //   (03)删除该最大节点。
                // 这类似于用"tree的左子树中最大节点"做"tree"的替身;
                // 采用这种方式的好处是:删除"tree的左子树中最大节点"之后,AVL树仍然是平衡的。
                AVLTreeNode<T>* max = maximum(tree->left);
                tree->key = max->key;
                tree->left = remove(tree->left, max);
            }
            else
            {
                // 如果tree的左子树不比右子树高(即它们相等,或右子树比左子树高1)
                // 则(01)找出tree的右子树中的最小节点
                //   (02)将该最小节点的值赋值给tree。
                //   (03)删除该最小节点。
                // 这类似于用"tree的右子树中最小节点"做"tree"的替身;
                // 采用这种方式的好处是:删除"tree的右子树中最小节点"之后,AVL树仍然是平衡的。
                AVLTreeNode<T>* min = maximum(tree->right);
                tree->key = min->key;
                tree->right = remove(tree->right, min);
            }
        }
        else
        {
            AVLTreeNode<T>* tmp = tree;
            tree = (tree->left!=NULL) ? tree->left : tree->right;
            delete tmp;
        }
    }

    return tree;
}

template <class T>
void AVLTree<T>::remove(T key)
{
    AVLTreeNode<T>* z; 

    if ((z = search(mRoot, key)) != NULL)
        mRoot = remove(mRoot, z);
}

 

注意关于AVL树的"前序遍历"、"中序遍历"、"后序遍历"、"最大值"、"最小值"、"查找"、"打印"、"销毁"等接口与"二叉查找树"基本一样,这些操作在"二叉查找树"中已经介绍过了,这里就不再单独介绍了。当然,后文给出的AVL树的完整源码中,有给出这些API的实现代码。这些接口很简单,Please RTFSC(Read The Fucking Source Code)!

 

完整的实现代码

AVL树的实现文件(AVRTree.h)

  1 #ifndef _AVL_TREE_HPP_
  2 #define _AVL_TREE_HPP_
  3 
  4 #include <iomanip>
  5 #include <iostream>
  6 using namespace std;
  7 
  8 template <class T>
  9 class AVLTreeNode{
 10     public:
 11         T key;                // 关键字(键值)
 12         int height;         // 高度
 13         AVLTreeNode *left;    // 左孩子
 14         AVLTreeNode *right;    // 右孩子
 15 
 16         AVLTreeNode(T value, AVLTreeNode *l, AVLTreeNode *r):
 17             key(value), height(0),left(l),right(r) {}
 18 };
 19 
 20 template <class T>
 21 class AVLTree {
 22     private:
 23         AVLTreeNode<T> *mRoot;    // 根结点
 24 
 25     public:
 26         AVLTree();
 27         ~AVLTree();
 28 
 29         // 获取树的高度
 30         int height();
 31         // 获取树的高度
 32         int max(int a, int b);
 33 
 34         // 前序遍历"AVL树"
 35         void preOrder();
 36         // 中序遍历"AVL树"
 37         void inOrder();
 38         // 后序遍历"AVL树"
 39         void postOrder();
 40 
 41         // (递归实现)查找"AVL树"中键值为key的节点
 42         AVLTreeNode<T>* search(T key);
 43         // (非递归实现)查找"AVL树"中键值为key的节点
 44         AVLTreeNode<T>* iterativeSearch(T key);
 45 
 46         // 查找最小结点:返回最小结点的键值。
 47         T minimum();
 48         // 查找最大结点:返回最大结点的键值。
 49         T maximum();
 50 
 51         // 将结点(key为节点键值)插入到AVL树中
 52         void insert(T key);
 53 
 54         // 删除结点(key为节点键值)
 55         void remove(T key);
 56 
 57         // 销毁AVL树
 58         void destroy();
 59 
 60         // 打印AVL树
 61         void print();
 62     private:
 63         // 获取树的高度
 64         int height(AVLTreeNode<T>* tree) ;
 65 
 66         // 前序遍历"AVL树"
 67         void preOrder(AVLTreeNode<T>* tree) const;
 68         // 中序遍历"AVL树"
 69         void inOrder(AVLTreeNode<T>* tree) const;
 70         // 后序遍历"AVL树"
 71         void postOrder(AVLTreeNode<T>* tree) const;
 72 
 73         // (递归实现)查找"AVL树x"中键值为key的节点
 74         AVLTreeNode<T>* search(AVLTreeNode<T>* x, T key) const;
 75         // (非递归实现)查找"AVL树x"中键值为key的节点
 76         AVLTreeNode<T>* iterativeSearch(AVLTreeNode<T>* x, T key) const;
 77 
 78         // 查找最小结点:返回tree为根结点的AVL树的最小结点。
 79         AVLTreeNode<T>* minimum(AVLTreeNode<T>* tree);
 80         // 查找最大结点:返回tree为根结点的AVL树的最大结点。
 81         AVLTreeNode<T>* maximum(AVLTreeNode<T>* tree);
 82 
 83         // LL:左左对应的情况(左单旋转)。
 84         AVLTreeNode<T>* leftLeftRotation(AVLTreeNode<T>* k2);
 85 
 86         // RR:右右对应的情况(右单旋转)。
 87         AVLTreeNode<T>* rightRightRotation(AVLTreeNode<T>* k1);
 88 
 89         // LR:左右对应的情况(左双旋转)。
 90         AVLTreeNode<T>* leftRightRotation(AVLTreeNode<T>* k3);
 91 
 92         // RL:右左对应的情况(右双旋转)。
 93         AVLTreeNode<T>* rightLeftRotation(AVLTreeNode<T>* k1);
 94 
 95         // 将结点(z)插入到AVL树(tree)中
 96         AVLTreeNode<T>* insert(AVLTreeNode<T>* &tree, T key);
 97 
 98         // 删除AVL树(tree)中的结点(z),并返回被删除的结点
 99         AVLTreeNode<T>* remove(AVLTreeNode<T>* &tree, AVLTreeNode<T>* z);
100 
101         // 销毁AVL树
102         void destroy(AVLTreeNode<T>* &tree);
103 
104         // 打印AVL树
105         void print(AVLTreeNode<T>* tree, T key, int direction);
106 };
107 
108 /* 
109  * 构造函数
110  */
111 template <class T>
112 AVLTree<T>::AVLTree():mRoot(NULL)
113 {
114 }
115 
116 /* 
117  * 析构函数
118  */
119 template <class T>
120 AVLTree<T>::~AVLTree() 
121 {
122     destroy(mRoot);
123 }
124 
125 /*
126  * 获取树的高度
127  */
128 template <class T>
129 int AVLTree<T>::height(AVLTreeNode<T>* tree) 
130 {
131     if (tree != NULL)
132         return tree->height;
133 
134     return 0;
135 }
136 
137 template <class T>
138 int AVLTree<T>::height() 
139 {
140     return height(mRoot);
141 }
142 /*
143  * 比较两个值的大小
144  */
145 template <class T>
146 int AVLTree<T>::max(int a, int b) 
147 {
148     return a>b ? a : b;
149 }
150 
151 /*
152  * 前序遍历"AVL树"
153  */
154 template <class T>
155 void AVLTree<T>::preOrder(AVLTreeNode<T>* tree) const
156 {
157     if(tree != NULL)
158     {
159         cout<< tree->key << " " ;
160         preOrder(tree->left);
161         preOrder(tree->right);
162     }
163 }
164 
165 template <class T>
166 void AVLTree<T>::preOrder() 
167 {
168     preOrder(mRoot);
169 }
170 
171 /*
172  * 中序遍历"AVL树"
173  */
174 template <class T>
175 void AVLTree<T>::inOrder(AVLTreeNode<T>* tree) const
176 {
177     if(tree != NULL)
178     {
179         inOrder(tree->left);
180         cout<< tree->key << " " ;
181         inOrder(tree->right);
182     }
183 }
184 
185 template <class T>
186 void AVLTree<T>::inOrder() 
187 {
188     inOrder(mRoot);
189 }
190 
191 /*
192  * 后序遍历"AVL树"
193  */
194 template <class T>
195 void AVLTree<T>::postOrder(AVLTreeNode<T>* tree) const
196 {
197     if(tree != NULL)
198     {
199         postOrder(tree->left);
200         postOrder(tree->right);
201         cout<< tree->key << " " ;
202     }
203 }
204 
205 template <class T>
206 void AVLTree<T>::postOrder() 
207 {
208     postOrder(mRoot);
209 }
210 
211 /*
212  * (递归实现)查找"AVL树x"中键值为key的节点
213  */
214 template <class T>
215 AVLTreeNode<T>* AVLTree<T>::search(AVLTreeNode<T>* x, T key) const
216 {
217     if (x==NULL || x->key==key)
218         return x;
219 
220     if (key < x->key)
221         return search(x->left, key);
222     else
223         return search(x->right, key);
224 }
225 
226 template <class T>
227 AVLTreeNode<T>* AVLTree<T>::search(T key) 
228 {
229     return search(mRoot, key);
230 }
231 
232 /*
233  * (非递归实现)查找"AVL树x"中键值为key的节点
234  */
235 template <class T>
236 AVLTreeNode<T>* AVLTree<T>::iterativeSearch(AVLTreeNode<T>* x, T key) const
237 {
238     while ((x!=NULL) && (x->key!=key))
239     {
240         if (key < x->key)
241             x = x->left;
242         else
243             x = x->right;
244     }
245 
246     return x;
247 }
248 
249 template <class T>
250 AVLTreeNode<T>* AVLTree<T>::iterativeSearch(T key)
251 {
252     return iterativeSearch(mRoot, key);
253 }
254 
255 /* 
256  * 查找最小结点:返回tree为根结点的AVL树的最小结点。
257  */
258 template <class T>
259 AVLTreeNode<T>* AVLTree<T>::minimum(AVLTreeNode<T>* tree)
260 {
261     if (tree == NULL)
262         return NULL;
263 
264     while(tree->left != NULL)
265         tree = tree->left;
266     return tree;
267 }
268 
269 template <class T>
270 T AVLTree<T>::minimum()
271 {
272     AVLTreeNode<T> *p = minimum(mRoot);
273     if (p != NULL)
274         return p->key;
275 
276     return (T)NULL;
277 }
278  
279 /* 
280  * 查找最大结点:返回tree为根结点的AVL树的最大结点。
281  */
282 template <class T>
283 AVLTreeNode<T>* AVLTree<T>::maximum(AVLTreeNode<T>* tree)
284 {
285     if (tree == NULL)
286         return NULL;
287 
288     while(tree->right != NULL)
289         tree = tree->right;
290     return tree;
291 }
292 
293 template <class T>
294 T AVLTree<T>::maximum()
295 {
296     AVLTreeNode<T> *p = maximum(mRoot);
297     if (p != NULL)
298         return p->key;
299 
300     return (T)NULL;
301 }
302 
303 /*
304  * LL:左左对应的情况(左单旋转)。
305  *
306  * 返回值:旋转后的根节点
307  */
308 template <class T>
309 AVLTreeNode<T>* AVLTree<T>::leftLeftRotation(AVLTreeNode<T>* k2)
310 {
311     AVLTreeNode<T>* k1;
312 
313     k1 = k2->left;
314     k2->left = k1->right;
315     k1->right = k2;
316 
317     k2->height = max( height(k2->left), height(k2->right)) + 1;
318     k1->height = max( height(k1->left), k2->height) + 1;
319 
320     return k1;
321 }
322 
323 /*
324  * RR:右右对应的情况(右单旋转)。
325  *
326  * 返回值:旋转后的根节点
327  */
328 template <class T>
329 AVLTreeNode<T>* AVLTree<T>::rightRightRotation(AVLTreeNode<T>* k1)
330 {
331     AVLTreeNode<T>* k2;
332 
333     k2 = k1->right;
334     k1->right = k2->left;
335     k2->left = k1;
336 
337     k1->height = max( height(k1->left), height(k1->right)) + 1;
338     k2->height = max( height(k2->right), k1->height) + 1;
339 
340     return k2;
341 }
342 
343 /*
344  * LR:左右对应的情况(左双旋转)。
345  *
346  * 返回值:旋转后的根节点
347  */
348 template <class T>
349 AVLTreeNode<T>* AVLTree<T>::leftRightRotation(AVLTreeNode<T>* k3)
350 {
351     k3->left = rightRightRotation(k3->left);
352 
353     return leftLeftRotation(k3);
354 }
355 
356 /*
357  * RL:右左对应的情况(右双旋转)。
358  *
359  * 返回值:旋转后的根节点
360  */
361 template <class T>
362 AVLTreeNode<T>* AVLTree<T>::rightLeftRotation(AVLTreeNode<T>* k1)
363 {
364     k1->right = leftLeftRotation(k1->right);
365 
366     return rightRightRotation(k1);
367 }
368 
369 /* 
370  * 将结点插入到AVL树中,并返回根节点
371  *
372  * 参数说明:
373  *     tree AVL树的根结点
374  *     key 插入的结点的键值
375  * 返回值:
376  *     根节点
377  */
378 template <class T>
379 AVLTreeNode<T>* AVLTree<T>::insert(AVLTreeNode<T>* &tree, T key)
380 {
381     if (tree == NULL) 
382     {
383         // 新建节点
384         tree = new AVLTreeNode<T>(key, NULL, NULL);
385         if (tree==NULL)
386         {
387             cout << "ERROR: create avltree node failed!" << endl;
388             return NULL;
389         }
390     }
391     else if (key < tree->key) // 应该将key插入到"tree的左子树"的情况
392     {
393         tree->left = insert(tree->left, key);
394         // 插入节点后,若AVL树失去平衡,则进行相应的调节。
395         if (height(tree->left) - height(tree->right) == 2)
396         {
397             if (key < tree->left->key)
398                 tree = leftLeftRotation(tree);
399             else
400                 tree = leftRightRotation(tree);
401         }
402     }
403     else if (key > tree->key) // 应该将key插入到"tree的右子树"的情况
404     {
405         tree->right = insert(tree->right, key);
406         // 插入节点后,若AVL树失去平衡,则进行相应的调节。
407         if (height(tree->right) - height(tree->left) == 2)
408         {
409             if (key > tree->right->key)
410                 tree = rightRightRotation(tree);
411             else
412                 tree = rightLeftRotation(tree);
413         }
414     }
415     else //key == tree->key)
416     {
417         cout << "添加失败:不允许添加相同的节点!" << endl;
418     }
419 
420     tree->height = max( height(tree->left), height(tree->right)) + 1;
421 
422     return tree;
423 }
424 
425 template <class T>
426 void AVLTree<T>::insert(T key)
427 {
428     insert(mRoot, key);
429 }
430 
431 /* 
432  * 删除结点(z),返回根节点
433  *
434  * 参数说明:
435  *     tree AVL树的根结点
436  *     z 待删除的结点
437  * 返回值:
438  *     根节点
439  */
440 template <class T>
441 AVLTreeNode<T>* AVLTree<T>::remove(AVLTreeNode<T>* &tree, AVLTreeNode<T>* z)
442 {
443     // 根为空 或者 没有要删除的节点,直接返回NULL。
444     if (tree==NULL || z==NULL)
445         return NULL;
446 
447     if (z->key < tree->key)        // 待删除的节点在"tree的左子树"中
448     {
449         tree->left = remove(tree->left, z);
450         // 删除节点后,若AVL树失去平衡,则进行相应的调节。
451         if (height(tree->right) - height(tree->left) == 2)
452         {
453             AVLTreeNode<T> *r =  tree->right;
454             if (height(r->left) > height(r->right))
455                 tree = rightLeftRotation(tree);
456             else
457                 tree = rightRightRotation(tree);
458         }
459     }
460     else if (z->key > tree->key)// 待删除的节点在"tree的右子树"中
461     {
462         tree->right = remove(tree->right, z);
463         // 删除节点后,若AVL树失去平衡,则进行相应的调节。
464         if (height(tree->left) - height(tree->right) == 2)
465         {
466             AVLTreeNode<T> *l =  tree->left;
467             if (height(l->right) > height(l->left))
468                 tree = leftRightRotation(tree);
469             else
470                 tree = leftLeftRotation(tree);
471         }
472     }
473     else    // tree是对应要删除的节点。
474     {
475         // tree的左右孩子都非空
476         if ((tree->left!=NULL) && (tree->right!=NULL))
477         {
478             if (height(tree->left) > height(tree->right))
479             {
480                 // 如果tree的左子树比右子树高;
481                 // 则(01)找出tree的左子树中的最大节点
482                 //   (02)将该最大节点的值赋值给tree。
483                 //   (03)删除该最大节点。
484                 // 这类似于用"tree的左子树中最大节点"做"tree"的替身;
485                 // 采用这种方式的好处是:删除"tree的左子树中最大节点"之后,AVL树仍然是平衡的。
486                 AVLTreeNode<T>* max = maximum(tree->left);
487                 tree->key = max->key;
488                 tree->left = remove(tree->left, max);
489             }
490             else
491             {
492                 // 如果tree的左子树不比右子树高(即它们相等,或右子树比左子树高1)
493                 // 则(01)找出tree的右子树中的最小节点
494                 //   (02)将该最小节点的值赋值给tree。
495                 //   (03)删除该最小节点。
496                 // 这类似于用"tree的右子树中最小节点"做"tree"的替身;
497                 // 采用这种方式的好处是:删除"tree的右子树中最小节点"之后,AVL树仍然是平衡的。
498                 AVLTreeNode<T>* min = maximum(tree->right);
499                 tree->key = min->key;
500                 tree->right = remove(tree->right, min);
501             }
502         }
503         else
504         {
505             AVLTreeNode<T>* tmp = tree;
506             tree = (tree->left!=NULL) ? tree->left : tree->right;
507             delete tmp;
508         }
509     }
510 
511     return tree;
512 }
513 
514 template <class T>
515 void AVLTree<T>::remove(T key)
516 {
517     AVLTreeNode<T>* z; 
518 
519     if ((z = search(mRoot, key)) != NULL)
520         mRoot = remove(mRoot, z);
521 }
522 
523 /* 
524  * 销毁AVL树
525  */
526 template <class T>
527 void AVLTree<T>::destroy(AVLTreeNode<T>* &tree)
528 {
529     if (tree==NULL)
530         return ;
531 
532     if (tree->left != NULL)
533         destroy(tree->left);
534     if (tree->right != NULL)
535         destroy(tree->right);
536 
537     delete tree;
538 }
539 
540 template <class T>
541 void AVLTree<T>::destroy()
542 {
543     destroy(mRoot);
544 }
545 
546 /*
547  * 打印"二叉查找树"
548  *
549  * key        -- 节点的键值 
550  * direction  --  0,表示该节点是根节点;
551  *               -1,表示该节点是它的父结点的左孩子;
552  *                1,表示该节点是它的父结点的右孩子。
553  */
554 template <class T>
555 void AVLTree<T>::print(AVLTreeNode<T>* tree, T key, int direction)
556 {
557     if(tree != NULL)
558     {
559         if(direction==0)    // tree是根节点
560             cout << setw(2) << tree->key << " is root" << endl;
561         else                // tree是分支节点
562             cout << setw(2) << tree->key << " is " << setw(2) << key << "'s "  << setw(12) << (direction==1?"right child" : "left child") << endl;
563 
564         print(tree->left, tree->key, -1);
565         print(tree->right,tree->key,  1);
566     }
567 }
568 
569 template <class T>
570 void AVLTree<T>::print()
571 {
572     if (mRoot != NULL)
573         print(mRoot, mRoot->key, 0);
574 }
575 #endif
View Code

AVL树的测试程序(AVLTreeTest.cpp)

 1 /**
 2  * C 语言: AVL树
 3  *
 4  * @author skywang
 5  * @date 2013/11/07
 6  */
 7 
 8 #include <iostream>
 9 #include "AVLTree.h"
10 using namespace std;
11 
12 static int arr[]= {3,2,1,4,5,6,7,16,15,14,13,12,11,10,8,9};
13 #define TBL_SIZE(a) ( (sizeof(a)) / (sizeof(a[0])) )
14 
15 int main()
16 {
17     int i,ilen;
18     AVLTree<int>* tree=new AVLTree<int>();
19 
20     cout << "== 依次添加: ";
21     ilen = TBL_SIZE(arr);
22     for(i=0; i<ilen; i++)
23     {
24         cout << arr[i] <<" ";
25         tree->insert(arr[i]);
26     }
27 
28     cout << "\n== 前序遍历: ";
29     tree->preOrder();
30 
31     cout << "\n== 中序遍历: ";
32     tree->inOrder();
33 
34     cout << "\n== 后序遍历: ";
35     tree->postOrder();
36     cout << endl;
37 
38     cout << "== 高度: " << tree->height() << endl;
39     cout << "== 最小值: " << tree->minimum() << endl;
40     cout << "== 最大值: " << tree->maximum() << endl;
41     cout << "== 树的详细信息: " << endl;
42     tree->print();
43 
44     i = 8;
45     cout << "\n== 删除根节点: " << i;
46     tree->remove(i);
47 
48     cout << "\n== 高度: " << tree->height() ;
49     cout << "\n== 中序遍历: " ;
50     tree->inOrder();
51     cout << "\n== 树的详细信息: " << endl;
52     tree->print();
53 
54     // 销毁二叉树
55     tree->destroy();
56 
57     return 0;
58 }
View Code

 

AVL树的C++测试程序

AVL树的测试程序代码(AVLTreeTest.cpp)在前面已经给出。在测试程序中,首先新建一棵AVL树,然后依次添加"3,2,1,4,5,6,7,16,15,14,13,12,11,10,8,9" 到AVL树中;添加完毕之后,再将8从AVL树中删除。AVL树的添加和删除过程如下图:

(01) 添加3,2
添加3,2都不会破坏AVL树的平衡性。

 

(02) 添加1
添加1之后,AVL树失去平衡(LL),此时需要对AVL树进行旋转(LL旋转)。旋转过程如下:

 

(03) 添加4
添加4不会破坏AVL树的平衡性。

 

(04) 添加5
添加5之后,AVL树失去平衡(RR),此时需要对AVL树进行旋转(RR旋转)。旋转过程如下:

 

(05) 添加6
添加6之后,AVL树失去平衡(RR),此时需要对AVL树进行旋转(RR旋转)。旋转过程如下:

 

(06) 添加7
添加7之后,AVL树失去平衡(RR),此时需要对AVL树进行旋转(RR旋转)。旋转过程如下:

 

(07) 添加16
添加16不会破坏AVL树的平衡性。

 

(08) 添加15
添加15之后,AVL树失去平衡(RR),此时需要对AVL树进行旋转(RR旋转)。旋转过程如下:

 

(09) 添加14
添加14之后,AVL树失去平衡(RL),此时需要对AVL树进行旋转(RL旋转)。旋转过程如下:

 

(10) 添加13
添加13之后,AVL树失去平衡(RR),此时需要对AVL树进行旋转(RR旋转)。旋转过程如下:

 

(11) 添加12
添加12之后,AVL树失去平衡(LL),此时需要对AVL树进行旋转(LL旋转)。旋转过程如下:

 

(12) 添加11
添加11之后,AVL树失去平衡(LL),此时需要对AVL树进行旋转(LL旋转)。旋转过程如下:

 

(13) 添加10
添加10之后,AVL树失去平衡(LL),此时需要对AVL树进行旋转(LL旋转)。旋转过程如下:

 

(14) 添加8
添加8不会破坏AVL树的平衡性。

 

(15) 添加9
但是添加9之后,AVL树失去平衡(LR),此时需要对AVL树进行旋转(LR旋转)。旋转过程如下:

 

添加完所有数据之后,得到的AVL树如下:

 

接着,删除节点8.删除节点8并不会造成AVL树的不平衡,所以不需要旋转,操作示意图如下:

 

程序运行结果如下:

== 依次添加: 3 2 1 4 5 6 7 16 15 14 13 12 11 10 8 9 
== 前序遍历: 7 4 2 1 3 6 5 13 11 9 8 10 12 15 14 16 
== 中序遍历: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 
== 后序遍历: 1 3 2 5 6 4 8 10 9 12 11 14 16 15 13 7 
== 高度: 5
== 最小值: 1
== 最大值: 16
== 树的详细信息: 
is root
is  7's   left child
is  4's   left child
is  2's   left child
is  2's  right child
is  4's  right child
is  6's   left child
is  7's  right child
is 13's   left child
is 11's   left child
is  9's   left child
is  9's  right child
is 11's  right child
is 13's  right child
is 15's   left child
is 15's  right child

== 删除根节点: 8
== 高度: 5
== 中序遍历: 1 2 3 4 5 6 7 9 10 11 12 13 14 15 16 
== 树的详细信息: 
is root
is  7's   left child
is  4's   left child
is  2's   left child
is  2's  right child
is  4's  right child
is  6's   left child
is  7's  right child
is 13's   left child
is 11's   left child
is  9's  right child
is 11's  right child
is 13's  right child
is 15's   left child
is 15's  right child

 

posted on 2014-03-29 08:49  如果天空不死  阅读(31319)  评论(23编辑  收藏  举报