- 论坛徽章:
- 0
|
#include <stdio.h>
#include <stdlib.h>
typedef char elemtype;
typedef struct node {
elemtype data;
struct node *lchild;
struct node *rchild;
}BinTNode,*BinTree;
void CreatBinTree (BinTree *t);
void preorder (BinTree t);
void main ()
{ BinTree t;
CreatBinTree(&t);
printf ("Print Previous Order:\n");
preorder (t);
}
void CreatBinTree (BinTree *t)
{
char ch;
scanf ("%c", &ch);
if (ch == ' ')
t = NULL;
else {
*t = malloc (sizeof (BinTNode));
(*t)->data=ch;
CreatBinTree (&((*t) -> lchild));
CreatBinTree (&((*t) -> rchild));
}
}
void preorder (BinTree t)
{
if (t!=NULL){
printf (" %c ", t -> data);
preorder (t -> lchild);
preorder (t -> rchild);
}
} |
|