#include <stdio.h>
#include <stdlib.h>
typedef struct BiTNode{
int data;
struct BiTNode *lchild;
struct BiTNode *rchild;
}BiTNode;
typedef BiTNode *BiTree;
//向二叉排序树中插入元素
void Insert(BiTree root,int x){
BiTNode *s;
if(root == NULL) {
s=(BiTNode *)malloc(sizeof(BiTNode));
s->data = x;
s->lchild = s->rchild = NULL;
root = s;
}
else if (root->data >x){
Insert(root->lchild, x);
}
else {
Insert(root->rchild, x);
}
}
//创建二叉排序树
BiTree CreateTree(){
BiTree root;
root=(BiTNode *)malloc(sizeof(BiTNode));
int k=0;
while(k!=-1){
scanf("%d",&k);
Insert(root, k);
}
return root;
}
//先序递归遍历
void PreOrder(BiTree root){
if(root==NULL) return;
printf("%c",root->data);
PreOrder(root->lchild);
PreOrder(root->rchild);
}
int main() {
BiTree root=CreateTree();
PreOrder(root);
return 0;
}
你建树的时候