#include<iostream>
using namespace std;
typedef int ElemType;
typedef struct TreeNode{
ElemType data;
struct TreeNode *l, *r;
}TreeNode, *BSTree;
void insert(BSTree *ST, int data)
{
BSTree p;
if(*ST == NULL)
{
p = new TreeNode;
p->data = data;
p->l = NULL;
p->r = NULL;
*ST = p;
}
else if(data<(*ST)->data)
insert(&(*ST)->l, data);
else if(data>(*ST)->data)
insert(&(*ST)->r, data);
}
void view(BSTree *ST)
{
view(&(*ST)->l);
cout<<(*ST)->data<<" ";
view(&(*ST)->l);
}
int main(void)
{
int data;
BSTree ST = NULL;
for(int i=0; i<5; i++)
{
cin>>data;
insert(&ST, data);
}
view(&ST);
return 0;
}
我找了好久硬是没找出问题来。
1
tsunli 2019-06-24 20:12:52 +08:00
void view(BSTree *ST)
{ if( !ST) return ; view(&(*ST)->l); cout<<(*ST)->data<<" "; view(&(*ST)->l); } |
2
tsunli 2019-06-24 20:14:01 +08:00
f( ! *ST) return ;
|
3
tsunli 2019-06-24 20:30:01 +08:00 1
cout<<(*ST)->data<<" ";
view(&(*ST)->r); |