æ ¹æ®ç½ä¸ç代ç æ¹äºä¸ï¼æ³¨éæ¯è¾æ¸
æ°äºï¼åºè¯¥è½ç¬¦åä½ çè¦æ±ï¼ä½ è¯ä¸çã
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/* avlæ æ°æ®ç»æåç¸å
³æä½ */
/*å
åéæ¾*/
#define xfree(p) free(p)
struct AVLTree
/*ç³è¯·å
å*/
inline void *xalloc(int size)
{
void *p;
p = (void *)malloc(size);
/*ç³è¯·å¤±è´¥*/
if(p == NULL)
{
printf("alloc error\n");
exit(1);
}
return p;
}
{
unsigned int nData; /*åå¨æ°æ®*/
struct AVLTree* pLeft; /*æåå·¦åæ */
struct AVLTree* pRight; /*æåå³åæ */
int nHeight; /*æ ç平衡度*/
};
int Max(int a, int b)
{
return (a > b ? a : b);
}
/*è¿åèç¹ç平衡度*/
int Height(struct AVLTree* pNode)
{
if (NULL == pNode)
return -1;
return pNode->nHeight;}
/********************************************************************
pNode pNode->pLeft
/ \
pNode->pLeft ==> pNode
\ /
pNode->pLeft->pRight pNode->pLeft->pRight
*********************************************************************/
struct AVLTree* SingleRotateWithLeft(struct AVLTree* pNode)
{
struct AVLTree* pNode1;
pNode1 = pNode->pLeft;
pNode->pLeft = pNode1->pRight;
pNode1->pRight = pNode;
/*ç»ç¹çä½ç½®åäº, è¦æ´æ°ç»ç¹çé«åº¦å¼*/
pNode->nHeight = Max(Height(pNode->pLeft), Height(pNode->pRight)) + 1;
pNode1->nHeight = Max(Height(pNode1->pLeft), pNode->nHeight) + 1;
return pNode1;}
/********************************************************************
pNode pNode->pRight
\ /
pNode->pRight ==> pNode
/ \
pNode->pRight->pLeft pNode->pRight->pLeft
*********************************************************************/
struct AVLTree* SingleRotateWithRight(struct AVLTree* pNode)
{
struct AVLTree* pNode1;
pNode1 = pNode->pRight;
pNode->pRight = pNode1->pLeft;
pNode1->pLeft = pNode;
/*ç»ç¹çä½ç½®åäº, è¦æ´æ°ç»ç¹çé«åº¦å¼*/
pNode->nHeight = Max(Height(pNode->pLeft), Height(pNode->pRight)) + 1;
pNode1->nHeight = Max(Height(pNode1->pRight), pNode->nHeight) + 1;
return pNode1;
}
struct AVLTree* DoubleRotateWithLeft(struct AVLTree* pNode)
{
pNode->pLeft = SingleRotateWithRight(pNode->pLeft);
return SingleRotateWithLeft(pNode);}
struct AVLTree* DoubleRotateWithRight(struct AVLTree* pNode)
{
pNode->pRight = SingleRotateWithLeft(pNode->pRight);
return SingleRotateWithRight(pNode);}
struct AVLTree* insert_tree(unsigned int nData, struct AVLTree* pNode)
{
if (NULL == pNode)
{
pNode = (struct AVLTree*)xalloc(sizeof(struct AVLTree));
pNode->nData = nData;
pNode->nHeight = 0;
pNode->pLeft = pNode->pRight = NULL;
}
else if (nData < pNode->nData) /*æå
¥å°å·¦åæ ä¸*/
{
pNode->pLeft = insert_tree(nData, pNode->pLeft);
if (Height(pNode->pLeft) - Height(pNode->pRight) == 2) /*AVLæ ä¸å¹³è¡¡*/
{
if (nData < pNode->pLeft->nData)
{
/*æå
¥å°äºå·¦åæ 左边, ååæ转*/
pNode = SingleRotateWithLeft(pNode);
}
else
{
/*æå
¥å°äºå·¦åæ å³è¾¹, ååæ转*/
pNode = DoubleRotateWithLeft(pNode);
}
}
}
else if (nData > pNode->nData) /*æå
¥å°å³åæ ä¸*/
{
pNode->pRight = insert_tree(nData, pNode->pRight);
if (Height(pNode->pRight) - Height(pNode->pLeft) == 2) /*AVLæ ä¸å¹³è¡¡*/
{
if (nData > pNode->pRight->nData)
{
/*æå
¥å°äºå³åæ å³è¾¹, ååæ转*/
pNode = SingleRotateWithRight(pNode);
}
else
{
/*æå
¥å°äºå³åæ 左边, ååæ转*/
pNode = DoubleRotateWithRight(pNode);
}
}
}
pNode->nHeight = Max(Height(pNode->pLeft), Height(pNode->pRight)) + 1;
return pNode;}
/*ä¸åºéåæå°æ çææç»ç¹, å 为左ç»ç¹ < ç¶ç»ç¹ < å³ç»ç¹, å æ¤æå°åºæ¥æ°æ®ç大å°æ¯éå¢ç*/
void print_tree(struct AVLTree* pRoot)
{
if (NULL == pRoot)
return;
static int n = 0;
print_tree(pRoot->pLeft);
//printf("[%d]nData = %u\n", ++n, pRoot->nData);
printf(" %u ", pRoot->nData);
print_tree(pRoot->pRight);
}
/*å
åºéåæå°æ çææç»ç¹ */
void print_tree_first(struct AVLTree* pRoot)
{
if (NULL == pRoot)
return;
static int n = 0;
printf(" %u ", pRoot->nData);
//printf("[%d]nData = %u\n", ++n, pRoot->nData);
print_tree_first(pRoot->pLeft);
print_tree_first(pRoot->pRight);
}
/*å é¤æ */
void delete_tree(struct AVLTree** ppRoot)
{
if (NULL == ppRoot || NULL == *ppRoot)
return;
delete_tree(&((*ppRoot)->pLeft));
delete_tree(&((*ppRoot)->pRight));
xfree(*ppRoot);
*ppRoot = NULL;
}
int main()
{
int i,j;
AVLTree* pRoot = NULL;
printf("平衡äºåæ æ¼ç¤ºï¼è¯·è¾å
¥è¥å¹²ä¸ªæ£æ´æ°ï¼ä»¥ -1 ç»æï¼\n");
j=0;
for (i = 1; j != -1; ++i)
{
printf("\n请è¾å
¥ç¬¬%d个æ°ï¼ -1代表ç»æï¼",i);
scanf("%d", &j);
if(j == -1)break;
pRoot = insert_tree(j, pRoot);
printf("\næå
¥ç¬¬%d个å
ç´ åï¼\nä¸åºéåè¾åºï¼",i );
print_tree(pRoot);
printf("\n å
åºéåè¾åºï¼");
print_tree_first(pRoot);
}
printf("\n åè§ï¼");
delete_tree(&pRoot);
return 0;
}
追é®ä½ 好 è¿å¨ä¹ï¼ä¸è¡å åè¯äºä¸ 没åæ³è¿è¡å
追çä¸å¥½ææè´´ä¸å»çæ¶å代ç éä½äºï¼ä½ æè¿å¥ç»æä½å®ä¹struct AVLTree å ææªå°è¿éï¼
struct AVLTree
{
unsigned int nData; /*åå¨æ°æ®*/
struct AVLTree* pLeft; /*æåå·¦åæ */
struct AVLTree* pRight; /*æåå³åæ */
int nHeight; /*æ ç平衡度*/
};
追é®é¦å
谢谢åããæ¹äºä¸ ç¡®å®å¯ä»¥è¿è¡äº å
åº ä¸åºè¾åºç¡®å®æ²¡é®é¢äº å¯æ¯æ²¡æ³çå°å¹³è¡¡äºåæ åããããæåæ³åï¼