1066 Root of AVL Tree (25分)(AVL树的实现)
2024-10-05 21:57:56
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.
Now given a sequence of insertions, you are supposed to tell the root of the resulting AVL tree.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (≤) 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:
5
88 70 61 96 120
Sample Output 1:
70
Sample Input 2:
7
88 70 61 96 120 90 65
Sample Output 2:
88
题目分析:写一个AVL树 背课文是好的 但也要理解清除AVL树的原理 之后还要学习各种平衡树 如红黑树什么的
#define _CRT_SECURE_NO_WARNINGS
#include <climits>
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
typedef struct TNode* Tree;
struct TNode {
int Data;
Tree TL;
Tree TR;
int Height; //要初始化为-1;
};
int GetHeight(Tree T) {
if (T)
return T->Height;
else
return -;
}
Tree singleLeftRotate(Tree T) {
Tree TL = T->TL;
T->TL = TL->TR;
TL->TR = T;
T->Height = max(GetHeight(T->TL), GetHeight(T->TR)) + ;
TL->Height = max(GetHeight(TL->TL), GetHeight(TL->TR)) + ;
return TL;
}
Tree singleRightRotate(Tree T) {
Tree TR = T->TR;
T->TR = TR->TL;
TR->TL = T;
T->Height = max(GetHeight(T->TL), GetHeight(T->TR)) + ;
TR->Height = max(GetHeight(TR->TL), GetHeight(TR->TR)) + ;
return TR;
}
Tree doubleLeftRightRotate(Tree T) {
T->TL = singleRightRotate(T->TL);
return singleLeftRotate(T);
}
Tree doubleRightLeftRotate(Tree T) {
T->TR = singleLeftRotate(T->TR);
return singleRightRotate(T);
}
Tree Insert(Tree T,int data) {
if (!T)
{
T = new TNode();
T->Data = data;
T->Height = ;
T->TL = T->TR = NULL;
}
else if (data > T->Data) {
T->TR = Insert(T->TR, data);
T->Height = max(GetHeight(T->TL), GetHeight(T->TR)) + ;
if (GetHeight(T->TR) - GetHeight(T->TL) == ) {
if (data > T->TR->Data)
T=singleRightRotate(T);
else
T=doubleRightLeftRotate(T);
}
}
else {
T->TL = Insert(T->TL, data);
T->Height = max(GetHeight(T->TL), GetHeight(T->TR)) + ;
if (GetHeight(T->TL) - GetHeight(T->TR) == ) {
if (data < T->TL->Data)
T=singleLeftRotate(T);
else
T=doubleLeftRightRotate(T);
}
}
return T;
} int main()
{
int N;
Tree T = NULL;
int data;
cin >> N;
for (int i = ; i < N; i++)
{
cin >> data;
T = Insert(T, data);
}
cout << T->Data;
}
最新文章
- 14. Longest Common Prefix
- QT常用资料
- Ubuntu使用ApkTool进行APK反编译
- 【转】赞一下huicpc035
- SharpGL学习笔记(十二) 光源例子:解决光源场景中的常见问题
- C# Excel写入
- web 性能忧化(IIS篇)
- mvc web api 保存多个实体类的方法
- HTML5会砸掉iOS和Android开发者的饭碗么?
- 从运行原理及使用场景看Apache和Nginx
- 乐酷工作室孙志伟:Testin云測试有广度有深度 省钱省力值得信赖
- [转]:如何使用Android Studio把自己的Android library分享到jCenter和Maven Central
- HA集群heartbeat配置--Nginx
- Spring Boot 2.x 启动全过程源码分析(上)入口类剖析
- centos7 安装python2.7与3共存
- springMVC参数传递实例
- Linux 的伪终端的基本原理 及其在远程登录(SSH,telnet等)中的应用
- 深入浅出 Java Concurrency (8): 加锁的原理 (Lock.lock)
- 重温CLR(八 ) 泛型
- jsp页面的传值(list)