STL源码笔记(18)—平衡二叉树AVL(C++封装+模板)

来源:互联网 发布:mac显示应用程序桌面 编辑:程序博客网 时间:2024/05/29 06:36

AVLTree平衡二叉树

在几年前刚学数据结构时,AVL-Tree只是一个仅仅需要掌握其概念的东西,今非昔比,借看STL源码剖析的契机希望从代码层面将其拿下。

1.简介

二叉查找树给我们带来了很多方便,但是由于其在有序序列插入时就会退化成单链表(时间复杂度退化成 O(n)),AVL-tree就克服了上述困难。AVL-tree是一个“加上了平衡条件的”二叉搜索树,平衡条件确保整棵树的深度为O(log n)。

AVL树是最先发明的自平衡二叉查找树。在AVL树中任何节点的两个子树的高度最大差别为一,所以它也被称为高度平衡树。查找、插入和删除在平均和最坏情况下都是 O(log n)。增加和删除可能需要通过一次或多次树旋转来重新平衡这个树。

节点的平衡因子是它的左子树的高度减去它的右子树的高度(有时相反)。带有平衡因子1、0或-1的节点被认为是平衡的。带有平衡因子-2或2的节点被认为是不平衡的,并需要重新平衡这个树。平衡因子可以直接存储在每个节点中,或从可能存储在节点中的子树高度计算出来。

AVL树的所有操作都与二叉查找树相同,不同的是,这里AVL树需要做“AVL旋转”。


2.AVL旋转

AVL树最重要的核心部分就是AVL旋转了,这部分我的感触是,单做旋转还是挺好理解的,只不过写起代码来有点复杂,书中以插入节点为例,删除节点的部分折腾了好久。

在理解AVL旋转之前,首先得知道以下几个概念:
1. AVL 树节点的插入总是在叶子节点。
2. AVL 树在插入节点之前总是满足平衡条件的。
3. 插入新节点后有可能满足平衡条件也有可能不满足。
4. 当不满足平衡条件后,我们就需要对新的树进行旋转。

旋转之前,我们首先要找到一个X节点,这个X节点做如下定义:

假如我们在某一个叶子节点处插入一个新的节点后,此时这棵树的某些节点的平衡性会发生变化,那么我们从叶子节点向上到根节点的路径上第一个平衡性发生变化的节点。

基于这个X节点,考虑一件事情:
这个X节点分为左右子树,左右子树又有左右子树,1分2,2分4,所以以这个X节点为根节点的话,新插入的节点可能出现的位置有:

X的左孩子节点的左子树上(left-left)
X的右孩子节点的右子树上(right-right)
X的左孩子节点的右子树上(left-right)
X的右孩子节点的左子树上(right-left)

根据上述情况就延生出了4种旋转:
1.left-left Rotation
2.right-right Rotation
3.left-right Rotation
4.right-left Rotation

前两种属于单旋转,后两种属于双旋转,双旋转的操作可以由两次单旋转组成。

PS:AVL树的旋转还是得画图来理解,这里直接贴出书中的图了。

插入新节点

这里写图片描述

平衡破坏条件

这里写图片描述

单旋转(以left-left Rotation为例)

这里写图片描述

双旋转(以left-right Rotation为例)

这里写图片描述

双旋转:两次单旋转

这里写图片描述


3.AVL-Tree实现

AVL-Tree是一个二叉排序树,其基本操作也跟它类似,唯一需要注意的就是在插入,删除节点后,需要对树进行调整,让树的每个节点保持平衡。

节点的平衡因子是通过计算其左子树和右子树的差得来的,这里有两种考虑方式:
1. 每次都计算一次(递归求深度)。
2. 将平衡因子作为一个成员变量保存在节点中,平衡性发生变化的时候更新。

我采取了第1种方式,网上也有用第2种方式的,我说不上利弊,但是感觉起码掌握一种方式就可以“一招鲜”了吧。

另外,这里我用了C++类封装,为了学习还顺便使用了模板,所以类的声明和实现都放在了一个文件中,感觉内容太多,还是分开来比较好。

(1)AVL-Tree节点结构定义

//AVLNode.h#ifndef __AVLNODE_H__#define __AVLNODE_H__#include <iostream>#include <vector>#include <algorithm>template <typename KeyType>class AVLNode{public:    KeyType key;    AVLNode * left;    AVLNode * right;    AVLNode() :key(0),left(NULL), right(NULL){}    AVLNode(KeyType k) :key(k), left(NULL), right(NULL){}};#endif

(2)AVL-Tree 类声明

//AVLTree.h#ifndef __AVLTREE_H__#define __AVLTREE_H__#include "AVLNode.h"//AVL树的模板实现template <typename KeyType>class AVLTree{    typedef AVLNode<KeyType> AVLNode;//类型定义private:    AVLNode * avlroot;//私有数据结构    int __height(const AVLNode *root);//求树的高度    int __diff(const AVLNode*root);//高度差(平衡因子)    //AVL4种旋转:左左,左右,右右,右左    //X定义为插入位置节点到根节点的路径上平衡条件被改变的节点中最深的那个节点    //X通过递归返回的方式找到    //左左:插入点位于X的左孩子节点的左子树    //左右:插入点位于X的左孩子节点的右子树    //右右:插入点位于X的右孩子节点的右子树    //右左:插入点位于X的右孩子节点的左子树    //单旋转    AVLNode * __ll_Rotation(AVLNode *root);//left-left rotation    AVLNode * __rr_Rotation(AVLNode *root);//right-right rotation    //双旋转    AVLNode * __lr_Rotation(AVLNode *root);//left-right rotationAVLNode * __rl_Rotation(AVLNode *root);//right-left rotation    AVLNode * __Balance(AVLNode *root);//平衡操作    AVLNode * __Insert(AVLNode *root, const KeyType &k);//插入的内部实现    //中序遍历的两种重载    void __InorderTraversal(const AVLNode* root);//输出    void __InorderTraversal(const AVLNode*root, std::vector<KeyType>&vec);//结果保存    bool __isLeaf(AVLNode* const &);//判断是否是叶子节点    bool __isNodeWithTwoChild(AVLNode * const &);//判断是否有两个孩子    AVLNode* __search(AVLNode *const root, const KeyType &k);//查找的内部实现    void __deleteTree(AVLNode * root);//删除树的所有节点    AVLNode* __Delete(AVLNode * root, const KeyType& k);//删除节点    AVLNode*__treeMin(AVLNode *root);//求当前根节点最小(一路向左)    AVLNode*__treeMax(AVLNode*root);//求当前根节点的最大(一路向右)public:    AVLTree(){ avlroot = NULL; }//默认构造函数    ~AVLTree();//析构函数删除树中所有节点    AVLTree(const std::vector<KeyType>&);//构造函数,容器构造    AVLTree(const KeyType * arr, size_t len);//构造函数,数组构造    void InorderTraversal();//中序遍历外部接口    void InorderTraversal(std::vector<KeyType>&);//中序遍历外部接口重载2    bool Delete(const KeyType &k);//删除节点的外部接口    bool Insert(const KeyType & k);//插入节点的外部接口    bool IsEmpty(){ return avlroot == NULL; }//树空?    bool search(const KeyType &k);//查询外部接口};//class AVLTree//...#endif

(3)插入节点

套路与BST一样,小的往左走,大的往右走,新节点插到叶子节点处,这里只需要根据新节点的位置进行四种不同的旋转即可。

//AVLTree.h//插入节点的私有成员实现template <typename KeyType>AVLNode<KeyType> * AVLTree<KeyType>::__Insert(AVLNode * root, const KeyType&k){    if (NULL == root)    {        root = new AVLNode(k);        return root;    }//递归返回条件    else if (k < root->key)    {        root->left = __Insert(root->left, k);//递归左子树        //balance operation        root = __Balance(root);//平衡操作包含了四种旋转    }    else if (k>root->key)    {        root->right = __Insert(root->right, k);//递归右子树        //balance operation        root = __Balance(root);//平衡操作包含了四种旋转    }    return root;}

这里还写了一个平衡操作的函数,就把四种旋转包含进去了。

//AVLTree.cpp//树高template <typename KeyType>int AVLTree<KeyType>::__height(const AVLNode *root)//求树高{    if (root == NULL)        return 0;    return std::max(__height(root->left) , __height(root->right)) + 1;}//平衡因子template <typename KeyType>int AVLTree<KeyType>::__diff(const AVLNode *root)//求平衡因子,即当前节点左右子树的差{    return __height(root->left) - __height(root->right);}//平衡操作template <typename KeyType>AVLNode<KeyType> * AVLTree<KeyType>::__Balance(AVLNode *root){    int balanceFactor = __diff(root);//__diff用来计算平衡因子(左右子树高度差)    if (balanceFactor > 1)//左子树高于右子树    {        if (__diff(root->left) > 0)//左左外侧            root=__ll_Rotation(root);        else//左右内侧            root=__lr_Rotation(root);    }    else if (balanceFactor < -1)//右子树高于左子树    {        if (__diff(root->right) > 0)//右左内侧            root=__rl_Rotation(root);        else//右右外侧            root=__rr_Rotation(root);    }    return root;}

四种旋转

AVL Tree的核心:在刚学数据结构的时候,这部分的要求是理解即可,现在要写代码还真有点懵逼,好在双旋转可以由单旋转合成,这样也就降低了我们的工作量,只需要完成两个单旋转即可(指针操作,两个单旋转操作对称),旋转的命名以插入节点与X节点相对位置来决定。

//AVLTree.h//四种AVL旋转template <typename KeyType>AVLNode<KeyType> * AVLTree<KeyType>::__rr_Rotation(AVLNode *root)//right-right rotation{    AVLNode* tmp;    tmp = root->right;    root->right = tmp->left;    tmp->left = root;    return tmp;}template <typename KeyType>AVLNode<KeyType> * AVLTree<KeyType>::__ll_Rotation(AVLNode *root)//left-left rotation{    AVLNode * tmp;    tmp = root->left;    root->left = tmp->right;    tmp->right = root;     return tmp;}template <typename KeyType>AVLNode<KeyType> * AVLTree<KeyType>::__lr_Rotation(AVLNode *root)//left-right rotation{    AVLNode * tmp;    tmp = root->left;    root->left = __rr_Rotation(tmp);    return __ll_Rotation(root);}template <typename KeyType>AVLNode<KeyType> * AVLTree<KeyType>::__rl_Rotation(AVLNode *root)//right-left rotation{    AVLNode * tmp;    tmp = root->right;    root->right = __ll_Rotation(tmp);    return __rr_Rotation(root);}

(4)删除节点

删除节点就麻烦了,也是要分情况讨论,删除节点后,还要根据不同的情况做相应的旋转。

//AVLTree.h//删除节点的私有成员实现template <typename KeyType>AVLNode<KeyType>* AVLTree<KeyType>::__Delete(AVLNode *root, const KeyType& k){    if (NULL == root)        return root;    if (!search(k))//查找删除元素是否存在    {        std::cerr << "Delete error , key not find" << std::endl;        return root;    }    if (k == root->key)//根节点    {        if (__isNodeWithTwoChild(root))//左右子树都非空        {            if (__diff(root) > 0)//左子树更高,在左边删除            {                root->key = __treeMax(root->left)->key;//以左子树的最大值替换当前值                root->left = __Delete(root->left, root->key);//删除左子树中已经替换上去的节点            }            else//右子树更高,在右边删除            {                root->key = __treeMin(root->right)->key;                root->right = __Delete(root->right, root->key);            }        }        else//有一个孩子、叶子节点的情况合并        {            //if (!__isLeaf(root))                AVLNode * tmp = root;                root = (root->left) ? (root->left) :( root->right);                delete tmp;                tmp = NULL;        }    }//end-if    else if (k < root->key)//往左边删除    {        root->left = __Delete(root->left, k);//左子树中递归删除        //判断平衡的条件与在插入时情况类似        if (__diff(root) < -1)//不满足平衡条件,删除左边的后,右子树变高        {            if (__diff(root->right) > 0)            {                root = __rl_Rotation(root);            }            else            {                root = __rr_Rotation(root);            }        }    }//end else if    else    {        root->right = __Delete(root->right, k);        if (__diff(root) > 1)//不满足平衡条件        {            if (__diff(root->left) < 0)            {                root = __lr_Rotation(root);            }            else            {                root = __ll_Rotation(root);            }        }    }    return root;}//删除节点的外部接口template <typename KeyType>bool AVLTree<KeyType>::Delete(const KeyType &k){    return __Delete(avlroot, k)==NULL?false:true;}

(5)构造与析构

构造

以插入元素为基础,构造函数就简单了,我写了三个构造函数,应该足够。
1.默认构造函数
2.用容器构造
3.数组构造

//AVLTree.htemplate <typename KeyType>class AVLTree{    typedef AVLNode<KeyType> AVLNode;//类型定义    //...public:    AVLTree(){ avlroot = NULL; }//默认构造函数    AVLTree(const std::vector<KeyType>&);//构造函数,容器构造    AVLTree(const KeyType * arr, size_t len);//构造函数,数组构造    //...};//构造函数1-容器构造template < typename KeyType >AVLTree<KeyType>::AVLTree(const std::vector<KeyType>&vec){    avlroot = NULL;    for (int i = 0; i < (int)vec.size(); i++)    {        Insert(vec[i]);    }}//构造函数2-数组构造template < typename KeyType >AVLTree<KeyType>::AVLTree(const KeyType * arr,size_t len){    avlroot = NULL;    for (int i = 0; i < (int)len; i++)    {        Insert(*(arr + i));    }}

析构

由于节点数据结构是动态申请的,这里在程序结束后,需要手动释放,AVL-Tree的析构很简单,因为它本质上就是一棵二叉树么,直接按照二叉树的方式去做就OK~

template <typename KeyType>void AVLTree<KeyType>::__deleteTree(AVLNode *root)//删除所有节点{    if (NULL == root)        return;    __deleteTree(root->left);    __deleteTree(root->right);    delete root;    root = NULL;    return;}//析构函数template <typename KeyType>AVLTree<KeyType>::~AVLTree(){    __deleteTree(avlroot);}

(6)AVL-Tree的查找与遍历

AVL-tree也是二叉搜索树,其中序遍历满足升序,且不允许有相同元素。
同理,AVL-tree的查找与BST一样,也是小的往左走,大的往右走,不同是,由于加了平衡条件,AVLTree查找的复杂度能控制在对数范围O(log n),这也是AVL相比较BST在有序情况下会退化为线性表的一个最大优势了。

//AVLTree.h//查找内部实现template <typename KeyType>AVLNode<KeyType>* AVLTree<KeyType>::__search(AVLNode *const root, const KeyType &k){    if (NULL == root)        return NULL;    if (k == root->key)        return root;    else if (k > root->key)        return __search(root->right, k);    else        return __search(root->left, k);}//查找外部接口template <typename KeyType>bool AVLTree<KeyType>::search(const KeyType &k){    return __search(avlroot, k) == NULL ? false : true;}//中序遍历内部调用(1直接打印)template <typename KeyType>void AVLTree<KeyType>::__InorderTraversal(const AVLNode*root){    if (NULL == root)        return;    __InorderTraversal(root->left);    std::cout << root->key << " ";    __InorderTraversal(root->right);}//中序遍历内部调用(2存入容器)template <typename KeyType>void AVLTree<KeyType>::__InorderTraversal(const AVLNode*root,std::vector<KeyType>&vec){    if (NULL == root)        return;    __InorderTraversal(root->left);    vec.push_back(root->val);    __InorderTraversal(root->right);}//中序遍历外部接口(重载版本1)template <typename KeyType>void AVLTree<KeyType>::InorderTraversal(){    __InorderTraversal(avlroot);}//中序遍历外部接口(重载版本2)template <typename KeyType>void AVLTree<KeyType>::InorderTraversal(std::vector<KeyType>&vec){    __InorderTraversal(avlroot,vec);}

(7)测试代码

测试环境
Visual Studio 2013
Windows 7 32bits

主要测试插入删除等函数:

//main.cpp#include "AVLTree.h"int main(){#if 1    std::vector<int>vec = { 7, 6, 5, 4, 3, 2, 1 };    AVLTree<int> avl(vec);    avl.Insert(8);    int keyToFind = 9;    if (avl.search(keyToFind))    {        std::cout << keyToFind << " is found" << std::endl;    }    else    {        std::cerr << keyToFind << " is not found" << std::endl;    }    keyToFind = 4;    if (avl.search(keyToFind))    {        std::cout << keyToFind << " is found" << std::endl;    }    else    {        std::cerr << keyToFind << " is not found" << std::endl;    }    avl.Delete(4);    //avl.InorderTraversal();#endif    std::cout << std::endl;    system("pause");    return 0;}

4.参考

https://tfetimes.com/c-avl-tree/
http://www.sanfoundry.com/cpp-program-implement-avl-trees/
http://blog.csdn.net/xiajun07061225/article/details/8292505

1 0
原创粉丝点击