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.

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:
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

 

 
   
  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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值