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;
}

    原文作者:平衡二叉树
    原文地址: https://blog.csdn.net/xomlee/article/details/17621177
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞