平衡二叉树——Balance Binary Sort Tree 设计与实现

骑猪看日落 2022-01-06 15:21 185阅读 0赞

平衡二叉树(Balanced Binary Tree)又被称为AVL树

平衡二叉树(Balanced Binary Tree)又被称为AVL树(有别于AVL算法),且具有以下性质:

它是一棵空树 或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。

构造与调整方法

平衡二叉树的常用算法有红黑树、AVL、Treap、伸展树等。

本程序采用AVL方案进行平衡树的构建。

在AVL中任何节点的两个儿子子树的高度最大差别为一,所以它也被称为高度平衡树。查找、插入和删除在平均和最坏情况下都是O(log n)。

增加和删除可能需要通过一次或多次树旋转来重新平衡这个树。由于这个性质,导致整个树的代价要比红黑树大,所以在STL的一些地方,如map/set都是使用红黑树实现的,所以也就知道了map/set这两个模板的各种代价了。

平衡的树,大多数操作——增删改查,都是O(logn)的,其中红黑树对比AVL树的优势也仅仅在于插入删除这两个操作红黑树会有一个常数级别的优势。

下面是实现的代码,转载请注明出处。

ContractedBlock.gif ExpandedBlockStart.gif

  1. 1 #define LENGTH 5
  2. 2 #include <iostream>
  3. 3 #include <vector>
  4. 4 #include <windows.h>
  5. 5 #include <cmath>
  6. 6 using namespace std;
  7. 7 class Balanced_Binary_Tree{
  8. 8 private:
  9. 9 int infor;
  10. 10 int balance_index;
  11. 11 Balanced_Binary_Tree *left;
  12. 12 Balanced_Binary_Tree *right;
  13. 13 Balanced_Binary_Tree *father;
  14. 14 public:
  15. 15 friend int get_height(Balanced_Binary_Tree*);
  16. 16 friend void print_tree(Balanced_Binary_Tree*);
  17. 17 friend Balanced_Binary_Tree* exist(int);
  18. 18 friend Balanced_Binary_Tree* get_max(Balanced_Binary_Tree*);
  19. 19 friend Balanced_Binary_Tree* get_min(Balanced_Binary_Tree*);
  20. 20 friend Balanced_Binary_Tree* search(int);
  21. 21 friend void insert_node(int);
  22. 22 friend void delete_node(int);
  23. 23 friend void rotate(Balanced_Binary_Tree*);
  24. 24 friend int max_distance(Balanced_Binary_Tree*);// a function for count the max distance between any two node in a BT
  25. 25 friend void Balanced_Binary_Tree_Test();
  26. 26 Balanced_Binary_Tree(int a){
  27. 27 infor=a;
  28. 28 left=NULL;
  29. 29 right=NULL;
  30. 30 father=NULL;
  31. 31 Set_Balance_Index();
  32. 32 }
  33. 33 Balanced_Binary_Tree(){
  34. 34 left=NULL;
  35. 35 right=NULL;
  36. 36 father=NULL;
  37. 37 infor = -1;
  38. 38 Set_Balance_Index();
  39. 39 }
  40. 40 void Set_Balance_Index(){
  41. 41 balance_index = get_height(left) - get_height(right);
  42. 42 }
  43. 43 ~Balanced_Binary_Tree(){cout<<"Delete "<<infor<<" !"<<endl;}
  44. 44 void print(){
  45. 45 cout<<infor;
  46. 46 }
  47. 47 };
  48. 48 static Balanced_Binary_Tree* Start = new Balanced_Binary_Tree(-2147483648);
  49. 49 int get_height(Balanced_Binary_Tree* t)
  50. 50 {
  51. 51 if(t == NULL)
  52. 52 return 0;
  53. 53 else{
  54. 54 int leftH=get_height((*t).left);
  55. 55 int rightH=get_height((*t).right);
  56. 56 return (leftH > rightH)?(leftH+1):(rightH+1);
  57. 57 }
  58. 58 }
  59. 59 //返回这个节点的高度
  60. 60 int max_distance(Balanced_Binary_Tree* root){
  61. 61 if(root == NULL)return 0;
  62. 62 else{
  63. 63 int distance;
  64. 64 distance = get_height((*root).left)+get_height((*root).right);
  65. 65 return distance;
  66. 66 }
  67. 67 }
  68. 68 //返回整个树的最大距离
  69. 69 Balanced_Binary_Tree* exist(int a){
  70. 70 if((*Start).right == NULL)
  71. 71 {
  72. 72 return NULL;
  73. 73 }
  74. 74 else{
  75. 75 Balanced_Binary_Tree *t = new Balanced_Binary_Tree();
  76. 76 t = (*Start).right;
  77. 77 while(t!=NULL){
  78. 78 if(a<(*t).infor){
  79. 79 t=(*t).left;
  80. 80 }
  81. 81 else if(a>(*t).infor){
  82. 82 t=(*t).right;
  83. 83 }
  84. 84 else if(a == (*t).infor)
  85. 85 break;
  86. 86 }//while
  87. 87 return t;
  88. 88 }//else
  89. 89 }//
  90. 90 //exist 判断一个数据是否在排序树上,找到返回引用,未找到返回NULL;这是用在对数据进行判定的
  91. 91 Balanced_Binary_Tree* search(int a){
  92. 92 if(exist(a)==NULL)
  93. 93 {
  94. 94 if((*Start).right == NULL)
  95. 95 {
  96. 96 return Start;
  97. 97 }
  98. 98 else{
  99. 99 Balanced_Binary_Tree *t = new Balanced_Binary_Tree();
  100. 100 Balanced_Binary_Tree *q = new Balanced_Binary_Tree();
  101. 101 t = (*Start).right;
  102. 102 while(t!=NULL){
  103. 103 q = t;
  104. 104 if(a<(*t).infor){
  105. 105 t=(*t).left;
  106. 106 }
  107. 107 else{
  108. 108 t=(*t).right;
  109. 109 }
  110. 110 }//while
  111. 111 return q;
  112. 112 }//else
  113. 113 }//if(!exist a)
  114. 114 else return NULL;
  115. 115 }//
  116. 116 //search 查找一个数据应该插入的位置;不存在树时候返回头结点,存在树的时候,返回最后的结点的引用;这用于处理对数据插入过程中的插入位置判断。
  117. 117 void insert_node(int a)
  118. 118 {
  119. 119 if(exist(a)==NULL)
  120. 120 {
  121. 121 Balanced_Binary_Tree *n = new Balanced_Binary_Tree(a);
  122. 122 Balanced_Binary_Tree *t;
  123. 123 t = search(a);
  124. 124 (*n).father = t;
  125. 125 if((*t).infor>a)
  126. 126 { (*t).left=n; }
  127. 127 else
  128. 128 { (*t).right=n; }
  129. 129 // 首先将这个节点插入树上
  130. 130 while(t!=Start){
  131. 131 t->Set_Balance_Index();
  132. 132 if(abs(t->balance_index)>=2)
  133. 133 break;
  134. 134 t = t->father;
  135. 135 }
  136. 136 //判断这个树是不是平衡的
  137. 137 if(t!=Start){
  138. 138 rotate(t);
  139. 139 }//这个子树不平衡
  140. 140 //if(t!=Start)
  141. 141 }//if(exist(a)==NULL)
  142. 142 }
  143. 143 //插入数据,如果数据不存在,就插入
  144. 144 //当平衡的二叉排序树因为插入节点而失去平衡的时候,仅仅需要对最小不平衡子树进行平衡旋转处理。
  145. 145 //因为经过旋转处理的子树深度与之前相同,因此不影响插入路径上所有祖先节点的平衡度。
  146. 146 void rotate(Balanced_Binary_Tree *t){
  147. 147 if(t->balance_index == 2){
  148. 148 t->left->Set_Balance_Index();
  149. 149 if(t->left->balance_index == 1){
  150. 150 Balanced_Binary_Tree *temp;
  151. 151 temp = t->left;
  152. 152 if((t->father->infor)>(t->infor))
  153. 153 t->father->left = temp;
  154. 154 else
  155. 155 t->father->right = temp;
  156. 156 temp->father = t->father;
  157. 157 t->left = temp->right;
  158. 158 if(temp->right != NULL){
  159. 159 temp->right->father = t;
  160. 160 }
  161. 161 temp->right = t;
  162. 162 t->father = temp;
  163. 163 }//if(t->left->balance_index == 1)
  164. 164 else{
  165. 165 Balanced_Binary_Tree *A,*B,*C;
  166. 166 A = t;
  167. 167 B = t->left;
  168. 168 C = B->right;
  169. 169 if((A->father->infor)>(A->infor))
  170. 170 A->father->left = C;
  171. 171 else
  172. 172 A->father->right = C;
  173. 173 C->father = A->father;
  174. 174
  175. 175 A->father = C;
  176. 176 B->father = C;
  177. 177
  178. 178 A->left = C->right;
  179. 179 if(C->right != NULL)
  180. 180 C->right->father = A;
  181. 181
  182. 182 B->right = C->left;
  183. 183 if(C->left != NULL)
  184. 184 C->left->father = B;
  185. 185
  186. 186 C->right = A;
  187. 187 C->left = B;
  188. 188
  189. 189 }//if(t->left->balance_index == -1)
  190. 190 }//if(t->balance_index == 2)
  191. 191 else {
  192. 192 if(t->right->balance_index == -1){
  193. 193 Balanced_Binary_Tree *temp;
  194. 194 temp = t->right;
  195. 195 if((t->father->infor)>(t->infor))
  196. 196 t->father->left = temp;
  197. 197 else
  198. 198 t->father->right = temp;
  199. 199 temp->father = t->father;
  200. 200 t->right = temp->left;
  201. 201 if(temp->left != NULL){
  202. 202 temp->left->father = t;
  203. 203 }
  204. 204 temp->left = t;
  205. 205 t->father = temp;
  206. 206 }//if(t->right->balance_index == -1)
  207. 207 else{
  208. 208 Balanced_Binary_Tree *A,*B,*C;
  209. 209 A = t;
  210. 210 B = t->right;
  211. 211 C = B->left;
  212. 212 if((A->father->infor)>(A->infor))
  213. 213 A->father->left = C;
  214. 214 else
  215. 215 A->father->right = C;
  216. 216 C->father = A->father;
  217. 217
  218. 218 A->father = C;
  219. 219 B->father = C;
  220. 220
  221. 221 A->right = C->left;
  222. 222 if(C->left != NULL)
  223. 223 C->left->father = A;
  224. 224
  225. 225 B->left = C->right;
  226. 226 if(C->right != NULL)
  227. 227 C->right->father = B;
  228. 228
  229. 229 C->right = B;
  230. 230 C->left = A;
  231. 231 }//if(t->right->balance_index == 1)
  232. 232 }
  233. 233 }
  234. 234
  235. 235
  236. 236 Balanced_Binary_Tree* get_min(Balanced_Binary_Tree* a)
  237. 237 {
  238. 238 Balanced_Binary_Tree *t;
  239. 239 t=a;
  240. 240 while((*a).left!=NULL){
  241. 241 t=a;
  242. 242 a=(*a).left;
  243. 243 }
  244. 244 return t;
  245. 245 }
  246. 246 //找到最左节点
  247. 247 Balanced_Binary_Tree* get_max(Balanced_Binary_Tree* a)
  248. 248 {
  249. 249 Balanced_Binary_Tree *t;
  250. 250 while((*a).right!=NULL){
  251. 251 t=a;
  252. 252 a=(*a).right;
  253. 253 }
  254. 254 return t;
  255. 255 }
  256. 256 //找到最右节点
  257. 257 void delete_node(int a){
  258. 258 if(exist(a)!=NULL)
  259. 259 {
  260. 260 Balanced_Binary_Tree *t=exist(a);
  261. 261 if((*t).left==NULL&&(*t).right==NULL)
  262. 262 {
  263. 263 if((*(*t).father).infor>(*t).infor){
  264. 264 (*(*t).father).left = NULL;
  265. 265 }
  266. 266 else (*(*t).father).right = NULL;
  267. 267 }
  268. 268 //删除的节点无子节点
  269. 269 else if((*t).left==NULL||(*t).right==NULL)
  270. 270 {
  271. 271
  272. 272 if((*t).left == NULL)
  273. 273 {
  274. 274 if((*(*t).father).infor>(*t).infor){
  275. 275 (*(*t).father).left = (*t).right;
  276. 276 }
  277. 277 else (*(*t).father).right = (*t).right;
  278. 278 (*(*t).right).father = (*t).father;
  279. 279 }
  280. 280 else{
  281. 281 if((*(*t).father).infor>(*t).infor){
  282. 282 (*(*t).father).left = (*t).left;
  283. 283 }
  284. 284 else (*(*t).father).right = (*t).left;
  285. 285 (*(*t).left).father = (*t).father;
  286. 286 }
  287. 287 }
  288. 288 //删除的节点有一个子节点
  289. 289 else{
  290. 290 if((*(*t).father).infor>(*t).infor){
  291. 291 (*(*t).father).left = (*t).right;
  292. 292 (*(*t).left).father = get_min((*t).right);
  293. 293 Balanced_Binary_Tree *a = get_min((*t).right);
  294. 294 (*a).left = (*t).left;
  295. 295 (*(*t).right).father = (*t).father;
  296. 296 }
  297. 297 else {
  298. 298 (*(*t).father).right = (*t).right;
  299. 299 (*(*t).left).father = get_min((*t).right);
  300. 300 Balanced_Binary_Tree *a = get_min((*t).right);
  301. 301 (*a).left = (*t).left;
  302. 302 (*(*t).right).father = (*t).father;
  303. 303 }
  304. 304 }
  305. 305 //删除的节点有两个子节点
  306. 306 Balanced_Binary_Tree *delete_node = t;
  307. 307 while(t!=Start){
  308. 308 t->Set_Balance_Index();
  309. 309 if(abs(t->balance_index)>=2)
  310. 310 break;
  311. 311 t = t->father;
  312. 312 }
  313. 313 if(t!=Start){
  314. 314 rotate(t);
  315. 315 }
  316. 316 (*delete_node).left =NULL;
  317. 317 (*delete_node).right =NULL;
  318. 318 (*delete_node).father=NULL;
  319. 319 delete delete_node;
  320. 320 //delete t;
  321. 321 }
  322. 322 }
  323. 323 //删除数据,如果数据存在,就删除
  324. 324 void print_void(int k){
  325. 325 while(k--)cout<<" ";
  326. 326 }
  327. 327 //打印k个制表符
  328. 328 void print_tree(Balanced_Binary_Tree* t){
  329. 329 Balanced_Binary_Tree* temp = t;
  330. 330 vector<Balanced_Binary_Tree*> list;
  331. 331 list.push_back(temp);
  332. 332 int k = get_height(t);
  333. 333 while(k--){
  334. 334 int print_length = pow((double)2,(double)k);
  335. 335 int length = list.size();
  336. 336 vector<Balanced_Binary_Tree*>::iterator iter;
  337. 337 iter = list.begin();
  338. 338 int number_son = list.size();
  339. 339 print_void(print_length);
  340. 340 for(int j=0;j<number_son;j++){
  341. 341 if((*iter)->infor == -1)
  342. 342 cout<<"*";
  343. 343 else
  344. 344 (*iter)->print();
  345. 345 if((*iter)->left == NULL) {
  346. 346 t = new Balanced_Binary_Tree();
  347. 347 list.push_back(t);
  348. 348 iter = list.begin()+j;
  349. 349 }
  350. 350 else {
  351. 351 list.push_back((*iter)->left);
  352. 352 iter = list.begin()+j;
  353. 353 }
  354. 354 if((*iter)->right == NULL) {
  355. 355 t = new Balanced_Binary_Tree();
  356. 356 list.push_back(t);
  357. 357 iter = list.begin()+j;
  358. 358 }
  359. 359 else {
  360. 360 list.push_back((*iter)->right);
  361. 361 iter = list.begin()+j;
  362. 362 }
  363. 363 iter++;
  364. 364 print_void(print_length*2);
  365. 365 }
  366. 366 list.erase(list.begin(),list.begin()+number_son);
  367. 367 cout<<endl;
  368. 368 }
  369. 369 }
  370. 370
  371. 371 void Balanced_Binary_Tree_Test()
  372. 372 {
  373. 373
  374. 374 int test[LENGTH] = {
  375. 1,3,5,4,2};
  376. 375 for(int a=0;a<LENGTH;a++){
  377. 376 insert_node(test[a]);
  378. 377 }
  379. 378
  380. 379
  381. 380 //TEST PART
  382. 381 //int a;
  383. 382 //while(cin>>a)insert_node(a);
  384. 383 //TEST PART
  385. 384 char c;
  386. 385 int i;
  387. 386 cout<<"AVL Tree!"<<endl;
  388. 387 cout<<"d——delete a node; "<<endl;
  389. 388 cout<<"g——get the node's information; "<<endl;
  390. 389 cout<<"i——insert a node; "<<endl;
  391. 390 cout<<"l——get max length of the sub_tree;"<<endl;
  392. 391 cout<<"c——clear the screen;"<<endl;
  393. 392 cout<<"else——quit the test;"<<endl;
  394. 393 while(cin>>c>>i){
  395. 394 cout<<"AVL Tree!"<<endl;
  396. 395 cout<<c<<" "<<i<<endl;
  397. 396 cout<<"d——delete a node; "<<endl;
  398. 397 cout<<"g——get the node's information; "<<endl;
  399. 398 cout<<"i——insert a node; "<<endl;
  400. 399 cout<<"l——get max length of the sub_tree;"<<endl;
  401. 400 cout<<"c——clear the screen;"<<endl;
  402. 401 cout<<"else——quit the test;"<<endl;
  403. 402 switch(c){
  404. 403 case 'c':{
  405. 404 system("cls");
  406. 405 break;
  407. 406 }
  408. 407 case 'g':
  409. 408 {
  410. 409 Balanced_Binary_Tree *t = new Balanced_Binary_Tree();
  411. 410 t = exist(i);
  412. 411 if(t==NULL){
  413. 412 cout<<"The node "<<i<<" do not exist!"<<endl;
  414. 413 }
  415. 414 print_tree(Start->right);
  416. 415 break;
  417. 416 }
  418. 417 case 'd':
  419. 418 {
  420. 419 Balanced_Binary_Tree *t = new Balanced_Binary_Tree();
  421. 420 t = exist(i);
  422. 421 if(t==NULL){
  423. 422 cout<<"The node "<<i<<" do not exist!"<<endl;
  424. 423 }
  425. 424 else{
  426. 425 delete_node((*t).infor);
  427. 426 }
  428. 427 print_tree(Start->right);
  429. 428 break;
  430. 429 }
  431. 430 case 'i':
  432. 431 {
  433. 432 Balanced_Binary_Tree *t = new Balanced_Binary_Tree();
  434. 433 t = exist(i);
  435. 434 if(t==NULL){
  436. 435 cout<<"The node "<<i<<" do not exist!"<<endl;
  437. 436 insert_node(i);
  438. 437 }
  439. 438 else cout<<"The node "<<i<<" already exist!"<<endl;
  440. 439 print_tree(Start->right);
  441. 440 break;
  442. 441 }
  443. 442 case 'l':
  444. 443 {
  445. 444 Balanced_Binary_Tree *t = new Balanced_Binary_Tree();
  446. 445 t = exist(i);
  447. 446 if(t==NULL){
  448. 447 cout<<"The node "<<i<<" do not exist!"<<endl;
  449. 448 }
  450. 449 else{
  451. 450 int l = max_distance(t);
  452. 451 print_tree(Start->right);
  453. 452 cout<<"max distance of sub tree "<<i<<" is " <<l<<endl;
  454. 453 }
  455. 454 break;
  456. 455 }
  457. 456 default:break;
  458. 457 }
  459. 458 }
  460. 459 }
  461. 460
  462. 461
  463. 462 int main(void)
  464. 463 {
  465. 464 Balanced_Binary_Tree_Test();
  466. 465 return 0;
  467. 466 }

运行示例:

d——delete a node;
g——get the node’s information;
i——insert a node;
l——get max length of the sub_tree;
c——clear the screen;
else——quit the test;
The node 45 do not exist!
3
1 32
* 2 5 33
* * * * 4 12 * 45
i 66
AVL Tree!
i 66
d——delete a node;
g——get the node’s information;
i——insert a node;
l——get max length of the sub_tree;
c——clear the screen;
else——quit the test;
The node 66 do not exist!
3
1 32
* 2 5 45
* * * * 4 12 33 66
i 80
AVL Tree!
i 80
d——delete a node;
g——get the node’s information;
i——insert a node;
l——get max length of the sub_tree;
c——clear the screen;
else——quit the test;
The node 80 do not exist!
32
3 45
1 5 33 66
* 2 4 12 * * * 80

转载于:https://www.cnblogs.com/wangbiaoneu/archive/2013/04/25/AVL\_Tree.html

发表评论

表情:
评论列表 (有 0 条评论,185人围观)

还没有评论,来说两句吧...

相关阅读