赞
踩
数据结构课程设计之平衡二叉树算法实现。
什么是平衡二叉树?
是一种结构平衡的二叉搜索树,是一种每个节点的左右子树高度差都不超过1的二叉树。
由前苏联的数学家 Adelse-Velskil 和 Landis 在 1962 年提出的高度平衡的二叉树,根据科学家的英文名也称为 AVL 树。它具有如下几个性质:
举个例子,下图不是平衡二叉树,因为结点 60 的左子树高度为2,右子树高度为0,高度超过1。
什么是二叉搜索树?
它或者是一棵空树,或者是具有下列性质的 二叉树 : 若它的左子树不空,则左子树上所有结点的值均小于它的 根结点 的值; 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值; 它的左、右子树也分别为 二叉排序树 。. 二叉搜索树作为一种经典的数据结构,它既有链表的快速插入与删除操作的特点,又有数组快速查找的优势;所以应用十分广泛,例如在文件系统和数据库系统一般会采用这种数据结构进行高效率的排序与检索操作。
为什么要拥有平衡二叉树呢?
二叉搜索树一定程度上可以提高搜索效率,但是当原序列有序时,例如序列 A = {1,2,3,4,5,6},构造二叉搜索树如下图。依据此序列构造的二叉搜索树为右斜树,同时二叉树退化成单链表,搜索效率降低为 O(n)。
二叉搜索树的查找效率取决于树的高度,因此保持树的高度最小,即可保证树的查找效率。同样的序列 A,将其改为下图的方式存储,查找元素 6 时只需比较 3 次,查找效率提升一倍。
可以看出当节点数目一定,保持树的左右两端保持平衡,树的查找效率最高。
这种左右子树的高度相差不超过 1 的树为平衡二叉树。
平衡因子:
某结点的左子树和右子树的高度差即为该结点的平衡因子,平衡二叉树中不存在平衡因子大于 1 的节点。
如何构造平衡二叉树?
首先我们知道,平衡二叉树是一颗二叉树,我们二叉树结构具有左子树、右子树、值,而在平衡二叉树,由于我们需要检查它的平衡性,以及失衡调整操作,我们需要定义一个深度。下面代码块是平衡二叉树的定义。
- //平衡二叉树的结构
- typedef struct AVLNode{
- int depth;//深度
- struct AVLNode *left;//左孩子
- struct AVLNode *right;//右孩子
- struct AVLNode *parent;//父结点
- ElenmentType value; //值
- }AVLtree,Tree;
平衡二叉树的初始化:
- //初始化
- Tree* Init(ElenmentType value){
- Tree* root=new Tree();
- root->parent = NULL;
- root->depth = 0;
- root->left = root->right = NULL;
- root->value=value;
- return root;
- }
平衡二叉树插入时的失衡和调整:
二叉树的基本操作有:插入、删除、遍历等。
对于平衡二叉树而言,插入和删除会导致二叉树的失衡。
下图是一棵平衡二叉树
在此平衡二叉树插入节点 99 ,树结构变为:
节点 66 的左子树高度为 1,右子树高度为 3,此时平衡因子为 -2,树失去平衡。
最小失衡子树:在新插入的结点向上查找,以第一个平衡因子的绝对值超过 1 的结点为根的子树称为最小不平衡子树。也就是说,一棵失衡的树,是有可能有多棵子树同时失衡的。而这个时候,我们只要调整最小的不平衡子树,就能够将不平衡的树调整为平衡的树。
平衡二叉树的失衡调整主要是通过旋转最小失衡子树来实现的。
根据旋转的方向有两种处理方式,左旋 与 右旋 。
旋转的目的就是减少高度,通过降低整棵树的高度来平衡。哪边的树高,就把那边的树向上旋转。
左旋:
在下图中,加入新节点 99 后, 节点 66 的左子树高度为 1,右子树高度为 3,此时平衡因子为 -2。为保证树的平衡,此时需要对节点 66 做出旋转,因为右子树高度高于左子树,对节点进行左旋操作,流程如下:
(1)节点的右孩子替代此节点位置 (2)右孩子的左子树变为该节点的右子树 (3)节点本身变为右孩子的左子树
- //RR型调整函数,执行左旋
- Tree* RR_rotate(Tree * root){
- Tree* temp;
- temp = root->right;
- root->right = temp->left;
- temp->left = root;
- return temp;
- }
右旋:
(1)节点的左孩子代表此节点 (2)节点的左孩子的右子树变为节点的左子树 (3)将此节点作为左孩子节点的右子树。
- //LL型调整函数,执行右旋
- Tree* LL_rotate(Tree *root){
- Tree *temp;
- temp = root->left;
- root->left = temp->right;
- temp->right = root;
- return temp;
-
- }
四种可能的插入方式:
LL和RR的插入方式中,我们对最小失衡子树实现右旋和左旋即可。
若 A 的左孩子节点 B 的右子树 E 插入节点 F ,导致节点 A 失衡,如图
这种称为LR的插入方式,在LR的插入方式中,我们先对最小失衡子树A的左孩子B进行左旋,再对最小失衡子树A进行右旋.
- //LR型调整函数,先左旋转,再右旋转
- Tree* LR_rotate(Tree* root){
- Tree* temp;
- temp = root->left;
- root->left =RR_rotate(temp);
- return LL_rotate(root);
- }
若 A 的右孩子节点 C 的左子树 D 插入节点 F ,导致节点 A 失衡,如图
这种称为RL的插入方式,在RL的插入方式中,我们先对最小失衡子树 A 的右孩子 C 进行右旋操作,再对最小失衡子树 A 做左旋操作。
- //RL型调整函数,先右旋转,再左旋转
- Tree* RL_rotate(Tree* root){
- Tree* temp;
- temp = root->right;
- root->right=LL_rotate(temp);
- return RR_rotate(root);
- }
- //插入结点
- Tree* Insert(Tree* root,ElenmentType k )
- {
- if (NULL == root)
- {
- root = Init(k);//如果根结点为null,则直接将值为根结点
- if(root==NULL)
- cout<<"创建失败"<<endl;
- return root;
- }
- else if (k < root->value)
- {
- root->left = Insert(root->left, k);//递归左子树
- root = Balance(root);//平衡操作
- }
- else if (k>root->value)
- {
- root->right = Insert(root->right, k);//递归右子树
- root = Balance(root);//平衡操作
- }
- return root;
- }

平衡二叉树删除时的失衡和调整:
删除操作分为四种情况:
(1)删除叶子节点
(2)删除的节点只有左子树
(3)删除的节点只有右子树
(4)删除的节点既有左子树又有右子树
AVL 树在删除节点后需要重新检查平衡性并修正,同时,删除操作与插入操作后的平衡修正区别在于,插入操作只需要调整最小的不平衡子树,而删除操作需要调整所有的不平衡子树。
删除操作的大致步骤如下:
1.查找删除元素是否存在
2.删除元素的值与根节点的值的比较,删除元素的值若小于根节点的值,则往左边删,大于则往右边删,直到找到删除节点。
3.判断删除的节点的情况,是既有左子树又有右子树,还是只有左子树,还是只有右子树,还是为叶子节点。
4.根据不同的情况进行删除
若删除的节点既有左子树又有右子树
1)左子树更高,在左边删除,以左子树的最大值替换当前值,删除左子树中已经替换上去的节点。(删除替换节点进行了递归)
2)右子树更高,在右边删除,以右子树的最小值替换当前值,删除右子树中已经替换上去的节点。(删除替换节点进行了递归)
若删除的节点只有一个孩子或者为叶子节点,那么我们可以直接进行删除。
5.删除之后,进行平衡性检查,若失衡,则进行失衡调整。
注:对于删除操作造成的非平衡状态的修正,可以这样理解:对左或者右子树的删除操作相当于对右或者左子树的插入操作,然后再对应上插入的四种情况选择相应的旋转就好了。
- //删除结点
- Tree* Delete(Tree *root, const ElenmentType k)
- {
- if (NULL == root)
- return root;
- if (!binaryTreeSearch(root,k))//查找删除元素是否存在
- {
- cout<<"删除失败,未找到"<<endl;
- return root;
- }
-
- if (k == root->value)//根节点
- {
- if (root->left!=NULL&&root->right!=NULL)//左右子树都非空
- {
- if (diff(root) > 0)//左子树更高,在左边删除
- {
- root->value = tree_max(root->left);//以左子树的最大值替换当前值
- root->left = Delete(root->left, root->value);//删除左子树中已经替换上去的节点
- }
- else//右子树更高,在右边删除
- {
- root->value = tree_min(root->right);//以右子树的最小值替换当前值
- root->right = Delete(root->right, root->value);//删除右子树中已经替换上去的节点
- }
- }
- else//有一个孩子、叶子节点的情况合并
- {
- Tree * tmp = root;
- root = (root->left) ? (root->left) :( root->right);
- delete tmp;
- tmp = NULL;
- }
- }
- //往左边删
- else if (k < root->value)
- {
- root->left = Delete(root->left, k);
- //不满足平衡条件
- if (diff(root) < -1)
- {
- if (diff(root->right) > 0)
- {
- root = RL_rotate(root);
- }
- else
- {
- root = RR_rotate(root);
- }
- }
- }
- //往右边删
- else
- {
- root->right = Delete(root->right, k);
- //不满足平衡 条件
- if (diff(root) > 1)
- {
- if (diff(root->left) < 0)
- {
- root = LR_rotate(root);
- }
- else
- {
- root = LL_rotate(root);
- }
- }
- }
- return root;
- }

完整代码如下:
- #include<iostream>
- using namespace std;
-
-
- typedef int ElenmentType;
- //平衡二叉树的结构
- typedef struct AVLNode{
- int depth;//深度
- struct AVLNode *left;//左孩子
- struct AVLNode *right;//右孩子
- struct AVLNode *parent;//父结点
- ElenmentType value; //值
- }AVLtree,Tree;
- //初始化
- Tree* Init(ElenmentType value){
- Tree* root=new Tree();
- root->parent = NULL;
- root->depth = 0;
- root->left = root->right = NULL;
- root->value=value;
- return root;
- }
-
- //LL型调整函数,执行右旋
- Tree* LL_rotate(Tree *root){
- Tree *temp;
- temp = root->left;
- root->left = temp->right;
- temp->right = root;
- return temp;
-
- }
- //RR型调整函数,执行左旋
- Tree* RR_rotate(Tree * root){
- Tree* temp;
- temp = root->right;
- root->right = temp->left;
- temp->left = root;
- return temp;
- }
- //LR型调整函数,先左旋转,再右旋转
- Tree* LR_rotate(Tree* root){
- Tree* temp;
- temp = root->left;
- root->left =RR_rotate(temp);
- return LL_rotate(root);
- }
- //RL型调整函数,先右旋转,再左旋转
- Tree* RL_rotate(Tree* root){
- Tree* temp;
- temp = root->right;
- root->right=LL_rotate(temp);
- return RR_rotate(root);
- }
-
- //树高
- int height(Tree* root)
- {
- if (root == NULL)
- return 0;
- int max;
- int left=height(root->left);
- int right=height(root->right);
- if(left>=right)
- max=left;
- else
- max=right;
- return max+1;
- }
- //求叶子节点个数
- int GetSumOfLeafNode(Tree* root)
- {
- if(root == NULL)
- return 0;
-
- if(root->left == NULL && root->right == NULL)
- return 1;
- else
- {
- return GetSumOfLeafNode(root->left)
- + GetSumOfLeafNode(root->right);
- }
- }
- //平衡因子,即当前节点左右子树的差
- int diff(Tree* root)
- {
- return height(root->left) - height(root->right);
- }
-
- //平衡操作
- Tree* Balance(Tree* root)
- {
- int balanceFactor = diff(root);//平衡因子(左右子树高度差)
- if (balanceFactor > 1)//左子树高于右子树
- {
- if (diff(root->left) > 0)
- //LL的情况
- root=LL_rotate(root);
- else
- //LR的情况
- root=LR_rotate(root);
- }
- else if (balanceFactor < -1)//右子树高于左子树
- {
- if (diff(root->right) > 0)
- //RL的情况
- root=RL_rotate(root);
- else
- //RR的情况
- root=RR_rotate(root);
- }
- return root;
- }
- //插入结点
- Tree* Insert(Tree* root,ElenmentType k )
- {
- if (NULL == root)
- {
- root = Init(k);//如果根结点为null,则直接将值为根结点
- if(root==NULL)
- cout<<"创建失败"<<endl;
- return root;
- }
- else if (k < root->value)
- {
- root->left = Insert(root->left, k);//递归左子树
- root = Balance(root);//平衡操作
- }
- else if (k>root->value)
- {
- root->right = Insert(root->right, k);//递归右子树
- root = Balance(root);//平衡操作
- }
- return root;
- }
- //前序遍历
- void displayTree_Pre(Tree* node){
- if(node == NULL) return;
- cout<<node->value<<" ";
- if(node->left != NULL){
- displayTree_Pre(node->left);
- }
- if(node->right != NULL){
- displayTree_Pre(node->right);
- }
- }
- //中序遍历
- void displayTree_Mid(Tree* node){
- if(node == NULL) return;
- if(node->left != NULL){
- displayTree_Mid(node->left);
- }
- cout<<node->value<<" ";
- if(node->right != NULL){
- displayTree_Mid(node->right);
- }
- }
- //后序遍历
- void displayTree_Post(Tree* node){
- if(node == NULL) return;
- if(node->left != NULL){
- displayTree_Post(node->left);
- }
- if(node->right != NULL){
- displayTree_Post(node->right);
- }
- cout<<node->value<<" ";
- }
- //查找value
- Tree* binaryTreeSearch(Tree *node,int value){
- if(node->value==value)
- return node;
- //大于,在左边找
- else if(node->value>value){
- if(node->left!=NULL)
- return binaryTreeSearch(node->left,value);
- else return NULL;
- }
- //否则,在右边找
- else{
- if(node->right!=NULL)
- return binaryTreeSearch(node->right,value);
- else
- return NULL;
- }
- }
-
- //平衡二叉树最大值
- ElenmentType tree_max(Tree *node){
- int value;
- value=node->value;
- if(node->right!=NULL)
- return tree_max(node->right);
- else
- return value;
-
- }
- //平衡二叉树最小值
- ElenmentType tree_min(Tree *node){
- int value;
- value=node->value;
- if(node->left!=NULL)
- return tree_min(node->left);
- else
- return value;
- }
-
- //删除结点
- Tree* Delete(Tree *root, const ElenmentType k)
- {
- if (NULL == root)
- return root;
- if (!binaryTreeSearch(root,k))//查找删除元素是否存在
- {
- cout<<"删除失败,未找到"<<endl;
- return root;
- }
-
- if (k == root->value)//根节点
- {
- if (root->left!=NULL&&root->right!=NULL)//左右子树都非空
- {
- if (diff(root) > 0)//左子树更高,在左边删除
- {
- root->value = tree_max(root->left);//以左子树的最大值替换当前值
- root->left = Delete(root->left, root->value);//删除左子树中已经替换上去的节点
- }
- else//右子树更高,在右边删除
- {
- root->value = tree_min(root->right);//以右子树的最小值替换当前值
- root->right = Delete(root->right, root->value);//删除右子树中已经替换上去的节点
- }
- }
- else//有一个孩子、叶子节点的情况合并
- {
- Tree * tmp = root;
- root = (root->left) ? (root->left) :( root->right);
- delete tmp;
- tmp = NULL;
- }
- }
- //往左边删
- else if (k < root->value)
- {
- root->left = Delete(root->left, k);
- //不满足平衡条件
- if (diff(root) < -1)
- {
- if (diff(root->right) > 0)
- {
- root = RL_rotate(root);
- }
- else
- {
- root = RR_rotate(root);
- }
- }
- }
- //往右边删
- else
- {
- root->right = Delete(root->right, k);
- //不满足平衡 条件
- if (diff(root) > 1)
- {
- if (diff(root->left) < 0)
- {
- root = LR_rotate(root);
- }
- else
- {
- root = LL_rotate(root);
- }
- }
- }
- return root;
- }
-
-
-
- int main(){
- int a[10]={12,13,55,66,44,77,99,33,46,79};
- Tree *root=NULL;
- for(int i=0;i<10;i++){
- root = Insert(root,a[i]);
- }
- cout<<"平衡二叉树前序遍历:";
- displayTree_Pre(root);
- cout<<endl;
- cout<<"平衡二叉树中序遍历:";
- displayTree_Mid(root);
- cout<<endl;
- cout<<"平衡二叉树高度:"<<height(root)<<endl;
- int value = 33;
- cout<<"平衡二叉树最大值:"<<tree_max(root)<<endl;
- cout<<"平衡二叉树最小值:"<<tree_min(root)<<endl;
- cout<<"平衡二叉树叶子结点:"<<GetSumOfLeafNode(root)<<endl;
- Tree* obj;
- if((obj=binaryTreeSearch(root,value))==NULL){
- cout<<value<<"值不存在"<<endl;
- }
- else
- cout<<value<<"值存在"<<endl;
- root = Insert(root,5);
- cout<<"插入结点5后,前序遍历:";
- displayTree_Pre(root);
- cout<<endl;
- cout<<"插入结点5后,中序遍历:";
- displayTree_Mid(root);
- cout<<endl;
- int del=79;
- if (!binaryTreeSearch(root,del))//查找删除元素是否存在
- {
- cout<<"删除失败,未找到要删除的结点"<<endl;
- }else{
- Delete(root,del);
- cout<<"删除结点后,前序遍历: ";
- displayTree_Pre(root);
- cout<<endl;
- cout<<"删除结点后,中序遍历: ";
- displayTree_Mid(root);
- cout<<endl;
- }
- return 0;
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。