Balanced binary tree (C language)

Source: Internet
Author: User

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)-&GT;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-&GT;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-&GT;BF)/*correction of the balance factor of *t and its left sub-tree*/            {                 CaseLH: (*t)-&GT;BF =RH; L-&GT;BF =EH;  Break;  CaseEH: (*t)-&GT;BF = l->bf=EH;  Break;  CaseRH: (*t)-&GT;BF =EH; L-&GT;BF =LH;  Break; } Lr-&GT;BF =EH; L_rotate (&AMP;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)-&GT;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)-&GT;BF =LH;  Break;  CaseLH: (*t)-&GT;BF =LH; Lr-&GT;BF =EH;  Break; } L-&GT;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)

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: info-contact@alibabacloud.com 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.