A balanced binary tree implementation example

  • 2020-04-02 02:06:42
  • OfStack



#include<cstdio>
#include<cstdlib>
#define LH 1
#define EH 0
#define RH -1
using namespace std;
typedef struct BTNode
{
 int data;
 int BF;//Balance factor
 struct BTNode *lchild,*rchild;
}BTNode,*BTree;
void R_Rotate(BTree *p)//The binary sort tree with p as the root node is rotated to the right
{
 BTree L;
 L=(*p)->lchild;
 (*p)->lchild=L->rchild;
 L->rchild=(*p);
 *p=L;//P points to the new root node
}
void L_Rotate(BTree *p)//The binary sort tree with p as root node is rotated to the left
{
 BTree R;
 R=(*p)->rchild;
 (*p)->rchild=R->lchild;
 R->lchild=(*p);
 *p=R;
}
void LeftBalance(BTree *T)
{
 BTree L,Lr;
 L=(*T)->lchild;
 switch(L->BF)
 {
  //Check the left subtree equilibrium degree of T and make the corresponding equilibrium treatment
  case LH://The new node is inserted into the left subtree of the left child of T to do a single dextro
   (*T)->BF=L->BF=EH;
   R_Rotate(T);
   break;
  case RH://The new insertion node is in the right subtree of the left child of T, doing double rotation
   Lr=L->rchild;
   switch(Lr->BF)
   {
    case LH:
     (*T)->BF=RH;
     L->BF=EH;
     break;
    case EH:
     (*T)->BF=L->BF=EH;
     break;
    case RH:
     (*T)->BF=EH;
     L->BF=LH;
     break;
   }
   Lr->BF=EH;
   L_Rotate(&(*T)->lchild);
   R_Rotate(T);
 }
}
void RightBalance(BTree *T)
{
 BTree R,Rl;
 R=(*T)->rchild;
 switch(R->BF)
 {
  case RH://The new node is inserted into the right subtree of the right child of T, and it needs to be single handed
   (*T)->BF=R->BF=EH;
   L_Rotate(T);
   break;
  case LH://The new node is inserted into the left subtree of the right child of T, and it needs to be double-rotated
   Rl=R->lchild;
   switch(Rl->BF)
   {
    case LH:
     (*T)->BF=EH;
     R->BF=RH;
     break;
    case EH:
     (*T)->BF=R->BF=EH;
     break;
    case RH:
     (*T)->BF=LH;
     R->BF=EH;
     break;
   }
   Rl->BF=EH;
   R_Rotate(&(*T)->rchild);
   L_Rotate(T);
 }
}
bool InsertAVL(BTree *T,int e,bool *taller)//The variable taller responds to whether T is taller or not
{
 if(!*T)
 {
  *T=(BTree)malloc(sizeof(BTNode));
  (*T)->data=e;
  (*T)->lchild=(*T)->rchild=NULL;
  (*T)->BF=EH;
  *taller=true;
 }
 else
 {
  if(e==(*T)->data)//Do not insert
  {
   *taller=false;
   return false; 
  }
  if(e<(*T)->data)
  {
   if(!InsertAVL(&(*T)->lchild,e,taller))//Not insert
    return false;
   if(*taller)//To insert the left subtree, and the left subtree gets taller
   {
    switch((*T)->BF)
    {
     case LH://The original left subtree is higher than the right subtree, so we need to do left balance
      LeftBalance(T);
      *taller=false;
      break;
     case EH://Originally about subtree height, now because of the left subtree height and tree height
      (*T)->BF=LH;
      *taller=true;
      break;
     case RH://Originally the right subtree is higher than the left subtree, now the left subtree is equal in height
      (*T)->BF=EH;
      *taller=false;
      break;
    }
   }
  }
  else
  {
   //Search in the right subtree of T
   if(!InsertAVL(&(*T)->rchild,e,taller))
    return false;
   if(*taller)//Insert the right subtree, and the right subtree grows
   {
    switch((*T)->BF)
    {
     case LH://The left subtree was originally higher than the right subtree, but now the left subtree is equal in height
      (*T)->BF=EH;
      *taller=false;
      break;
     case EH://Now the right subtree is higher than the left subtree
      (*T)->BF=RH;
      *taller=true;
      break;
     case RH://The right subtree was taller than the left subtree, but now it needs to be balanced
      RightBalance(T);
      *taller=false;
      break;
    }
   }
  }
 }
 return true;
}
bool Find(BTree T,int key)
{
 if(!T)
  return false;
 else if(T->data==key)
  return true;
 else if(T->data<key)
  return Find(T->rchild,key);
 else
  return Find(T->lchild,key);
}
void Output(BTree T)
{
 if(T)
 {
  printf("%d",T->data);
  if(T->lchild||T->rchild)
  {
   printf("(");
   Output(T->lchild);
   printf(",");
   Output(T->rchild);
   printf(")");
  }
 }
}
int main(int argc,char *argv[])
{
 int i;
 int A[]={3,2,1,4,5,6,7,10,9,8};
 BTree T=NULL;
 bool taller;
 for(i=0;i<sizeof(A)/sizeof(int);i++)
  InsertAVL(&T,A[i],&taller);
 Output(T);
 printf("n");
 if(Find(T,6))
  printf("6 is find in the AVL tree!n");
 else 
  printf("6 is not find in the AVL tree!n");
 return 0;
}


Related articles: