PAT甲级——A1066 Root of AVL Tree

PAT甲级——A1066 Root of AVL Tree,第1张

概述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

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:
588 70 61 96 120
Sample Output 1:
70
Sample input 2:
788 70 61 96 120 90 65
Sample Output 2:


88
 1 #include <iostream> 2 using namespace std; 3 struct node { 4     int val; 5     struct node *left,*right; 6 }; 7 node *rotateleft(node *root) { 8     node *t = root->right; 9     root->right = t->left;10     t->left = root;11     return t;12 }13 node *rotateRight(node *root) {14     node *t = root->left;15     root->left = t->right;16     t->right = root;17     return t;18 }19 node *rotateleftRight(node *root) {20     root->left = rotateleft(root->left);21     return rotateRight(root);22 }23 node *rotateRightleft(node *root) {24     root->right = rotateRight(root->right);25     return rotateleft(root);26 }27 int getHeight(node *root) {28     if(root == NulL) return 0;29     return max(getHeight(root->left),getHeight(root->right)) + 1;30 }31 node *insert(node *root,int val) {32     if(root == NulL) {33         root = new node();34         root->val = val;35         root->left = root->right = NulL;36     } else if(val < root->val) {37         root->left = insert(root->left,val);38         if(getHeight(root->left) - getHeight(root->right) == 2)39             root = val < root->left->val ? rotateRight(root) : rotateleftRight(root);40     } else {41         root->right = insert(root->right,val);42         if(getHeight(root->left) - getHeight(root->right) == -2)43             root = val > root->right->val ? rotateleft(root) : rotateRightleft(root);44     }45     return root;46 }47 int main() {48     int n,val;49     scanf("%d",&n);50     node *root = NulL;51     for(int i = 0; i < n; i++) {52         scanf("%d",&val);53         root = insert(root,val);54     }55     printf("%d",root->val);56     return 0;57 }
总结

以上是内存溢出为你收集整理的PAT甲级——A1066 Root of AVL Tree全部内容,希望文章能够帮你解决PAT甲级——A1066 Root of AVL Tree所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/1020043.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存