I think this is the most understandable version, adapted from the internet, the source forgot, I complete his code, and changed the style to my,, the function really does not like a brain of all throw above,. ,,,。
It is recommended to read these two articles:
Http://www.cnki.net/KCMS/detail/detail.aspx? Queryid=2&currec=1&recid=&filename=gwdt201034126&dbname=cjfdn0911&dbcode=cjfr&pr= CJFR2010; Cfjd2010;&urlid=&yx=&uid= Weevrecwsljhsldra1finlpyaknurfryd0tktukwazllww5tbjyzyxfvv3pdk3pwyjbqn3zznjdkqtlvqkpwn3l3pt0=$9a4hf_ yauvq5obgvaqnkpcycejkensw4iqmovwhtwkf4vypohbkxjw!! &v=mdayotjgeursvzd2sulqclblckc0sdliuhe0nuhzb1i4zvgxthv4wvm3rggxvdnxvhjxttfgcknvukw2zvplzhe=
Http://www.cnblogs.com/fornever/archive/2011/11/15/2249492.html
#include <stdio.h>#include<stdlib.h>#defineEH 0/* Equal height */#defineLH 1/* Left high */#defineRH-1/* Right high */typedefintElemtype;/*Data Type*/typedefstructbitree{elemtype data; /*Data Elements*/ intBF;/*Balance Factor*/ structBitree *lchild,*rchild;/*left and right child hands*/}*Bitree,bitreenode;intINSERTAVL (Bitree *t,elemtype E,BOOL*taller);voidLeftbalance (Bitree *T);voidRightbalance (Bitree *T);voidR_rotate (Bitree *T);voidL_rotate (Bitree *T);BOOL*taller= (BOOL*)malloc(sizeof(BOOL));intMainvoid){ intdata; Bitree T=NULL; while(1) {printf ("Enter the number (zero to exit):"); scanf ("%d",&data); if(0==data) Break; Insertavl (&T,data,taller); } return 0;}/*If there are no nodes with the same key code as E in the balanced two-fork sort tree T, insert a data element of E*//*new node, and reverse back to 1, or reverse back to 0. If the two-fork sorting tree loses its balance due to insertion, the balance rotation is processed,*/ /*Boolean variable taller reflects T-length height or not*/ intINSERTAVL (Bitree *t,elemtype E,BOOL*taller) { if(!*t)/*Insert new node, tree "long height", set taller to Ture*/ { (*t) = (bitree)malloc(sizeof(Bitreenode)); (*t)->data =e; (*t)->lchild = (*t)->rchild =NULL; (*t)->BF =EH; *taller =true; } Else { if(e== (*t)->data)/*There are nodes in the tree that have the same key code as E, without inserting*/ { *taller =false; return 0; } if(e< (*t),data) { if(! Insertavl (& (*t)->lchild,e,taller))return 0;/*not inserted*/ if(*taller)Switch((*t)BF) { CaseEh:/*originally left and right sub-tree, such as high, due to the increase of the left subtree to increase the tree*/ (*T)->bf=LH; *taller=true; Break; CaseLh:/*the left sub-tree is tall and needs to be left balanced.*/leftbalance (T); *taller=false; Break; CaseRh:/*originally right sub-tree high, so that the left and right sub-tree, such as high*/ (*T)->bf=EH; *taller=false; Break; } } Else { if(! Insertavl (& (*t)->rchild,e,taller))return 0;/*not inserted*/ if(*taller)Switch((*t)BF) { CaseEh:/*originally left and right sub-tree, such as high, because the right subtree increased to increase the tree*/ (*T)->bf=RH; *taller=true; Break; CaseLh:/*originally left sub-tree high, so that the left and right sub-tree, such as high*/ (*T)->bf=EH; *taller=false; Break; CaseRh:/*the right sub-tree is tall and needs to be treated with right balance.*/rightbalance (T); *taller=false; Break; } } } return 1;}/*The *p point is the root of the sub-tree, the left balance rotation processing, after processing, the *p points to the new root of the subtree*/voidLeftbalance (Bitree *T) {Bitree L= (*t)->lchild,lr;/*L Point to *t Zogen Junction.*/ Switch(L->BF)/*check L balance and handle accordingly*/ { CaseLh:/*The new node is inserted in the left subtree of the *p Zuozi, and it needs to be rotated by single right .*/ (*T)->bf=l->bf=EH; R_rotate (T); Break; CaseEh:/*originally left and right sub-tree, such as high, due to the increase of the left subtree to increase the tree*/ (*T)->bf=lh;//eh seems to have no need to write here.*taller=true; Break; CaseRh:/*The new node is inserted in the right sub-tree of the left child of *t, and must be left and right double-spin .*/Lr=l->rchild;/*Lr points to *p left child's right subtree node .*/ Switch(LR->BF)/*correction of the balance factor of *t and its left sub-tree*/ { CaseLH: (*t)->BF =RH; L->BF =EH; Break; CaseEH: (*t)->BF = l->bf=EH; Break; CaseRH: (*t)->BF =EH; L->BF =LH; Break; } Lr->BF =EH; L_rotate (&L);/*left rotation of the *t Zuozi*/r_rotate (T); /*right rotation treatment of *t*/ }}//here and leftbalance a truth, try to write your ownvoidRightbalance (Bitree *T) {Bitree Lr= (*t)rchild,l; Switch(lr->BF) { CaseEH:*taller =true; (*t)->BF =RH; Break; CaseRH: (*T)->bf=lr->bf=EH; L_rotate (T); Break; Caselh:l= lr->Lchild; Switch(l->BF) { CaseEH: (*T)->bf=lr->bf=EH; Break; CaseRH:LR->bf=EH; (*t)->BF =LH; Break; CaseLH: (*t)->BF =LH; Lr->BF =EH; Break; } L->BF =EH; R_rotate (&Lr); L_rotate (T); }}/*The sub-tree, which is the root of the node pointed by *t, is processed by the right single rotation, and after processing, the node *t points to is the new root of the subtree.*/voidR_rotate (Bitree *T) {Bitree L= (*t)->lchild;/*L Point to *t Zogen Junction.*/ (*T)->lchild=l->rchild;/*L's right son rime the left subtree of *t.*/L->rchild=*t; *t=l;/**l point to the new root node*/}/*The *p point is the root of the sub-tree, the left single rotation processing, after processing, the *p points to the new root of the subtree*/voidL_rotate (Bitree *T) {Bitree Lr= (*t)->rchild;/*Lr points to the *t right subtree node.*/ (*T)->rchild=lr->lchild;/*L's left child rime to *p right subtree*/Lr->lchild=*T; *T=LR;/**l point to the new root node*/}
Balanced binary tree (C language)