二叉树前序遍历!不知道哪里错了,望指点!谢谢!
// 模拟二叉树.cpp : 定义控制台应用程序的入口点。//
#include "stdafx.h"
#include "stdio.h"
#include "malloc.h"
typedef struct TNode
{
int data;
struct TNode *left;
struct TNode *right;
}Tree;
void Insert(Tree *T,int value)
{
struct TNode *tmp;
Tree *t;//有问题
if(T==NULL)
{
tmp=(struct TNode *)malloc(sizeof(TNode));
tmp->data =value;
tmp->left =NULL;
tmp->right=NULL;
*t=tmp;
}
else
{
if(value<=T->data)
{
if(T->left!=NULL)
Insert(&(T->left),value);
else
{
tmp=(struct TNode *)malloc(sizeof(TNode));
tmp->data =value;
tmp->left =NULL;
tmp->right=NULL;
*t->left=tmp;
}
}
else
{
if(T->right!=NULL)
Insert(&(T->right),value);
else
{
tmp=(struct TNode *)malloc(sizeof(TNode));
tmp->data =value;
tmp->left =NULL;
tmp->right=NULL;
*t->right=tmp;
}
}
}
}
int PreOrder(Tree T)
{
if(T==NULL)
return 0;
else
{
printf("%d\t",T->data );
PreOrder(T->left);
PreOrder(T->right);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
Tree T;
int i,a,n;
T=NULL;
scanf("%d\n",&n);
for(i=0;i<n;i++)
{
scanf("%d\n",&a);
Insert(&T,a);
}
PreOrder(T);
return 0;
}