AVL树自平衡的几种旋转
来源:互联网 发布:华为软件官网 编辑:程序博客网 时间:2024/05/18 15:08
标注: AVL树的基本题,仔细想想动手画画RS, LS,LRS,RLS!!code
04-树5 Root of AVL Tree (25分)
An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. Figures 1-4 illustrate the rotation rules.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (≤20) which is the total number of keys to be inserted. Then N distinct integer keys are given in the next line. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print the root of the resulting AVL tree in one line.
Sample Input 1:
588 70 61 96 120
Sample Output 1:
70
Sample Input 2:
788 70 61 96 120 90 65
Sample Output 2:
88
#include#include #include #define MAX(a,b) (a>b)?a:btypedef int Element;typedef struct AVLtreeNode* AVLTree;struct AVLtreeNode{ Element data; AVLTree left; AVLTree right; int height;};AVLTree AVl_Insertion(Element x,AVLTree T);Element GetHeight(AVLTree A);AVLTree SingleLeftRotation(AVLTree A);AVLTree SingleRightRotation(AVLTree A);AVLTree DoubleLeftRightRotation(AVLTree A);AVLTree DoubleRightLeftRotation(AVLTree A);int main(){ // freopen("11.txt","r",stdin); int N; AVLTree T = NULL; scanf("%d",&N); while (N--) { int x; scanf("%d",&x); T = AVl_Insertion(x,T); } printf("%d\n",T->data); return 0;}Element GetHeight(AVLTree A){ int HL,HR,Maxh; if (A) { HL = GetHeight(A->left); HR = GetHeight(A->right); Maxh = MAX(HL,HR); return (Maxh+1); } else return 0;}AVLTree AVl_Insertion(Element x,AVLTree T){ if(!T) { T = (AVLTree)malloc(sizeof(struct AVLtreeNode)); T->data=x; T->height = 0; T->left=T->right=NULL; } else if (x < T->data) { T->left = AVl_Insertion(x,T->left); if(GetHeight(T->left)-GetHeight(T->right)==2) { if (x < T->left->data) { T = SingleLeftRotation(T); } else { T = DoubleLeftRightRotation(T); } } } else if (x > T->data) { T->right = AVl_Insertion(x,T->right); if(GetHeight(T->left)-GetHeight(T->right)==-2) { if (x > T->right->data) { T = SingleRightRotation(T); } else { T = DoubleRightLeftRotation(T); } } } T->height = MAX(GetHeight(T->left),GetHeight(T->right))+1; return T;}AVLTree SingleLeftRotation(AVLTree A){ AVLTree B = A->left; A->left = B->right; B->right = A; A->height = MAX(GetHeight(A->left),GetHeight(A->right)) + 1; B->height = MAX(GetHeight(B->left),GetHeight(B->right)) + 1; return B;}AVLTree SingleRightRotation(AVLTree A){ AVLTree B = A->right; A->right = B->left; B->left = A; A->height = MAX(GetHeight(A->left),GetHeight(A->right)) + 1; B->height = MAX(GetHeight(B->left),GetHeight(B->right)) + 1; return B;}AVLTree DoubleLeftRightRotation(AVLTree A){ A->left = SingleRightRotation(A->left); return SingleLeftRotation(A);}AVLTree DoubleRightLeftRotation(AVLTree A){ A->right = SingleLeftRotation(A->right); return SingleRightRotation(A);}
0 0
- AVL树自平衡的几种旋转
- AVL平衡树的旋转
- AVL平衡树的旋转
- AVL树的旋转平衡
- AVL(平衡)树的旋转
- AVL树的平衡化旋转
- 平衡二叉树(AVL树)的平衡旋转
- 平衡二叉树中的AVL树的旋转
- 数据结构:关于AVL树的平衡旋转详解
- 关于AVL(自平衡二叉排序树)的旋转名称的解释
- 平衡树之AVL树旋转
- AVL平衡二叉树旋转说明
- AVL 平衡二叉树旋转方法
- 基于AVL自平衡树的TreeMap实现
- 平衡二叉树(AVL树,AVL树旋转)
- AVL树(高度平衡的二叉搜索树)平衡因子的调节和旋转
- 关于平衡二叉树(AVL tree)旋转后平衡标志调整的计算公式
- AVL - 自平衡二叉树 - 详解
- doGet与doPost的区别
- MySql分区类型以及建分区语句
- 南阳理工ACM 题目57 6174问题
- Jedis客户端操作redis
- 不同的路径 II
- AVL树自平衡的几种旋转
- javascript中函数中定义变量的作用域
- spring AOP自定义注解方式实现日志管理
- 洛谷1118 数字三角形
- Linux(CentOS)下,下载安装Nginx并配置
- Servlet编程实例 续4
- poj 2309
- Java中的Filter过滤器
- 南阳理工ACM 题目33 蛇形填数