博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
1066. Root of AVL Tree (25)
阅读量:4073 次
发布时间:2019-05-25

本文共 3226 字,大约阅读时间需要 10 分钟。

1066. Root of AVL Tree (25)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 (<=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 ythe 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
#include
#include
#include
#include
using namespace std;struct Node{ int data; int height;//树高 Node *leftChild; Node *rightChild;};int getHeight(Node * t){ if(!t) return 0; else return t->height;}int MAX(int a,int b){ return a>b?a:b;}Node * singleLeftRotation(Node *A){ Node *B = A->leftChild; A->leftChild = B->rightChild; B->rightChild = A; A->height = MAX(getHeight(A->leftChild),getHeight(A->rightChild))+1; B->height = MAX(getHeight(B->leftChild),getHeight(B->rightChild))+1; return B;}Node * singleRightRotation(Node *A){ Node *B = A->rightChild; A->rightChild = B->leftChild; B->leftChild = A; A->height = MAX(getHeight(A->leftChild),getHeight(A->rightChild))+1; B->height = MAX(getHeight(B->leftChild),getHeight(B->rightChild))+1; return B;}Node *doubleLeftAndRightRotaion(Node *A){ A->leftChild = singleRightRotation(A->leftChild); return singleLeftRotation(A);}Node *doubleRightAndLeftRotaion(Node *A){ A->rightChild = singleLeftRotation(A->rightChild); return singleRightRotation(A);}Node *InsertAVLTree(int data,Node * treeNode){ if(!treeNode) { treeNode = new Node; treeNode->data = data; treeNode->height = 1; treeNode->leftChild = treeNode->rightChild = NULL; } else if(data
data) //插入左子树 { treeNode->leftChild = InsertAVLTree(data,treeNode->leftChild); int leftHight = getHeight(treeNode->leftChild); int rightHight = getHeight(treeNode->rightChild); if(leftHight-rightHight==2) //需要左旋转 { if(data
leftChild->data)//LL旋转 左单旋 { treeNode = singleLeftRotation(treeNode); } else //LR旋转,LR双旋 { treeNode = doubleLeftAndRightRotaion(treeNode); } } } else if(data>treeNode->data) //插入右子树 { treeNode->rightChild = InsertAVLTree(data,treeNode->rightChild); int leftHight = getHeight(treeNode->leftChild); int rightHight = getHeight(treeNode->rightChild); if(leftHight-rightHight==-2) //右需要旋转 { if(data>treeNode->rightChild->data)//RR旋转 右单旋 { treeNode = singleRightRotation(treeNode); } else //RL旋转,RL双旋 { treeNode = doubleRightAndLeftRotaion(treeNode); } } } treeNode->height = MAX(getHeight(treeNode->leftChild),getHeight(treeNode->rightChild))+1; return treeNode;}int main(){ int n,temp; scanf("%d",&n); Node *root=NULL; for(int i =0; i
data); return 0;}

转载地址:http://vmhji.baihongyu.com/

你可能感兴趣的文章
vu 是什么
查看>>
io口的作用
查看>>
IO口的作用
查看>>
UIView的使用setNeedsDisplay
查看>>
归档与解归档
查看>>
Window
查看>>
为什么button在设置标题时要用一个方法,而不像lable一样直接用一个属性
查看>>
字符串的截取
查看>>
2. Add Two Numbers
查看>>
17. Letter Combinations of a Phone Number (DFS, String)
查看>>
93. Restore IP Addresses (DFS, String)
查看>>
19. Remove Nth Node From End of List (双指针)
查看>>
49. Group Anagrams (String, Map)
查看>>
139. Word Break (DP)
查看>>
23. Merge k Sorted Lists (Divide and conquer, Linked List) 以及java匿名内部类
查看>>
Tensorflow入门资料
查看>>
剑指_用两个栈实现队列
查看>>
剑指_顺时针打印矩阵
查看>>
剑指_栈的压入弹出序列
查看>>
剑指_复杂链表的复制
查看>>