二叉树segmentation default(加急)
#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
}
页:
[1]