小弟学习数据结构的练习:排序二叉树 望各位大虾提出意见 谢谢

xrbeck 2002-01-05 07:16:27
希望各位大虾多多提意见,谢谢了
// ======================================
// Header
# include "iostream.h"
# include "stdio.h"
# include "conio.h"
# include "malloc.h"

typedef struct tagNode
{
int Number;
struct tagNode *lchild,*rchild;

}bitnode,*bitree;

# define LEN sizeof(bitnode)
// ========================================
// 中序遍历二叉树
void inorder(bitree root)
{
if(root!=NULL)
{
inorder(root->lchild); // 遍历左子树
printf("%d\t",root->Number); // 访问根节点
inorder(root->rchild); // 遍历右子树
}
}
//-------------------------------------------
// 为得到从大到小的排列进行的逆中序排列
void Covinorder(bitree root)
{
if(root!=NULL)
{
Covinorder(root->rchild);
printf("%d\t",root->Number);
Covinorder(root->lchild);
}

}
//-------------------------------------------
// 插入节点
void InsertNode(int Num,bitree *root)
{
(*root)=(bitree)malloc(LEN);
(*root)->Number=Num;
(*root)->lchild=NULL;
(*root)->rchild=NULL;

}

//-------------------------------------------
// 查找插入的节点
void SearchNode(int Num,bitree *root)
{

if(*root==NULL)
InsertNode(Num,root);
else
{
if(Num>((*root)->Number))
{
if((*root)->rchild==NULL)
InsertNode(Num,&((*root)->rchild));
else
SearchNode(Num,&((*root)->rchild));
}
else
{
if((*root)->lchild==NULL)
InsertNode(Num,&((*root)->lchild));
else
SearchNode(Num,&((*root)->lchild));
}
}

}

//-------------------------------------------
// 生成排序二叉树
void CreateTree(bitree *root)
{

int Num=0;
printf("Input your first element(999 to quit):\n");
scanf("%d",&Num);

while(Num!=999)
{
SearchNode(Num,root);
scanf("%d",&Num);
} // End of while(Num!=999)

}

void main()
{
bitree root=NULL;
CreateTree(&root);

printf("从大到小(中序排列)为:\n");
inorder(root);
printf("\n");

printf("从大到小(逆中序排列)为:\n");
Covinorder(root);
printf("\n");

getch();

}
...全文
46 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
xrbeck 2002-01-07
  • 打赏
  • 举报
回复
大家帮忙看看。谢谢。
cxjddd 2002-01-05
  • 打赏
  • 举报
回复
不一定要用指针去实现的。用一个数组去表示链表也可以。

33,010

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧