数据结构——AVL平衡树
2015-01-04 00:09
190 查看
1、是二叉搜索树(Binary Search Tree)
2、树和所有左右子树高度之差为-1,0,1
平衡因子(balance factor)
=右子树高度-左子树高度
平衡化旋转:
1、从插入位置向根节点计算节点的平衡因子;
2、若发现不平衡点(即平衡因子绝对值大于1),从此节点向下取两层;
3、若三节点在同一直线上,则左单旋或右单旋,中间为旋转中心;
4、否则左右双旋或右左双旋,最下为旋转中心。
View Code
平衡删除完整代码:
待补充
2、树和所有左右子树高度之差为-1,0,1
平衡因子(balance factor)
=右子树高度-左子树高度
平衡化旋转:
1、从插入位置向根节点计算节点的平衡因子;
2、若发现不平衡点(即平衡因子绝对值大于1),从此节点向下取两层;
3、若三节点在同一直线上,则左单旋或右单旋,中间为旋转中心;
4、否则左右双旋或右左双旋,最下为旋转中心。
AVLnode *AVLtree::insert(const string &item) { AVLnode *t=root,*newnode,*parent=NULL; while(t!=NULL) { parent=t; if(item==t->nodeValue) return NULL; else if(item<t->nodeValue) t=t->left; else t=t->right; } newnode=creatAVLNode(item,NULL,NULL,parent); if(parent==NULL) root=newnode; else if(item>parent->nodeValue) parent->right=newnode; else parent->left=newnode; treeSize++; int bf=0,count=0; AVLnode *cur=newnode,*p=newnode; while(bf>=-1&&bf<=1&&cur!=root) { cur=cur->parent; count++; bf=balanceFactor(cur); } if(bf<-1||bf>1) { if(count==2) ; else { for(int i=0;i<count-2;i++) p=p->parent; } //all left if(p->nodeValue<p->parent->nodeValue&&p->parent->nodeValue<cur->nodeValue) rightRotate(p->parent); //all right else if(p->nodeValue>p->parent->nodeValue&&p->parent->nodeValue>cur->nodeValue) leftRotate(p->parent); //right left else if(p->nodeValue<p->parent->nodeValue&&p->parent->nodeValue>cur->nodeValue) rightleftRotate(p); //left right else if(p->nodeValue>p->parent->nodeValue&&p->parent->nodeValue<cur->nodeValue) leftrightRotate(p); } return newnode; }
View Code
平衡删除完整代码:
待补充
相关文章推荐
- 数据结构与算法总论
- COM+(三层结构)应用中数据访问的技术(E)
- MySQL数据目录结构大全
- 数据结构大学教程之数据结构及其基本概念(2)
- 数据结构之应用 "栈(Stack)" 实现: 解析算术表达式及计算求值 (C#/Java)
- 经典数据结构题目
- 数据结构
- 数据结构实验2
- 数据结构大学教程之数据结构及其基本概念(1)
- Decal SDL-Delphi的范型类库-通用数据结构与算法类库(一)
- c# 数据结构
- 如何删除某个用户的所有表的数据而保留表结构?
- 获取Oracle表结构时,两种OleDb驱动得到的列数据类型“DATA_TYPE”值的类型不同
- MySQL数据目录结构
- 根据基本表结构及其数据生成 INSERT ... 的 SQL
- 树型结构数据在数据库基本表中的存储及维护
- 数据结构中关键路径算法的实现与应用
- MySQL数据目录结构
- 数据结构之应用 "栈(Stack)" 实现: 解析算术表达式及计算求值 (C#/Java)
- SQL Server数据对象结构的动态建立与访问