[Data structures]Balance binary search tree (AVL)
#include <stdio.h> #include <stdlib.h> typedef struct _BinaryTree{ int key; int bf; struct _BinaryTree *left; struct _BinaryTree *right; }BinaryTree; void R_Rotate(BinaryTree **T) { BinaryTree *lc; lc = (*T)->left; (*T)->left = lc->right; lc->right = (*T); (*T) = lc; } void L_Rotate(BinaryTree **T) { BinaryTree *lc; lc = (*T)->right; (*T)->right = lc->left; lc->left = (*T); (*T) = lc; } void LeftBalance(BinaryTree **T) { BinaryTree *lc = (*T)->left; BinaryTree *rd = NULL; switch(lc->bf){ case 1: (*T)->bf = lc->bf = 0; R_Rotate(T); break; case -1: rd = lc->right; switch(rd->bf){ case 1: (*T)->bf = -1; lc->bf = 0; break; case 0: (*T)->bf = lc->bf = 0; break; case -1: (*T)->bf = 0; lc->bf = 1; break; } rd->bf = 0; L_Rotate(&(*T)->left); R_Rotate(T); break; } } void RightBalance(BinaryTree **T) { BinaryTree *rc = (*T)->right; BinaryTree *ld = NULL; switch(rc->bf){ case 1: ld = rc->left; switch(ld->bf){ case 1: (*T)->bf = 0; rc->bf = -1; break; case 0: (*T)->bf = rc->bf = 0; break; case -1: (*T)->bf = 1; rc->bf = 0; break; } ld->bf = 0; R_Rotate(&(*T)->right); L_Rotate(T); break; case -1: (*T)->bf = rc->bf = 0; L_Rotate(T); break; } } int InsertAVL(BinaryTree **T, int key, int *taller) { if(*T == NULL){ (*T) = (BinaryTree *)malloc(sizeof(BinaryTree)); (*T)->key = key; (*T)->left = (*T)->right = NULL; (*T)->bf = 0; *taller = 1; return 1; }else if((*T)->key == key){ *taller = 0; return 0; }else if( key < (*T)->key){ if( !InsertAVL( &(*T)->left, key, taller) ){ return 0; } if(*taller){ switch((*T)->bf){ case 1: LeftBalance(T); *taller = 0; break; case 0: (*T)->bf = 1; *taller = 1; break; case -1: (*T)->bf = 0; *taller = 0; break; } } }else{ if( !InsertAVL( &(*T)->right, key, taller) ){ return 0; } if(*taller){ switch((*T)->bf){ case 1: (*T)->bf = 0; *taller = 0; break; case 0: (*T)->bf = -1; *taller = 1; break; case -1: RightBalance(T); *taller = 0; break; } } } } void printTree(BinaryTree *T){ if(T == NULL){ return; } printf(" %d", T->key ); printf("("); if(T->left != NULL){ printTree(T->left); } printf(","); if(T->right != NULL){ printTree(T->right); } printf(")"); } void main() { int seq[] = { 0, 13, 24, 37, 90, 53 }; int n = sizeof(seq)/sizeof(int); int i; BinaryTree *T = NULL; int taller; printf("Original data:\n"); for (i=0; i<n; i++) { printf("%d\t", seq[i]); } printf("\n"); printf("Insert balance binary search tree:\n"); for(i=1;i<n;i++){ InsertAVL(&T, seq[i], &taller); } printf("\n"); printf("Print the tree:\n"); printTree(T); printf("\n"); }
日历
最新微语
- 有的时候,会站在分叉路口,不知道向左还是右
2023-12-26 15:34
- 繁花乱开,鸟雀逐风。心自宁静,纷扰不闻。
2023-03-14 09:56
- 对于不可控的事,我们保持乐观,对于可控的事情,我们保持谨慎。
2023-02-09 11:03
- 小时候,
暑假意味着无忧无虑地玩很长一段时间,
节假意味着好吃好喝还有很多长期不见的小朋友来玩...
长大后,
这是女儿第一个暑假,
一个半月...
2022-07-11 08:54
- Watching the autumn leaves falling as you grow older together
2018-10-25 09:45
分类
最新评论
- Goonog
i get it now :) - 萧
@Fluzak:The web host... - Fluzak
Nice blog here! Also... - Albertarive
In my opinion you co... - ChesterHep
What does it plan? - ChesterHep
No, opposite. - mojoheadz
Everything is OK!... - Josephmaigh
I just want to say t... - ChesterHep
What good topic - AnthonyBub
Certainly, never it ...
发表评论: