04-树5 Root of AVL Tree (25分)
来源:互联网 发布:python 文件重命名 编辑:程序博客网 时间:2024/04/28 08:22
04-树5 Root of AVL Tree (25分) Now given a sequence of insertions, you are supposed to tell the root of the resulting AVL tree.
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
主要思路:
1、二叉平衡树的插入新结点的四种情况判断(此处递归插入)
2、LL,RR,LR,RL旋转的实现
#include <iostream>#include <vector>using namespace std;#define Max_Node 1000000typedef struct node//树的结点{ int left; int right; int height;}Node;int Get_Height(vector<Node>& Tree,int root)//递归求该结点在二叉树中的高度{ int HL,HR,MaxH; if (root) { HL=Get_Height(Tree, Tree[root].left); HR=Get_Height(Tree, Tree[root].right); if (HL>HR) { MaxH=HL; }else { MaxH=HR; } return (MaxH+1); }else { return 0; }}int SingleLeftRotation(int A,vector<Node>& Tree)//LL旋转{ int B=Tree[A].left; Tree[A].left=Tree[B].right; Tree[B].right=A; Tree[A].height=Get_Height(Tree, A); Tree[B].height=Get_Height(Tree, B); return B;}int SingleRightRotation(int A,vector<Node>& Tree)//RR旋转{ int B=Tree[A].right; Tree[A].right=Tree[B].left; Tree[B].left=A; Tree[A].height=Get_Height(Tree, A); Tree[B].height=Get_Height(Tree, B); return B;}int DoubleLeftRightRotation(int A,vector<Node>& Tree)//LR旋转{ Tree[A].left=SingleRightRotation(Tree[A].left, Tree); return SingleLeftRotation(A, Tree);}int DoubleRightLeftRotation(int A,vector<Node>& Tree)//RL旋转{ Tree[A].right=SingleLeftRotation(Tree[A].right, Tree); return SingleRightRotation(A, Tree);}int AVL_Insertion(int X,vector<Node>& Tree,int T)//二叉平衡树插入一个新结点{ if(!T) { Tree[X].left=0; Tree[X].right=0; Tree[X].height=0; T=X; }else { if (X<T)//小于当前结点,插入到当前结点的左子树 { Tree[T].left=AVL_Insertion(X, Tree, Tree[T].left); if (Get_Height(Tree, Tree[T].left)-Get_Height(Tree, Tree[T].right)==2)//插入后出现不平衡的情形(只可能为LL或LR的情形) { if (X<Tree[T].left) { T=SingleLeftRotation(T, Tree); }else { T=DoubleLeftRightRotation(T, Tree); } } }else if(X>T)//大于当前结点,插入到当前结点的右子树 { Tree[T].right=AVL_Insertion(X, Tree, Tree[T].right); if (Get_Height(Tree, Tree[T].left)-Get_Height(Tree, Tree[T].right)==-2) { if (X>Tree[T].right) { T=SingleRightRotation(T, Tree); }else { T=DoubleRightLeftRotation(T, Tree); } } } } Tree[T].height=Get_Height(Tree, T); return T;}int main(){ vector<Node> Tree(Max_Node); int Root=0; int N=0; cin>>N; int X=0; while (N--) { cin>>X; Root=AVL_Insertion(X, Tree, Root); } cout<<Root; return 0;}
0 0
- 04-树5 Root of AVL Tree (25分)
- 04-树5 Root of AVL Tree (25分)
- 04-树5 Root of AVL Tree (25分)
- 04-树5 Root of AVL Tree (25分)
- 04-树5 Root of AVL Tree (25分)
- 04-树5 Root of AVL Tree (25分)
- 04-树5 Root of AVL Tree (25分)
- 04-树5 Root of AVL Tree (25分)
- 04-树5 Root of AVL Tree (25 分)
- 04-树5 Root of AVL Tree (25分)
- 04-树5 Root of AVL Tree (25分)
- 04-树5 Root of AVL Tree (25分)
- 04-树5 Root of AVL Tree (25分)
- 04-树5 Root of AVL Tree(25 分)
- 04-树5 Root of AVL Tree(25 分
- 04-树5 Root of AVL Tree(25 分)
- 04-树5 Root of AVL Tree (25分)
- Root of AVL Tree(25 分)
- eclipse开发Android时,使用第三方模拟器BlueStacks加速调试
- Kali安装后简单配置
- jQuery插件之Cookie
- 第五周实践项目3————时间类(2)
- 深入理解快速排序
- 04-树5 Root of AVL Tree (25分)
- Apache Spark Jobs 性能调优(二)
- Core Animation
- C语言学习笔记之文件I/O(fgets()函数和fputs()函数)
- 40个Java多线程问题总结
- 《C++ Primer》 第四版 第3章 标准库类型
- 20160328servlet学习笔记网站代码结构分析MVC结构
- [ Android SystemUi ] 动态修改系统状态栏颜色【沉浸式状态栏】
- 代码重定位实战1~2