小弟学习数据结构的练习:排序二叉树 望各位大虾提出意见 谢谢
希望各位大虾多多提意见,谢谢了
// ======================================
// 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();
}