Data structure---C-language realization of Balanced binary tree (AVL tree)

Source: Internet
Author: User

AVL (auto-balanced binary tree) #include <stdio.h> #include <stdlib.h>typedef int elemtype;//average typedef of each node enum{EH = 0, LH =                              1, RH =-1}bh_t;typedef enum{FALSE = 0, TRUE = 1}bool_t;//define Balanced binary tree typedef struct bstnode{elemtype key;//balance value int bf; struct Bstnode *lchild,*rchild;}  Bstnode, *bstree;//in sequence traversal void Inordertraverse (Bstree root) {if (NULL! = root) {inordertraverse (root->lchild);  printf ("%d\t", Root->key); Inordertraverse (Root->rchild);}}  The pre-order traversal of void Preordertraverse (Bstree root) {if (NULL! = root) {printf ("%d\t", Root->key);  Preordertraverse (Root->lchild); Preordertraverse (Root->rchild); }}//right-handed void R_rotate (Bstree *p) {bstree lc= (*p)->lchild; (*p)->lchild=lc->rchild; lc->rchild=*p; *P=LC;} L-void L_rotate (Bstree *p) {bstree rc= (*p)->rchild; (*p)->rchild=rc->lchild; rc->lchild=*p; *P=RC;} Make left balance void leftbalance (Bstree *t) {bstree lc= (*t)->lchild; Bstree rd = lc->rchild;  Determine which side to rotate switch (LC-&GT;BF) {case LH: (*t)->bf=lc->bf=eh;   R_rotate (T);  Break     Case Rh:switch (RD-&GT;BF) {case LH: (*t)->bf=rh;     lc->bf=eh;    Break     Case EH: (*t)->bf=lc->bf=eh;    Break     Case RH: (*t)->bf=eh;     lc->bf=lh;   Break   } rd->bf=eh;   L_rotate (& (*t)->lchild);   R_rotate (T); Break }}//make Right balance void rightbalance (Bstree *t) {bstree rc= (*t)->rchild; Bstree ld=rc->lchild;   Switch (RC-&GT;BF) {case RH: (*t)->bf=rc->bf=eh;   L_rotate (T);  Break     Case Lh:switch (LD-&GT;BF) {case RH: (*t)->bf=lh;     rc->bf=eh;    Break     Case EH: (*t)->bf=rc->bf=eh;    Break     Case LH: (*t)->bf=eh;     rc->bf=rh;   Break   } ld->bf=eh;   R_rotate (& (*t)->rchild);   L_rotate (T); Break }}//Insert Element bool_t Insertavl (bstree *t,elemtype e,bool_t *taller) {if (null = = t) return FALSE; if (null = = *t) {*t= (Bstree) m  Alloc (sizeof (Bstnode));  if (NULL = = *t) return FALSE;  (*t)->key=e; (*t)->lchild= (*t)->rchild=null;  (*t)->bf=eh; *taller=true;   } else {if (e== (*t)->key) {*taller=false;  return FALSE;   if (e< (*t)->key) {if (false = = Insertavl (& (*t)->lchild), E,taller) return FALSE;      if (*taller) {switch ((*t)->bf) {case lh:leftbalance (t);      *taller=false;     Break      Case EH: (*t)->bf=lh;      *taller=true;     Break      Case RH: (*t)->bf=eh;      *taller=false;    Break   }}} else {if (false = = Insertavl (& (*t)->rchild), E,taller) return FALSE;      if (*taller) {switch ((*t)->bf) {case rh:rightbalance (t);      *taller=false;     Break      Case EH: (*t)->bf=rh;      *taller=true;     Break      Case LH: (*t)->bf=eh;      *taller=false;    Break }}}} return TRUE;  Bstree Searchavl (Bstree t,elemtype key) {Bstree p=t; while (p) {if (P->key==key) return p;  else if (P->key<key) p=p->rchild; else p=p-&Gt;lchild; } return p;}  static void Destroy (Bstree *t) {if (NULL! = *t) {Destroy (& ((*t)->lchild));  Destroy (& (*t)->rchild);  Free (*t); *t = NULL; } return; void Destroyavl (Bstree root) {if (NULL! = root) {destroy (&root);} return;} int main () {Bstree root=null,r; bool_t taller=false; int array[]={13,24,37,90,53}; int i = 0; for (i=0; i < 5; i++) Ins Ertavl (&root,array[i],&taller); printf ("middle order traversal: \ n"); Inordertraverse (root); printf ("\ n-sequence traversal \ n"); Preordertraverse (Root);p rintf ("\ n search: 37\n"); R=searchavl (root,37); if (r) {printf ("%d\n", R->key),} else {printf ("not find!\n");} Destroyavl (Root); root = NULL; return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Data structure---C-language realization of Balanced binary tree (AVL tree)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.