二叉树问题,构建二叉树,用中序和后序遍历树并输出。

新手上路,请多包涵

我是初学者,这道题内容是构建二叉树,用中序和后序遍历树并输出。以下代码里面除了main函数之外都是来源于《数据结构与算法分析》这本书,main函数来自于老师,经过个人理解之后进行了混合,结果在dev c++上运行失败,如下:

include<stdio.h>

include<string.h>

include<stdlib.h>

typedef struct TreeNode *SearchTree;
typedef int ElementType;
struct TreeNode;

struct TreeNode
{

ElementType Element;
SearchTree Left;
SearchTree Right;

};

SearchTree Insert(ElementType X,SearchTree);
void Postorder(SearchTree);
void Inorder(SearchTree);
void Visit(ElementType);

SearchTree Insert(ElementType X,SearchTree T)
{

if(T == NULL)
{
    T=malloc(sizeof(struct TreeNode));
    if(T == NULL)
    {
        printf("Out of space!!!");
    }
    else
    {
        T->Element = X;
        T->Left = T->Right =NULL;
    }
}
else if(X < T->Element)
{
    T->Left =Insert(X,T->Left);
}
else if(X > T->Element)
{
    T->Right =Insert(X,T->Right);
}

}

void Postorder(SearchTree T)
{

 if(T)
 {
     Postorder(T->Left);
     Postorder(T->Right);
     Visit(T->Element); 
 }

}

void Inorder(SearchTree T)
{

 if(T)
 {
      Inorder(T->Left);
      Visit(T->Element); 
      Inorder(T->Right);
 }

}

void Visit(ElementType T)
{

printf("%d,",T);

}

int main()
{

 SearchTree T=NULL;
 int ch;
 char input[100]={0};
 scanf("%s",input);
 char *delim=",";
 char *p=strtok(input,delim);
 while(p!=NULL)
 {
    T=Insert(atoi(p),T);
    p=strtok(NULL,delim);
 }
 Postorder(T);
 printf("\n");
 Inorder(T);
 getchar();
 getchar();
 return 0;

}

这已经不是我第一次碰到说返回失败的失败了,以前都是指针出错,但是这次实在查不出来,希望大神们可以帮帮忙orz,谢谢了。

clipboard.png

clipboard.png

clipboard.png

阅读 2.1k
1 个回答

提问之前可不可以先调调代码格式?,就算真的会都懒得看了?

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进