【数据结构】AVL树的旋转和插入

前端之家收集整理的这篇文章主要介绍了【数据结构】AVL树的旋转和插入前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

AVL树

左单旋


代码实现

  1. void _RotateL(Node* parent)
  2. {
  3. Node* subR=parent->_right;
  4. Node* subRL=subR->_left;
  5. Node* ppNode=parent->_parent;
  6. subR->_left=parent;
  7. parent->_right=subRL;
  8. if(subRL)
  9. subRL->_parent=parent;
  10. if(ppNode==NULL)
  11. {
  12. _root=subR;
  13. subR->_parent=NULL;
  14. }
  15. else if(ppNode->_left==parent)
  16. {
  17. ppNode->_left=subR;
  18. subR->_parent=ppNode;
  19. }
  20. else//(ppNode->_right==parent)
  21. {
  22. ppNode->_right=subR;
  23. subR->_parent=ppNode;
  24. }
  25. subR->_bf=parent->_bf=0;
  26. }


右单旋


实现

  1. void _RotateR(Node* parent)
  2. {
  3. Node* subL=parent->_left;
  4. Node* subLR=subL->_right;
  5. Node* ppNode=parent->_parent;
  6. subL->_right=parent;
  7. parent->_left=subLR;
  8. if(subLR)
  9. subLR->_parent=parent;
  10. if(ppNode==NULL)
  11. {
  12. _root=subL;
  13. subL->_parent==NULL;
  14. }
  15. else if(ppNode->_left==parent)
  16. {
  17. subL->_parent=ppNode;
  18. ppNode->_left=subL;
  19. }
  20. else if (ppNode->_right==parent)
  21. {
  22. subL->_parent=ppNode;
  23. ppNode->_right=subL;
  24. }
  25. subL->_bf=parent->_bf=0;
  26. }


左右旋


实现

  1. void _RotateLR(Node* parent)
  2. {
  3.  
  4. Node* subL=parent->_left;
  5. Node* subLR=subL->_right;
  6. int bf=subLR->_bf;
  7. _RotateL(parent->_left);
  8. _RotateR(parent);
  9. if (bf==0)
  10. {
  11. subL->_bf=subLR->_bf=parent->_bf=0;
  12. }
  13. else if (bf==-1)
  14. {
  15. parent->_bf=1;
  16. subLR->_bf=subL->_bf=0;
  17. }
  18. else
  19. {
  20. subL->_bf=-1;
  21. subLR->_bf=parent->_bf=0;
  22. }
  23. }


右左旋


实现

  1. void _RotateRL(Node* parent)
  2. {
  3. Node* subR=parent->_right;
  4. Node* subRL=subR->_left;
  5. int bf=subRL->_bf;
  6. _RotateR(parent->_right);
  7. _RotateRL(parent);
  8. if(bf==0)
  9. {
  10. subR->_bf=parent->_bf=0;
  11. }
  12. else if(bf==-1)
  13. {
  14. parent->_bf=subR->_bf=0;
  15. subRL->_bf=1;
  16. }
  17. else
  18. {
  19. parent->_bf=-1;
  20. subR->_bf=subRL->_bf=0;
  21. }
  22. }

两个特殊测试用例



解决这两个特殊用例的部分代码(具体实现在插入函数

  1. else
  2. {
  3. if(parent->_bf == 2)
  4. {
  5. if(cur->_bf == 1)
  6. {
  7. _RotateL(parent);
  8. }
  9. else if(cur->_bf==-2)
  10. {
  11. _RotateRL(parent);
  12. }
  13. }
  14. else if(parent->_bf==-2)
  15. {
  16. if(cur->_bf == -1)
  17. {
  18. _RotateR(parent);
  19. }
  20. else if(cur->_bf==2)
  21. {
  22. _RotateLR(parent);
  23. }
  24. }

插入函数
  1. bool Insert(const K& key,const V& value)
  2. {
  3. Node* newNode=new Node(key,value);
  4. if(_root==NULL)
  5. {
  6. _root=newNode;
  7. return true;
  8. }
  9. Node* parent=NULL;
  10. Node* cur=_root;
  11. while(cur)
  12. {
  13. if(cur->_key>key)
  14. {
  15. parent=cur;
  16. cur=cur->_left;
  17. }
  18. else if(cur->_key<key)
  19. {
  20. parent=cur;
  21. cur=cur->_right;
  22. }
  23. else
  24. return false;
  25. }
  26. cur=new Node(key,value);
  27. cur->_parent=parent;
  28. if(parent->_key>key)
  29. parent->_left=cur;
  30. else if(parent->_key<key)
  31. parent->_right=cur;
  32. while (parent)
  33. {
  34. if(parent->_left==cur)
  35. {
  36. --parent->_bf;
  37. }
  38. else if(parent->_right==cur)
  39. {
  40. ++parent->_bf;
  41. }
  42. if(parent->_bf==0)
  43. {
  44. return true;
  45. }
  46. if(abs(parent->_bf)==1)
  47. {
  48. cur=parent;
  49. parent=parent->_parent;
  50. }
  51.  
  52. else
  53. {
  54. if(parent->_bf == 2)
  55. {
  56. if(cur->_bf == 1)
  57. {
  58. _RotateL(parent);
  59. }
  60. else if(cur->_bf==-2)
  61. {
  62. _RotateRL(parent);
  63. }
  64. }
  65. else if(parent->_bf==-2)
  66. {
  67. if(cur->_bf == -1)
  68. {
  69. _RotateR(parent);
  70. }
  71. else if(cur->_bf==2)
  72. {
  73. _RotateLR(parent);
  74. }
  75. }
  76. else
  77. return true;
  78. }
  79. }
  80. return true;
  81. }

全部代码

AVLtree.h

  1. #include<iostream>
  2. using namespace std;
  3. template<class K,class V>
  4. struct AVLtreeNode
  5. {
  6. AVLtreeNode<K,V>* _left;
  7. AVLtreeNode<K,V>* _right;
  8. AVLtreeNode<K,V>* _parent;
  9. K _key;
  10. V _value;
  11. int _bf;
  12. AVLtreeNode(const K& key,const V& value)
  13. :_left(NULL),_right(NULL),_parent(NULL),_key(key),_value(value),_bf(0)
  14. {}
  15. };
  16. template<class K,class V>
  17. class AVLtree
  18. {
  19. typedef AVLtreeNode<K,V> Node;
  20. public:
  21. AVLtree()
  22. :_root(NULL)
  23. {}
  24. ~AVLtree()
  25. {}
  26. bool Insert(const K& key,value);
  27. cur->_parent=parent;
  28. if(parent->_key>key)
  29. parent->_left=cur;
  30. else if(parent->_key<key)
  31. parent->_right=cur;
  32. while (parent)
  33. {
  34. if(parent->_left==cur)
  35. {
  36. --parent->_bf;
  37. }
  38. else if(parent->_right==cur)
  39. {
  40. ++parent->_bf;
  41. }
  42. if(parent->_bf==0)
  43. {
  44. return true;
  45. }
  46. if(abs(parent->_bf)==1)
  47. {
  48. cur=parent;
  49. parent=parent->_parent;
  50. }
  51.  
  52. else
  53. {
  54. if(parent->_bf == 2)
  55. {
  56. if(cur->_bf == 1)
  57. {
  58. _RotateL(parent);
  59. }
  60. else if(cur->_bf==-2)
  61. {
  62. _RotateRL(parent);
  63. }
  64. }
  65. else if(parent->_bf==-2)
  66. {
  67. if(cur->_bf == -1)
  68. {
  69. _RotateR(parent);
  70. }
  71. else if(cur->_bf==2)
  72. {
  73. _RotateLR(parent);
  74. }
  75. }
  76. else
  77. return true;
  78. }
  79. }
  80. return true;
  81. }
  82. void Inorder()
  83. {
  84. _Inorder(_root);
  85. cout<<endl;
  86. }
  87. int Height()
  88. {
  89. return _Height(_root);
  90. }
  91. bool IsBalance()
  92. {
  93. return _IsBalance(_root);
  94. }
  95. bool IsBalanceOP()
  96. {
  97. int Height=0;
  98. return _IsBalanceOP(_root,Height);
  99. }
  100.  
  101. protected:
  102. void _Inorder(Node* root)
  103. {
  104. if(root==NULL)
  105. return;
  106. _Inorder(root->_left);
  107. cout<<" KEY: "<<root->_key<<" VALUE: "<<root->_value<<" BF: "<<root->_bf<<endl;
  108. _Inorder(root->_right);
  109. }
  110. int _Height(Node* root)
  111. {
  112. if(root==NULL)
  113. return 0;
  114. int left=_Height(root->_left)+1;
  115. int right=_Height(root->_right)+1;
  116. return left>right ? left:right;
  117. }
  118. bool _IsBalance(Node* parent)
  119. {
  120. if(parent==NULL)
  121. return true;
  122. int bf=_Height(parent->_right)-_Height(parent->_left);
  123. if(parent->_bf!=bf)
  124. {
  125. cout<<"不是平衡树"<<parent->_key<<endl;
  126. return false;
  127. }
  128. return _IsBalance(parent->_left);
  129. return _IsBalance(parent->_right);
  130. }
  131. bool _IsBalanceOP(Node* root,int height)
  132. {
  133. if (root==NULL)
  134. {
  135. height=0;
  136. return true;
  137. }
  138. int leftHeight=0;
  139. if(_IsBalanceOP(root->_left,leftHeight)==false)
  140. return false;
  141. int rightHeigt=0;
  142. if(_IsBalanceOP(root->_right,rightHeigt)==false)
  143. return false;
  144. return true;
  145. }
  146. void _RotateL(Node* parent)
  147. {
  148. Node* subR=parent->_right;
  149. Node* subRL=subR->_left;
  150. Node* ppNode=parent->_parent;
  151. subR->_left=parent;
  152. parent->_right=subRL;
  153. if(subRL)
  154. subRL->_parent=parent;
  155. if(ppNode==NULL)
  156. {
  157. _root=subR;
  158. subR->_parent=NULL;
  159. }
  160. else if(ppNode->_left==parent)
  161. {
  162. ppNode->_left=subR;
  163. subR->_parent=ppNode;
  164. }
  165. else//(ppNode->_right==parent)
  166. {
  167. ppNode->_right=subR;
  168. subR->_parent=ppNode;
  169. }
  170. subR->_bf=parent->_bf=0;
  171. }
  172. void _RotateR(Node* parent)
  173. {
  174. Node* subL=parent->_left;
  175. Node* subLR=subL->_right;
  176. Node* ppNode=parent->_parent;
  177. subL->_right=parent;
  178. parent->_left=subLR;
  179. if(subLR)
  180. subLR->_parent=parent;
  181. if(ppNode==NULL)
  182. {
  183. _root=subL;
  184. subL->_parent==NULL;
  185. }
  186. else if(ppNode->_left==parent)
  187. {
  188. subL->_parent=ppNode;
  189. ppNode->_left=subL;
  190. }
  191. else if (ppNode->_right==parent)
  192. {
  193. subL->_parent=ppNode;
  194. ppNode->_right=subL;
  195. }
  196. subL->_bf=parent->_bf=0;
  197. }
  198. void _RotateLR(Node* parent)
  199. {
  200.  
  201. Node* subL=parent->_left;
  202. Node* subLR=subL->_right;
  203. int bf=subLR->_bf;
  204. _RotateL(parent->_left);
  205. _RotateR(parent);
  206. if (bf==0)
  207. {
  208. subL->_bf=subLR->_bf=parent->_bf=0;
  209. }
  210. else if (bf==-1)
  211. {
  212. parent->_bf=1;
  213. subLR->_bf=subL->_bf=0;
  214. }
  215. else
  216. {
  217. subL->_bf=-1;
  218. subLR->_bf=parent->_bf=0;
  219. }
  220. }
  221. void _RotateRL(Node* parent)
  222. {
  223. Node* subR=parent->_right;
  224. Node* subRL=subR->_left;
  225. int bf=subRL->_bf;
  226. _RotateR(parent->_right);
  227. _RotateRL(parent);
  228. if(bf==0)
  229. {
  230. subR->_bf=parent->_bf=0;
  231. }
  232. else if(bf==-1)
  233. {
  234. parent->_bf=subR->_bf=0;
  235. subRL->_bf=1;
  236. }
  237. else
  238. {
  239. parent->_bf=-1;
  240. subR->_bf=subRL->_bf=0;
  241. }
  242. }
  243. private:
  244. Node* _root;
  245. };
  246. void TestAVLtree1()
  247. {
  248. AVLtree<int,int> At;
  249. At.Insert(0,1);
  250. At.Insert(1,1);
  251. At.Insert(2,1);
  252. At.Insert(3,1);
  253. At.Insert(4,1);
  254. At.Insert(5,1);
  255. At.Insert(6,1);
  256. At.Insert(7,1);
  257. At.Insert(8,1);
  258. At.Insert(9,1);
  259. At.Insert(10,1);
  260. At.Insert(11,1);
  261. At.Inorder();
  262. cout<<At.IsBalance()<<endl;
  263.  
  264. }
  265. void TestAVLtree2()
  266. {
  267. AVLtree<int,int> At;
  268. At.Insert(4,1);
  269. At.Insert(15,1);
  270. At.Insert(16,1);
  271. At.Inorder();
  272. cout<<At.IsBalance()<<endl;
  273.  
  274. }
  275. void TestAVLtree3()
  276. {
  277.  
  278. AVLtree<int,int> At;
  279. At.Insert(16,1);
  280. At.Insert(26,1);
  281. At.Insert(18,1);
  282. At.Insert(14,1);
  283. At.Inorder();
  284. cout<<At.IsBalanceOP()<<endl;
  285.  
  286. }

main.cpp
  1. #include "AVLtree.h"
  2. #include <cstdlib>
  3. int main()
  4. {
  5. TestAVLtree1();
  6. TestAVLtree2();
  7. TestAVLtree3();
  8. system("pause");
  9. return 0;
  10. }

猜你在找的数据结构相关文章