平衡二叉树思想及C语言实现
2016-06-03 10:55
302 查看
形态匀称的二叉树称为平衡二叉树 (Balanced binary tree) ,其严格定义是: 一棵空树是平衡二叉树;若 T 是一棵非空二叉树,其左、右子树为 TL 和 TR ,令 hl 和 hr 分别为左、右子树的深度。当且仅当 ①TL 、 TR 都是平衡二叉树; ② | hl - hr |≤ 1;时,则 T 是平衡二叉树。【例】如图 8.3 所示。(a)平衡二叉树 (b)非平衡二叉树图8.3 平衡二叉树与非平衡二叉树相应地定义 hl - hr 为二叉平衡树的平衡因子 (balance factor) 。因此,平衡二叉树上所有结点的平衡因子可能是 -1 , 0 , 1 。换言之,若一棵二叉树上任一结点的平衡因子的绝对值都不大于 1 ,则该树是就平衡二叉树。动态平衡技术1.动态平衡技术Adelson-Velskii 和 Landis 提出了一个动态地保持二叉排序树平衡的方法,其基本思想是: 在构造二叉排序树的过程中,每当插入一个结点时,首先检查是否因插入而破坏了树的平衡性,如果是因插入结点而破坏了树的平衡性,则找出其中最小不平衡子树,在保持排序树特性的前提下,调整最小不平衡子树中各结点之间的连接关系,以达到新的平衡。通常将这样得到的平衡二叉排序树简称为 AVL 树。2.最小不平衡子树 以离插入结点最近、且平衡因子绝对值大于 1 的结点作根结点的子树。为了简化讨论,不妨假设二叉排序树的最小不平衡子树的根结点为 A ,则调整该子树的规律可归纳为下列四种情况:(1) LL 型: 新结点 X 插在 A 的左孩子的左子树里。调整方法见图 8.5(a) 。图中以 B 为轴心,将 A 结点从 B 的右上方转到 B 的右下侧,使 A 成为 B 的右孩子。图8.5 平衡调整的4种基本类型(结点旁的数字是平衡因子)(2)RR 型: 新结点 X 插在 A 的右孩子的右子树里。调整方法见图 8.5(b) 。图中以 B 为轴心,将 A 结点从 B 的左上方转到 B 的左下侧,使 A 成为 B 的左孩子。(3)LR 型: 新结点 X 插在 A 的左孩子的右子树里。调整方法见图 8.5(c) 。分为两步进行:第一步以 X 为轴心,将 B 从 X 的左上方转到 X 的左下侧,使 B 成为 X 的左孩子, X 成为 A 的左孩子。第二步跟 LL 型一样处理 ( 应以 X 为轴心 ) 。(4)RL 型: 新结点 X 插在 A 的右孩子的左子树里。调整方法见图 8.5(d) 。分为两步进行:第一步以 X 为轴心,将 B 从 X 的右上方转到 X 的右下侧,使 B 成为 X 的右孩子, X 成为 A 的右孩子。第二步跟 RR 型一样处理 ( 应以 X 为轴心 ) 。【例】实际的插入情况,可能比图 8.5 要复杂。因为 A 、 B 结点可能还会有子树。现举一例说明,设一组记录的关键字按以下次序进行插入: 4 、 5 、 7 , 2 、 1 、 3 、 6 ,其生成及调整成二叉平衡树的过程示于图 8.6 。 在图 8.6 中,当插入关键字为 3 的结点后,由于离结点 3 最近的平衡因子为 2 的祖先是根结点 5 。所以,第一次旋转应以结点 4 为轴心,把结点 2 从结点 4 的左上方转到左下侧,从而结点 5 的左孩子是结点 4 ,结点 4 的左孩子是结点 2 ,原结点 4 的左孩子变成了结点 2 的右孩子。第二步再以结点 4 为轴心,按 LL 类型进行转换。这种插入与调整平衡的方法可以编成算法和程序,这里就不再讨论了。图 8.6 二叉平衡树插入结点 ( 结点旁的数字为其平衡因子 )代码实现:utl.h
avl.h
avl.c
测试:
形态匀称的二叉树称为平衡二叉树 (Balanced binary tree) ,其严格定义是: 一棵空树是平衡二叉树;若 T 是一棵非空二叉树,其左、右子树为 TL 和 TR ,令 hl 和 hr 分别为左、右子树的深度。当且仅当 ①TL 、 TR 都是平衡二叉树; ② | hl - hr |≤ 1;时,则 T 是平衡二叉树。【例】如图 8.3 所示。(a)平衡二叉树 (b)非平衡二叉树图8.3 平衡二叉树与非平衡二叉树相应地定义 hl - hr 为二叉平衡树的平衡因子 (balance factor) 。因此,平衡二叉树上所有结点的平衡因子可能是 -1 , 0 , 1 。换言之,若一棵二叉树上任一结点的平衡因子的绝对值都不大于 1 ,则该树是就平衡二叉树。动态平衡技术1.动态平衡技术Adelson-Velskii 和 Landis 提出了一个动态地保持二叉排序树平衡的方法,其基本思想是: 在构造二叉排序树的过程中,每当插入一个结点时,首先检查是否因插入而破坏了树的平衡性,如果是因插入结点而破坏了树的平衡性,则找出其中最小不平衡子树,在保持排序树特性的前提下,调整最小不平衡子树中各结点之间的连接关系,以达到新的平衡。通常将这样得到的平衡二叉排序树简称为 AVL 树。2.最小不平衡子树 以离插入结点最近、且平衡因子绝对值大于 1 的结点作根结点的子树。为了简化讨论,不妨假设二叉排序树的最小不平衡子树的根结点为 A ,则调整该子树的规律可归纳为下列四种情况:(1) LL 型: 新结点 X 插在 A 的左孩子的左子树里。调整方法见图 8.5(a) 。图中以 B 为轴心,将 A 结点从 B 的右上方转到 B 的右下侧,使 A 成为 B 的右孩子。图8.5 平衡调整的4种基本类型(结点旁的数字是平衡因子)(2)RR 型: 新结点 X 插在 A 的右孩子的右子树里。调整方法见图 8.5(b) 。图中以 B 为轴心,将 A 结点从 B 的左上方转到 B 的左下侧,使 A 成为 B 的左孩子。(3)LR 型: 新结点 X 插在 A 的左孩子的右子树里。调整方法见图 8.5(c) 。分为两步进行:第一步以 X 为轴心,将 B 从 X 的左上方转到 X 的左下侧,使 B 成为 X 的左孩子, X 成为 A 的左孩子。第二步跟 LL 型一样处理 ( 应以 X 为轴心 ) 。(4)RL 型: 新结点 X 插在 A 的右孩子的左子树里。调整方法见图 8.5(d) 。分为两步进行:第一步以 X 为轴心,将 B 从 X 的右上方转到 X 的右下侧,使 B 成为 X 的右孩子, X 成为 A 的右孩子。第二步跟 RR 型一样处理 ( 应以 X 为轴心 ) 。【例】实际的插入情况,可能比图 8.5 要复杂。因为 A 、 B 结点可能还会有子树。现举一例说明,设一组记录的关键字按以下次序进行插入: 4 、 5 、 7 , 2 、 1 、 3 、 6 ,其生成及调整成二叉平衡树的过程示于图 8.6 。 在图 8.6 中,当插入关键字为 3 的结点后,由于离结点 3 最近的平衡因子为 2 的祖先是根结点 5 。所以,第一次旋转应以结点 4 为轴心,把结点 2 从结点 4 的左上方转到左下侧,从而结点 5 的左孩子是结点 4 ,结点 4 的左孩子是结点 2 ,原结点 4 的左孩子变成了结点 2 的右孩子。第二步再以结点 4 为轴心,按 LL 类型进行转换。这种插入与调整平衡的方法可以编成算法和程序,这里就不再讨论了。图 8.6 二叉平衡树插入结点 ( 结点旁的数字为其平衡因子 )代码实现:utl.h
#ifndef UTL_H_ #define UTL_H_ /* *整理了一些常用的功能,如内存管理 */ #include<stdio.h> #include<stdlib.h> /*申请内存*/ inline void*xalloc(int size) { void *p; p = (void*)malloc(size); /*申请失败*/ if(p==NULL) { printf("alloc error\n"); exit(1); } return p; } /*内存释放*/ #define xfree(p)free(p) #endif |
#ifndef AVL_H__ #define AVL_H__ /* *avl树数据结构及相关操作 */ #include<stdio.h> #include<stdlib.h> struct AVLTree { unsigned int nData; /*存储数据*/ struct AVLTree* pLeft; /*指向左子树*/ struct AVLTree* pRight; /*指向右子树*/ int nHeight; /*树的平衡度*/ }; /*插入操作*/ struct AVLTree* insert_tree(unsignedint nData,struct AVLTree* pNode); /*查找操作,找到返回1,否则,返回0*/ int find_tree(unsignedint data,struct AVLTree* pRoot); /*删除操作,删除所有节点*/ void delete_tree(struct AVLTree** ppRoot); /*打印操作*/ void print_tree(struct AVLTree* pRoot); #endif |
#include"avl.h" #include "utl.h" static intMax(int a,int b); static int Height(struct AVLTree* pNode); /*旋转操作*/ static struct AVLTree* SingleRotateWithLeft(struct AVLTree* pNode); static struct AVLTree* SingleRotateWithRight(struct AVLTree* pNode); static struct AVLTree* DoubleRotateWithLeft(struct AVLTree* pNode); static struct AVLTree* DoubleRotateWithRight(struct AVLTree* pNode); struct AVLTree* insert_tree(unsignedint nData,struct AVLTree* pNode) { if (NULL== pNode) { pNode = (struct AVLTree*)xalloc(sizeof(struct AVLTree)); pNode->nData= nData; pNode->nHeight= 0; pNode->pLeft= pNode->pRight=NULL; } else if (nData < pNode->nData) /*插入到左子树中*/ { pNode->pLeft= insert_tree(nData, pNode->pLeft); if (Height(pNode->pLeft)- Height(pNode->pRight)== 2) /*AVL树不平衡*/ { if (nData< pNode->pLeft->nData) { /*插入到了左子树左边, 做单旋转*/ pNode = SingleRotateWithLeft(pNode); } else { /*插入到了左子树右边, 做双旋转*/ pNode = DoubleRotateWithLeft(pNode); } } } else if (nData > pNode->nData) /*插入到右子树中*/ { pNode->pRight= insert_tree(nData, pNode->pRight); if (Height(pNode->pRight)- Height(pNode->pLeft)== 2) /*AVL树不平衡*/ { if (nData> pNode->pRight->nData) { /*插入到了右子树右边, 做单旋转*/ pNode = SingleRotateWithRight(pNode); } else { /*插入到了右子树左边, 做双旋转*/ pNode = DoubleRotateWithRight(pNode); } } } pNode->nHeight=Max(Height(pNode->pLeft), Height(pNode->pRight))+ 1; return pNode; } /*删除树*/ void delete_tree(struct AVLTree** ppRoot) { if (NULL== ppRoot||NULL==*ppRoot) return; delete_tree(&((*ppRoot)->pLeft)); delete_tree(&((*ppRoot)->pRight)); xfree(*ppRoot); *ppRoot = NULL; } /*中序遍历打印树的所有结点, 因为左结点 < 父结点 < 右结点, 因此打印出来数据的大小是递增的*/ void print_tree(struct AVLTree* pRoot) { if (NULL== pRoot) return; static int n= 0; print_tree(pRoot->pLeft); printf("[%d]nData = %u\n",++n, pRoot->nData); print_tree(pRoot->pRight); } /* *查找操作,找到返回1,否则,返回0 *data是待查找的数据 *pRoot:avl树的指针 */ int find_tree(unsignedint data,struct AVLTree* pRoot) { static int k=1; /*查找次数*/ if (NULL== pRoot) { printf("not find %d times\n", k); return 0; } if(data== pRoot->nData) { printf("find:%d times\n", k); return 1; } else if(data< pRoot->nData) { ++k; return find_tree(data, pRoot->pLeft); } else if(data> pRoot->nData) { ++k; return find_tree(data, pRoot->pRight); } } static intMax(int a,int b) { return (a > b ? a : b); } /*返回节点的平衡度*/ static int Height(struct AVLTree* pNode) { if (NULL== pNode) return -1; return pNode->nHeight; } /******************************************************************** pNode pNode->pLeft / \ pNode->pLeft ==> pNode \ / pNode->pLeft->pRight pNode->pLeft->pRight *********************************************************************/ static struct AVLTree* SingleRotateWithLeft(struct AVLTree* pNode) { struct AVLTree* pNode1; pNode1 = pNode->pLeft; pNode->pLeft= pNode1->pRight; pNode1->pRight= pNode; /*结点的位置变了, 要更新结点的高度值*/ pNode->nHeight=Max(Height(pNode->pLeft), Height(pNode->pRight))+ 1; pNode1->nHeight=Max(Height(pNode1->pLeft), pNode->nHeight)+ 1; return pNode1; } /******************************************************************** pNode pNode->pRight \ / pNode->pRight ==> pNode / \ pNode->pRight->pLeft pNode->pRight->pLeft *********************************************************************/ static struct AVLTree* SingleRotateWithRight(struct AVLTree* pNode) { struct AVLTree* pNode1; pNode1 = pNode->pRight; pNode->pRight= pNode1->pLeft; pNode1->pLeft= pNode; /*结点的位置变了, 要更新结点的高度值*/ pNode->nHeight=Max(Height(pNode->pLeft), Height(pNode->pRight))+ 1; pNode1->nHeight=Max(Height(pNode1->pRight), pNode->nHeight)+ 1; return pNode1; } static struct AVLTree* DoubleRotateWithLeft(struct AVLTree* pNode) { pNode->pLeft= SingleRotateWithRight(pNode->pLeft); return SingleRotateWithLeft(pNode); } static struct AVLTree* DoubleRotateWithRight(struct AVLTree* pNode) { pNode->pRight= SingleRotateWithLeft(pNode->pRight); return SingleRotateWithRight(pNode); } |
#include #include #include "avl.h" int main() { int i,j; AVLTree* pRoot =NULL; srand((unsignedint)time(NULL)); for (i = 0; i< 10;++i) { j = rand(); printf("%d\n", j); pRoot = Insert(j, pRoot); } PrintTree(pRoot); DeleteTree(&pRoot); return 0; } 删除节点 从AVL树中删除节点分为两个步骤:首先删除节点;然后调整平衡。删除操作对应为插入操作的逆向操作,调整平衡的时候也需要确定被删除节点的分支构型来选择合适的旋转方法。// delete the given key from AVL tree.avlnode* avl_delete(avltree* &tree, keyType key) {if (NULL == tree) {return NULL;}// delete the node with the given keyif (key > tree->key) { // key exists in the right subtreetree->right = avl_delete(tree->right, key);} else if (key < tree->key) { // key exists in the left subtreetree->left = avl_delete(tree->left, key);} else {if (NULL != tree->left) { // when left is not NULL// find max node if left treeavlnode* dn = NULL;for (dn = tree->left; NULL != dn->right; dn = dn->right) {}// change the valuetree->key = dn->key;// delete the max nodetree->left = avl_delete(tree->left, dn->key);} else if (NULL != tree->right) { // when the right tree is not NULL// find the minimal nodeavlnode* dn = NULL;for (dn = tree->right; NULL != dn->left; dn = dn->left) {}// change the valuetree->key = dn->key;// delete the minimal nodetree->right = avl_delete(tree->right, dn->key);} else { // when the node has no childfree(tree);// the tree is Empty now, no need to do any operationreturn NULL;}}// adjust the tree to balance state after deletionif (height(tree->left) - height(tree->right) == 2) { // when the left subtree is too highif (height(tree->left->right) - height(tree->left->left) == 1) { // LR modeltree = leftRightRotate(tree);} else { // LL modeltree = singleRightRotate(tree);}} else if (height(tree->left) - height(tree->right) == -2) { // when the right subtree is too highif (height(tree->right->left) - height(tree->right->right) == 1) { // RL modeltree = rightLeftRotate(tree);} else { // RR modeltree = singleLeftRotate(tree);}} else {// the tree is already balanced, nothing to do ...}// recalculate the height of the tree.tree->height = maxh(height(tree->right), height(tree->left)) + 1;return tree;}URL:http://blog.csdn.net/whucyl/article/details/17289841 |
相关文章推荐
- 如何组织构建多文件 C 语言程序(二)
- 如何写好 C main 函数
- Lua和C语言的交互详解
- 关于C语言中参数的传值问题
- 简要对比C语言中三个用于退出进程的函数
- 深入C++中API的问题详解
- 基于C语言string函数的详解
- C语言中fchdir()函数和rewinddir()函数的使用详解
- C语言内存对齐实例详解
- C语言编程中统计输入的行数以及单词个数的方法
- C语言自动生成enum值和名字映射代码
- C语言练习题:自由落体的小球简单实例
- 使用C语言判断英文字符大小写的方法
- c语言实现的带通配符匹配算法
- C语言实现顺序表基本操作汇总
- C语言中进制知识汇总
- C语言中计算正弦的相关函数总结
- 使用C语言详解霍夫曼树数据结构
- C语言实现选择排序、冒泡排序和快速排序的代码示例
- 探讨C语言的那些小秘密之断言