【转】AVL
#include <iostream>
#include <ctime>
#include <queue>
#include <cassert>
#include <algorithm> static const size_t TEST_DATA_COUNT = ; // 测试数据的个数
static const size_t TEST_DATA_LOWER_LIMIT = ; // 测试数据的下限
static const size_t TEST_DATA_UPPER_LIMIT = ; // 测试数据的上限 namespace ghost{ /// AVL树
template<typename ComparableT>
class AVLTree{
public:
typedef ComparableT DataType; public:
/// 节点,缓存了自身的高度
struct Node_
{
DataType data; // 可进行比较的数据
Node_* pLeftChild; // 指向左儿子
Node_* pRightChild; // 指向右儿子
int height; // 作为根节点的树高度, Node_():pLeftChild(), pRightChild(), height() // 约定叶子高度为0,故节点高度初始化为0
{ }
explicit Node_(const DataType& d): data(d), pLeftChild(), pRightChild(), height() // 约定叶子高度为0,故节点高度初始化为0
{ } Node_(const Node_& node)
{
data = node.data;
pLeftChild = node.pLeftChild;
pRightChild = node.pRightChild;
height = node.height;
};
Node_& operator =(const Node_& node)
{
data = node.data;
pLeftChild = node.pLeftChild;
pRightChild = node.pRightChild;
height = node.height;
};
};
Node_* pRoot_; // 指向根节点 public:
/// 默认初始化为空树
AVLTree():pRoot_()
{ }
~AVLTree()
{
Clear();
} public:
/// 获取树高度,空树返回-1,只有个节点返回0
int GetHeight() const{return GetHeight_(pRoot_);} public:
/// 插入数据
void Insert(const DataType& data)
{
Insert_(data, pRoot_);
}
/// 删除数据
void Erase(const DataType& data)
{
Erase_(data, pRoot_);
} /// 清空
void Clear()
{
// 销毁所有节点
RecursDestroyNode_(pRoot_);
pRoot_ = ;
} private:
/// 创建节点
static Node_* CreateNode_(const DataType& data)
{
return new Node_(data);
}
/// 销毁节点
static void DestroyNode_(Node_* pNode)
{
delete pNode;
}
/// 递归销毁节点
static void RecursDestroyNode_(Node_* pNode)
{
if (pNode)
{
// 先递归销毁子节点
RecursDestroyNode_(pNode->pLeftChild);
RecursDestroyNode_(pNode->pRightChild);
// 再销毁自身
DestroyNode_(pNode);
}
} /// 获取树高度,约定空树高度为-1
static int GetHeight_(const Node_* pRoot)
{
return pRoot?pRoot->height : -;
}
/**
计算树高度
因为约定空树高度为-1,叶子高度为0,所以树高度等于左右子树较高者高度+1
*/
static int CalcHeight_(const Node_* pRoot)
{
assert(pRoot); // 断言树存在
return std::max(GetHeight_(pRoot->pLeftChild), GetHeight_(pRoot->pRightChild)) + ;
} /**
与子树进行单旋转
由于旋转后节点将成为其原儿子的儿子,故节点指针pNode将会指向其原儿子
pChild1指向被旋转的儿子成员指针,pChild2指向另一个儿子成员指针
*/
static void SingleRatateWithChild_(Node_*& pNode, Node_* Node_::* pChild1, Node_* Node_::* pChild2)
{
assert(pChild1 && pChild2); // 断言成员变量指针有效 assert(pNode); // 断言节点存在 // 节点的儿子1重定向于儿子1的儿子2
Node_* pOriginalChild = pNode->*pChild1;
pNode->*pChild1 = pOriginalChild->*pChild2;
// 节点的原儿子1的儿子2重定向于节点
pOriginalChild->*pChild2 = pNode; // 旋转之后需要重新计算高度
pNode->height = CalcHeight_(pNode);
pOriginalChild->height = CalcHeight_(pOriginalChild); // pNode指向其原儿子
pNode = pOriginalChild;
} /// 与左子树进行单旋转
static void RotateWithLeftChild_(Node_*& pNode)
{
SingleRatateWithChild_(pNode, &Node_::pLeftChild, &Node_::pRightChild);
} /// 与右子树进行单旋转
static void RotateWithRightChild_(Node_*& pNode)
{
SingleRatateWithChild_(pNode, &Node_::pRightChild, &Node_::pLeftChild);
} /**
与子树进行双旋转
由于旋转后节点将成为其原儿子的儿子,故节点指针pNode将会指向其原儿子
pChild1指向被旋转的儿子成员指针,pChild2指向另一个儿子成员指针
*/
static void DoubleRatateWithChild_(Node_*& pNode, Node_* Node_::* pChild1, Node_* Node_::* pChild2)
{
assert(pChild1); // 断言成员变量指针有效 // 先对儿子进行一次旋转
SingleRatateWithChild_(pNode->*pChild1, pChild2, pChild1);
// 再对自己进行一次旋转
SingleRatateWithChild_(pNode, pChild1, pChild2);
} /// 与左子树进行双旋转
static void DoubleRotateWithLeftChild_(Node_*& pNode)
{
DoubleRatateWithChild_(pNode, &Node_::pLeftChild, &Node_::pRightChild);
} /// 与右子树进行双旋转
static void DoubleRotateWithRightChild_(Node_*& pNode)
{
DoubleRatateWithChild_(pNode, &Node_::pRightChild, &Node_::pLeftChild);
} /**
确定左子树是否过高(破坏了AVL平衡条件),是则与其进行旋转
当在左子树中插入新节点,或者在右子树中删除节点时使用
*/
static void RatateWithLeftChildIfNeed_(Node_*& pNode)
{
// AVL平衡条件为左右子树高度相差不超过1
// 左子树比右子树高2,需要通过旋转来使之重新达到AVL平衡条件
if ( == GetHeight_(pNode->pLeftChild) - GetHeight_(pNode->pRightChild))
{
if (GetHeight_(pNode->pLeftChild->pLeftChild) > GetHeight_(pNode->pLeftChild->pRightChild))
{
// 左子树的左子树高于左子树的右子树,应当与左子树进行单旋转
RotateWithLeftChild_(pNode);
}
else
{
// 左子树的右子树高于左子树的左子树,应当与左子树进行双旋转
DoubleRotateWithLeftChild_(pNode);
}
}
} /**
确定右子树是否过高(破坏了AVL平衡条件),是则与其进行旋转
当在右子树中插入新节点,或者在左子树中删除节点时使用
*/
static void RatateWithRightChildIfNeed_(Node_*& pNode)
{
// AVL平衡条件为左右子树高度相差不超过1
// 右子树比左子树高2,需要通过旋转来使之重新达到AVL平衡条件
if ( == GetHeight_(pNode->pRightChild) - GetHeight_(pNode->pLeftChild))
{
if (GetHeight_(pNode->pRightChild->pRightChild) > GetHeight_(pNode->pRightChild->pLeftChild))
{
// 右子树的右子树高于右子树的左子树,应当与右子树进行单旋转
RotateWithRightChild_(pNode);
}
else
{
// 右子树的左子树高于右子树的右子树,应当与右子树进行双旋转
DoubleRotateWithRightChild_(pNode);
}
}
} /**
插入新节点:
如果当前节点为空则说明找到了插入的位置,创建新节点,返回插入成功
如果数据小于当前节点数据则到左子树中插入,如果插入成功,可能需要旋转使之重新平衡(左子树过高),重新计算高度
如果数据大于当前节点数据则道右子树中插入,如果插入成功,可能需要旋转使之重新平衡(右子树过高),重新计算高度
如果数据等于当前节点数据则什么都不做,返回插入失败
*/
static bool Insert_(const DataType& data, Node_*& pNode)
{
if (!pNode)
{
// 找到位置,创建节点
pNode = CreateNode_(data);
assert(pNode); // 断言创建节点成功
return true;
}
else if (data < pNode->data)
{
// 将较小的数据插入到左子树
if (Insert_(data, pNode->pLeftChild))
{
// 成功插入新节点
// 如果需要,则与左子树进行旋转以维持AVL平衡条件
RatateWithLeftChildIfNeed_(pNode); // 重新计算高度
pNode->height = CalcHeight_(pNode);
return true;
}
}
else if (data > pNode->data)
{
// 将较大的数据插入到右子树
if (Insert_(data, pNode->pRightChild))
{
// 成功插入新节点
// 如果需要,则与右子树进行旋转以维持AVL平衡条件
RatateWithRightChildIfNeed_(pNode); // 重新计算高度
pNode->height = CalcHeight_(pNode);
return true;
}
}
else
{
// 重复数据(什么也不做,或者进行计数)
}
return false;
} /**
删除节点
查找被删除的节点:
如果当前节点为空则说明没有找到被删除的节点,返回删除失败
如果被删除的数据小于节点数据,则在节点的左子树中查找并删除,如果删除成功,可能需要旋转使之重新平衡(右子树过高),重新计算高度
如果被删除的数据大于节点数据,则在节点的右子树中查找并删除,如果删除成功,可能需要旋转使之重新平衡(左子树过高),重新计算高度
如果被删除的数据等于节点数据,则找到被删除的节点,开始删除,返回删除成功 删除节点过程,将被删除的节点作为标记节点:
如果标记节点存在左右双子树,利用右子树的最小节点的数据替换此节点数据,然后删除右子树的最小节点:
如果右子树有左子树,从左子树中找到最小节点,将其右子树提升一级,可能需要旋转使其父节点重新平衡(其父节点的右子树过高),重新计算其父节点高度
如果右子树没有左子树,此时右子树则即是最小节点,将其右子树提升一级
可能需要旋转使标记节点重新平衡(标记节点的左子树过高),重新计算标记节点高度 如果标记节点不存在左右双子树,删除标记节点,提升其子树
*/
static bool Erase_(const DataType& data, Node_*& pNode)
{
if (!pNode)
{
// 没有找到节点
return false;
}
else if (data < pNode->data)
{
// 节点较小,在左子树中删除
if (Erase_(data, pNode->pLeftChild))
{
// 成功删除节点
// 如果需要,则与右子树进行旋转以维持AVL平衡条件
RatateWithRightChildIfNeed_(pNode); // 重新计算高度
pNode->height = CalcHeight_(pNode);
return true;
} }
else if (data > pNode->data)
{
// 节点较大,在右子树中删除
if (Erase_(data, pNode->pRightChild))
{
// 成功删除节点
// 如果需要,则与左子树进行旋转以维持AVL平衡条件
RatateWithLeftChildIfNeed_(pNode); // 重新计算高度
pNode->height = CalcHeight_(pNode);
return true;
} }
else
{
// 找到了需要被删除的节点
if (pNode->pLeftChild && pNode->pRightChild)
{
// 存在双子树,利用右子树最小节点替换,并删除右子树最小节点
Node_* pMin = pNode->pRightChild;
if (pNode->pRightChild->pLeftChild)
{
// 右子树存在左子树,从右子树的左子树中找最小节点
Node_* pMinParent = pNode->pRightChild;
while (pMinParent->pLeftChild->pLeftChild)
{
pMinParent = pMinParent->pLeftChild;
}
pMin = pMinParent->pLeftChild; // 提升最小节点的右子树
pMinParent->pLeftChild = pMin->pRightChild; // 如果需要,最小节点的父节点则与其右子树进行旋转以维持AVL平衡条件
RatateWithRightChildIfNeed_(pMinParent); // 重新计算最小节点的父节点的高度
pMinParent->height = CalcHeight_(pMinParent);
}
else
{
// 右子树不存在左子树,那么提升右子树的右子树
pNode->pRightChild = pNode->pRightChild->pRightChild;
}
// 用最小节点替换
pNode->data = pMin->data; // 删除最小节点
DestroyNode_(pMin); // 如果需要,则与左子树进行旋转以维持AVL平衡条件
RatateWithLeftChildIfNeed_(pNode); // 重新计算高度
pNode->height = CalcHeight_(pNode);
}
else
{
// 不存在双子树,则直接用儿子替换
Node_* pTemp = pNode;
pNode = pNode->pLeftChild?pNode->pLeftChild : pNode->pRightChild;
// 销毁节点
DestroyNode_(pTemp);
}
return true;
}
return false;
} }; // class AVLTree } // namespace ghost
/// 随机构造测试数据
int BuildTestData()
{
return TEST_DATA_LOWER_LIMIT + rand() % (TEST_DATA_UPPER_LIMIT-TEST_DATA_LOWER_LIMIT);
} void printtree(ghost::AVLTree<int>& tree)
{
if(tree.pRoot_ == NULL)
{
return;
}
std::cout<<tree.pRoot_->data<<",";
std::queue<ghost::AVLTree<int>::Node_*> q;
q.push(tree.pRoot_);
while(!q.empty())
{
ghost::AVLTree<int>::Node_* tmp = q.front();
std::cout<<tmp->data<<",";
q.pop();
if(tmp->pLeftChild!=NULL)
{
q.push(tmp->pLeftChild);
}
else
{
if(tmp->data!=)
q.push(new ghost::AVLTree<int>::Node_());
}
if(tmp->pRightChild!=NULL)
{
q.push(tmp->pRightChild);
}
else
{
if(tmp->data!=)
q.push(new ghost::AVLTree<int>::Node_());
}
}
std::cout<<std::endl;
}
ghost::AVLTree<int> treeg;
int main()
{
srand((int)time()); // 随机插入测试数据
for (size_t i = ; i < TEST_DATA_COUNT; ++i)
{
treeg.Insert(BuildTestData());
printtree(treeg);
}
std::cout<<"++++++++++"<<std::endl; // 随机删除测试数据
for (size_t i = ; i < TEST_DATA_COUNT; ++i)
{
treeg.Erase(BuildTestData());
printtree(treeg);
}
std::cout<<"-----"<<std::endl; return ;
}
http://www.cnblogs.com/EvilGhost/archive/2011/06/17/AVLTree.html
【转】AVL的更多相关文章
- 算法与数据结构(十一) 平衡二叉树(AVL树)
今天的博客是在上一篇博客的基础上进行的延伸.上一篇博客我们主要聊了二叉排序树,详情请戳<二叉排序树的查找.插入与删除>.本篇博客我们就在二叉排序树的基础上来聊聊平衡二叉树,也叫AVL树,A ...
- AVL树原理及实现(C语言实现以及Java语言实现)
欢迎探讨,如有错误敬请指正 如需转载,请注明出处http://www.cnblogs.com/nullzx/ 1. AVL定义 AVL树是一种改进版的搜索二叉树.对于一般的搜索二叉树而言,如果数据恰好 ...
- 数据结构之平衡查找树(AVL)
AVL树的旋转操作 图解 最详细 各大教课书上讲的都是左旋与右旋,其实这样很容易理解错误,我们换一种叫法.我们称呼左旋为:逆进针旋转.我们称呼右旋为:顺进针旋转.
- AVL树
AVL树 在二叉查找树(BST)中,频繁的插入操作可能会让树的性能发生退化,因此,需要加入一些平衡操作,使树的高度达到理想的O(logn),这就是AVL树出现的背景.注意,AVL树的起名来源于两个发明 ...
- AVL Insight 开源情报工具:一站式情报管理服务
一.概要 AVL Insight 开源情报工具是安天移动安全推出的一款情报收集工具,它是配合AVL Insight移动威胁情报平台的Chrome浏览器扩展程序,用户可以使用该工具,对网站中的公开信息进 ...
- 病毒四度升级:安天AVL Team揭露一例跨期两年的电信诈骗进化史
自2014年9月起,安天AVL移动安全团队持续检测到一类基于Android移动平台的间谍类病毒,病毒样本大多伪装成名为"最高人民检察院"的应用.经过反编译逆向分析以及长期的跟踪调查 ...
- 安天AVL联合小米MIUI首擒顽固病毒“不死鸟”
不死鸟作为希腊神话中的一种怪物,拥有不断再生的能力,每当寿限将至时,它会在巢穴中自焚,并在三天后重新复活.就在近期,安天AVL移动安全团队和小米MIUI安全中心发现了病毒界的“不死鸟”,其顽固程度之深 ...
- AVL树的平衡算法(JAVA实现)
1.概念: AVL树本质上还是一个二叉搜索树,不过比二叉搜索树多了一个平衡条件:每个节点的左右子树的高度差不大于1. 二叉树的应用是为了弥补链表的查询效率问题,但是极端情况下,二叉搜索树会无限接近 ...
- 【数据结构】平衡二叉树—AVL树
(百度百科)在计算机科学中,AVL树是最先发明的自平衡二叉查找树.在AVL树中任何节点的两个子树的高度最大差别为一,所以它也被称为高度平衡树.查找.插入和删除在平均和最坏情况下都是O(log n).增 ...
- 平衡二叉树AVL删除
平衡二叉树的插入过程:http://www.cnblogs.com/hujunzheng/p/4665451.html 对于二叉平衡树的删除采用的是二叉排序树删除的思路: 假设被删结点是*p,其双亲是 ...
随机推荐
- Chp11: Sorting and Searching
Common Sorting Algo: Bubble Sort: Runime: O(n2) average and worst case. Memory: O(1). void BubbleSor ...
- POJ 2255 Tree Recovery(根据前序遍历和中序遍历,输出后序遍历)
题意:给出一颗二叉树的前序遍历和中序遍历的序列,让你输出后序遍历的序列. 思路:见代码,采用递归. #include <iostream> #include <stdio.h> ...
- iptables 代理设置
代理: 开启转发:echo 1 > /proc/sys/net/ipv4/ip_forward iptables -t nat -A POSTROUTING -s 192.168.1.0/24 ...
- [转]Java数组初始化详解
一维数组1) int[] a; //声明,没有初始化 2) int[] a=new int[5]; //初始化为默认值,int型为0 3) int[] a={1,2,3,4,5}; ...
- 李洪强iOS开发之Foundation框架—集合
Foundation框架—集合 一.NSArray和NSMutableArray (一)NSArray不可变数组 (1)NSArray的基本介绍 NSArray是OC中使用的数组,是面向对象的,以面向 ...
- QT进度条QProgressBar的练习(定制QProgressBar,单独成为一个控件)
progressbar.h #ifndef PROGRESSBAR_H #define PROGRESSBAR_H #include <QProgressBar> class QStrin ...
- redhat 新装后不能联网
1.ifconfig查看是否有ip地址,如果没有就配置,命令如下: [root@redhat ~]# system-config-network 设置DHCP 为 [*] [ok]后,重新ifconf ...
- JCIFS读取远程服务器文件过慢的解决方法
JCIFS读取远程服务器文件过慢的解决方法 发表于3年前(2013-07-12 11:23) 阅读(1174) | 评论(0) // 我要收藏"; var favor_del = &qu ...
- PCL—低层次视觉—点云分割(超体聚类)
1.超体聚类——一种来自图像的分割方法 超体(supervoxel)是一种集合,集合的元素是“体”.与体素滤波器中的体类似,其本质是一个个的小方块.与之前提到的所有分割手段不同,超体聚类的目的并不是分 ...
- 前端自动化工具:Grunt使用教程
1.下载node.js,然后将node.exe文件所在的目录加入path环境变量 2.安装npm管理工具 2.1.下载npm源码,解压到npm文件夹里,不要把npm放在和node.exe相同的文件夹 ...