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

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

时间限制
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.

850322-20151206112322627-2044226447.jpg 
850322-20151206112322830-1693220595.jpg

850322-20151206112323018-2089046593.jpg 
850322-20151206112323361-1773857691.jpg

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
来源: <>
 
 
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4. struct Node
  5. {
  6. int value;
  7. Node* leftChild;
  8. Node* rightChild;
  9. int height; //左子树高度 - 右子树高度
  10. Node(int v):value(v),leftChild(NULL),rightChild(NULL),height(0){}
  11. };
  12. int getHeight(Node* node)
  13. {
  14. if(node == NULL)return -1;
  15. return node->height;
  16. }
  17. bool isBalanced(Node* parent)
  18. {
  19. return abs(getHeight(parent->leftChild) - getHeight(parent->rightChild)) < 2;
  20. }
  21. Node* rotate_LL(Node* parent)
  22. {
  23. Node* child = parent->leftChild;
  24. parent->leftChild = child->rightChild;
  25. child->rightChild = parent;
  26. parent->height = max(getHeight(parent->leftChild),getHeight(parent->rightChild)) + 1;
  27. child->height = max(getHeight(child->leftChild),getHeight(child->rightChild)) + 1;
  28. return child;
  29. }
  30. Node* rotate_RR(Node* parent)
  31. {
  32. Node* child = parent->rightChild;
  33. parent->rightChild = child->leftChild;
  34. child->leftChild = parent;
  35. parent->height = max(getHeight(parent->leftChild),getHeight(parent->rightChild)) + 1;
  36. child->height = max(getHeight(child->leftChild),getHeight(child->rightChild)) + 1;
  37. return child;
  38. }
  39. Node* rotate_LR(Node* parent)
  40. {
  41. Node* child = parent->leftChild;
  42. parent->leftChild = rotate_RR(child);
  43. return rotate_LL(parent);
  44. }
  45. Node* rotate_RL(Node* parent)
  46. {
  47. Node* child = parent->rightChild;
  48. parent->rightChild = rotate_LL(child);
  49. return rotate_RR(parent);
  50. }
  51. Node* InsertNode(Node* root,int newValue)
  52. {
  53. if(root!=NULL)
  54. {
  55. if(newValue > root->value) //R
  56. {
  57. root->rightChild = InsertNode(root->rightChild,newValue);
  58. if(!isBalanced(root))
  59. {
  60. if(newValue > root->rightChild->value) //R-R
  61. {
  62. root = rotate_RR(root);
  63. }else //R-L
  64. {
  65. root = rotate_RL(root);
  66. }
  67. }
  68. }else //L
  69. {
  70. root->leftChild = InsertNode(root->leftChild,newValue);
  71. if(!isBalanced(root))
  72. {
  73. if(newValue > root->leftChild->value) //L-R
  74. {
  75. root = rotate_LR(root);
  76. }else //L-L
  77. {
  78. root = rotate_LL(root);
  79. }
  80. }
  81. }
  82. }else
  83. {
  84. root = new Node(newValue);
  85. }
  86. root->height = max(getHeight(root->leftChild),getHeight(root->rightChild)) + 1;
  87. return root;
  88. }
  89. void PrintTree(Node* root)
  90. {
  91. if(root != NULL)
  92. {
  93. cout<<root->value<<"--";
  94. PrintTree(root->leftChild);
  95. PrintTree(root->rightChild);
  96. }
  97. }
  98. int main()
  99. {
  100. int n;
  101. cin>>n;
  102. Node *root = NULL;
  103. int x;
  104. int i;
  105. for(i=0;i<n;i++)
  106. {
  107. cin>>x;
  108. root = InsertNode(root,x);
  109. }
  110. cout<<root->value<<endl;
  111. system("PAUSE");
  112. return 0;
  113. }

转载于:https://www.cnblogs.com/zzandliz/p/5023210.html

你可能感兴趣的文章
关于堆栈的讲解(一)
查看>>
拓展中国剩余定理(不互质的情况)
查看>>
C# ASP.NET 读取EXCEL 单元格 读取 空值 不显示
查看>>
R语言学习-sep和rep
查看>>
PLC串口通讯时报运算错误
查看>>
joomla
查看>>
LeetCode——Longest Consecutive Sequence
查看>>
网页只能在微信内置浏览器中访问
查看>>
Windows Phone 7 优秀开源项目收集
查看>>
白盒交换机NOS列表(picos/SnapRoute/ONL)
查看>>
微信小程序设置控件权重
查看>>
第六单元 用户、群组和权限
查看>>
UITableView的UITableViewStyleGrouped
查看>>
[翻译] GCDiscreetNotificationView
查看>>
用TableView写带特效的cell
查看>>
Spring AOP实战例子与springmvc整合不起效果的解决办法
查看>>
C++之重载
查看>>
z-index用法
查看>>
Oracle数据库入门到高薪培训教程(从Oracle 11g 到 Oracle 19c)
查看>>
Python day 7(2) 类和实例(1)
查看>>