排序二叉树建立后中序输出请问错在哪里,求大神

#include
using namespace std;

const int maxn=10010;
int num[maxn];
typedef struct tree
{
int data;
tree *lc,*rc;
} tree,*Tree;

void Insert(Tree &T,int key)
{
if(T==NULL)
{
T=new tree;
T->lc=T->rc=NULL;
T->data=key;
return ;
}
if(T)
{
if(key > T->data)
{
Insert(T->rc,key);
}
else if (key < T->data)
{
Insert(T->lc,key);
}
}
}
int top;
void midout(Tree &T)
{
if(T)
{
midout(T->lc);
///cout<data<<" ";
top++;
num[top]=T->data;
midout(T->rc);
}
}
void lastout(Tree &T)
{
if (T)
{
lastout(T->lc);
lastout(T->rc);
cout<data<<" ";
}
}

int main()
{
int n,i,key;
Tree T;
while (cin>>n)
{
top=0;
T=NULL;
for (i=1; i<=n; i++)
{
cin>>key;
Insert(T,key);
}
midout(T);
for (i=1; i<=n; i++)
{
cout<<num[i]<<" ";
}
}
return 0;
}

排序那边,后面输入的所有数都只和T->key进行了比较而已

我都怀疑你这排序能通过么。

新插入的一个节点,你应该先与根节点比,比根节点大了,再跟右子树节点比,一层层比下去,比到最后才能选择放左边还是放右边

建树的时候,当T为空,新建叶节点。但你没有把这个叶节点加到树上,应该建树的过程就不对
if(T==NULL)
{
T=new tree;
T->lc=T->rc=NULL;
T->data=key;
return ;
}