Data Structure-balanced binary tree (AVL Tree) in C Language)

Source: Internet
Author: User

Data Structure-balanced binary tree (AVL Tree) in C Language)

// AVL (automatically balanced binary tree)
#include
#include
typedef int ElemType;
// average of each node
typedef enum
{
 EH = 0,
 LH = 1,
 RH = -1
} bh_t;

typedef enum
{
  FALSE = 0,
  TRUE = 1
} bool_t;

// Define a balanced binary tree
typedef struct BSTNode
{
 ElemType key; // balance value
 int bf;
 struct BSTNode * lchild, * rchild;
} BSTNode, * BSTree;


// Middle order traversal
void InOrderTraverse (BSTree root)
{
 if (NULL! = root)
 {
  InOrderTraverse (root-> lchild);
  printf (% d, root-> key);
  InOrderTraverse (root-> rchild);
}
}


// Preface traversal
void PreOrderTraverse (BSTree root)
{
 if (NULL! = root)
{
  printf (% d, root-> key);
  PreOrderTraverse (root-> lchild);
  PreOrderTraverse (root-> rchild);
 }
}


// right turn
void R_Rotate (BSTree * p)
{
 BSTree lc = (* p)-> lchild;
 (* p)-> lchild = lc-> rchild;
 lc-> rchild = * p;
 * p = lc;
}

// left
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;
 // Judge which side to rotate to
switch (lc-> bf)
 {
  case LH:
   (* T)-> bf = lc-> bf = EH;
   R_Rotate (T);
   break;
  case RH:
   switch (rd-> 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-> bf)
 {
  case RH:
   (* T)-> bf = rc-> bf = EH;
   L_Rotate (T);
   break;
  case LH:
   switch (ld-> 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) malloc (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-> keyrchild;
  else
   p = p-> 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 ++)
  InsertAVL (& root, array [i], & taller);
 printf (in-order traversal:
);
 InOrderTraverse (root);
 printf (
Preorder traversal
);
 PreOrderTraverse (root);
printf (
Search: 37
);
 r = searchAVL (root, 37);
 if (r)
 {
  printf (% d
, r-> key);
 }
 else
 {
 printf (not find!
);
 }
 destroyAVL (root);
 root = NULL;
 return 0;
} 
  
 

 

Result:


 


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: 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.