AVL树,平衡二叉树的LL,LR ,RR,RL旋转

//
创建平衡二叉树的调整
@xomlee    20131227  

#include <stdio.h>
#include <Windows.h>

typedef struct AVLTree
{
 int data;
 struct AVLTree *LTree;
 struct AVLTree *RTree;
 struct AVLTree *parent;
}AVLTree;

AVLTree * ROOT = NULL;
//
创建平衡二叉树
void InsertBi_Tree(AVLTree **tree,int nInsertValue)
{
 if (*tree == NULL)    //  判断 NULL 树
 {
  (*tree) = (AVLTree*)malloc(sizeof(AVLTree));
  (*tree) ->data = nInsertValue;
  (*tree) ->LTree = NULL;
  (*tree) ->RTree = NULL;
  (*tree) ->parent = NULL;
  return;
 }

 AVLTree * temp = (*tree);

 while(true)
 {
  if(temp ->data > nInsertValue)  //  放在左边
  {
   if (temp ->LTree == NULL)
   {
    temp ->LTree= (AVLTree*)malloc(sizeof(AVLTree));
    temp ->LTree ->parent = temp;
    temp ->LTree ->data = nInsertValue;
    temp ->LTree ->LTree = NULL;
    temp ->LTree ->RTree = NULL;
    break;
   }
   else
   {
    temp = temp ->LTree;
   }
  } 
  else
  { 
   if (temp->RTree == NULL)
   {
    temp ->RTree = (AVLTree*)malloc(sizeof(AVLTree));
    temp ->RTree ->parent = temp;
    temp->RTree->data= nInsertValue;
    temp->RTree->LTree = NULL;
    temp->RTree->RTree = NULL;
    break;
   }
   else
   {
    temp = temp->RTree;
   }
  }
 }
}

//平衡二叉树的调整=========LL && LR=============================
//判断是左不平衡还是右不平衡,将不平衡的结点传进去,调整完返回传进来的子树

AVLTree * changeBiTreeWeight_r(AVLTree *pTree)
{
 AVLTree *LchildTree ;
 LchildTree = pTree ->LTree;//标记传进来的结点的左孩子

 pTree ->LTree = LchildTree ->RTree;//改变根节点的左孩子
 LchildTree ->RTree = pTree;//调整左孩子的右子树

 if(NULL == pTree ->parent)
 {
  pTree = LchildTree;
 }
 else if(pTree ->parent ->LTree == pTree)
 {
  pTree ->parent ->LTree = LchildTree ;//改变父亲节点

 }
 else
 {
  pTree ->parent ->RTree = LchildTree;
 }

 LchildTree ->parent = pTree ->parent;
 pTree ->parent = LchildTree;

 if(pTree ->LTree)
 {
  pTree ->LTree ->parent = pTree;
 }
 return LchildTree;
}


AVLTree * changeBiTreeWeight_l(AVLTree *pTree)
{
 AVLTree *RchildTree ;
 RchildTree = pTree ->RTree;//标记传进来的结点的you孩子

 pTree ->RTree = RchildTree ->LTree;//改变根节点的you孩子
 RchildTree ->LTree = pTree;//调整you孩子的zuo子树

 if(NULL == pTree ->parent)
 {
  pTree = RchildTree;
 }
 else if(pTree ->parent ->RTree == pTree)
 {
  pTree ->parent ->RTree = RchildTree ;//改变父亲节点

 }
 else
 {
  pTree ->parent ->LTree = RchildTree;
 }

 RchildTree ->parent = pTree ->parent;
 pTree ->parent = RchildTree;

 if(pTree ->RTree)
 {
  pTree ->RTree ->parent = pTree;
 }
 return RchildTree;
}

AVLTree * createBitree(AVLTree *tree)
{

 InsertBi_Tree(&tree,4);
 InsertBi_Tree(&tree,2);
 InsertBi_Tree(&tree,5);
 InsertBi_Tree(&tree,1);
 InsertBi_Tree(&tree,3);

 return tree;
}

void MyPrint(AVLTree *pTree)
{
 if(pTree)
 {
  MyPrint(pTree ->LTree);
  printf("%d ",pTree ->data);
  MyPrint(pTree ->RTree);
 }
}

int main()
{
 AVLTree * root = NULL;
 root = createBitree(root);
 MyPrint(root);
 printf("\n======================\n");

 root = changeBiTreeWeight_l(root);
 MyPrint(root);
 printf("\n======================\n");

 //root = changeBiTreeWeight_r(root);
 //MyPrint(root);
 return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值