用C语言 编写一个数据结构的程序 平衡二叉树的

功能要求及说明: (1)以-1为输入结束标志,输入数列L,生成一棵平衡二叉
2)每加入一个关键字,对调整后的平衡二叉树,进行中序和先序遍历,输出两种遍历的结果。
希望能有完整代码 及必要注释 百度分数需要的话 我可以追加

根据网上的代码改了下,注释比较清晰了,应该能符合你的要求,你试下看。
#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; /*树的平衡度*/
};

追问

首先 谢谢哈。。改了下 确实可以运行了 先序 中序输出确实没问题了 可是没法看到平衡二叉树啊。。。。有办法吗?

温馨提示:内容为网友见解,仅供参考
第1个回答  2012-10-30
邮箱告我,我发你追问

744640777@qq.com

本回答被提问者采纳
相似回答