试建立一棵二叉树,并输出以中序遍历此二叉树的结果序列。

来源:百度知道 编辑:UC知道 时间:2024/07/05 01:15:42
要求算法中包含有建立二叉树和中序遍历二叉树两个函数。可以采用递归算法实现。(用tourb c编程)作业题 谢谢!!

#include<stdio.h>
#include<malloc.h>
#define SIZE sizeof(struct BinaryTree)
struct BinaryTree
{
int data;
struct BinaryTree *llink,*rlink;
}*root;

void creat()
{
struct BinaryTree *p1,*p2;
int temp,flag;
root=NULL;
p1=p2=(struct BinaryTree *)malloc(SIZE);
printf("Input the data,input 0 stop:\n");
while(1)
{
scanf("%d",&temp); //输入0的时候,输入完成
if(temp==0) break;
if(root==NULL)
{
root=(struct BinaryTree *)malloc(SIZE);
root->data=temp; root->llink=root->rlink=NULL;
}
else
{
p2=root; flag=0;
while(flag!=1)
{
if(temp>=p2->data&&p2->rlink!=NULL) p2=p2->rlink;
else if(temp<p2->data&&p2->llink!=NULL) p2=p2