马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <stdio.h>
#include <stdlib.h>
typedef char ElemType;
typedef struct BiTNode{
ElemType data;
struct BiTNode *lchild, *rchild;
}BiTNode, *BiTree;
void createBiTree(BiTree *T){ //double ptr
ElemType c;
printf(">>>");scanf("%c",&c);
if('#' == c){
*T = NULL;
}else{
*T = (BiTree)malloc(sizeof(BiTNode));
(*T)->data = c;
}
createBiTree(&(*T)->lchild);//compile to exam
createBiTree(&(*T)->rchild);//compile to exam
}
void visit(ElemType c, int level){
printf("%c locate in layer %d\n");
}
void preOrderTraverse(BiTree T, int level){
if(T){
visit(T->data, level); //1
preOrderTraverse(T->lchild, level+1);//2
preOrderTraverse(T->rchild, level+1);//3
}
}
//midOrderTraverse:: 2->1->3
//posOrderTraverse:: 2->3->1
void test(){
int level =1;
BiTree T = NULL;
createBiTree(&T);
preOrderTraverse(T, level);
}
int main(void)
{
test();
return 0;
}
if('#' == c){
*T = NULL;
}else{
*T = (BiTree)malloc(sizeof(BiTNode));
(*T)->data = c;
createBiTree(&(*T)->lchild);//compile to exam
createBiTree(&(*T)->rchild);//compile to exam
}
|