标签:creat color style 递归 sizeof null 遍历 evel 遍历二叉树
1 //BiTree 2 #include<stdio.h> 3 #include<stdlib.h> 4 5 typedef char Elemtype; 6 7 typedef struct BiTNode 8 { 9 char data; 10 struct BiTNode *lchild,*rchild; 11 }BiTNode,*BiTree; 12 13 //创建一棵二叉树,遵照用户用前序遍历的方式输入数据 14 CreateBiTree(BiTree *T) //递归创建二叉树 15 { 16 char c; 17 18 scanf("%c",&c); 19 if(‘ ‘ == c) //没有叶子则设为NULL; 20 { 21 *T = NULL; 22 } 23 else 24 { 25 *T = (BiTNode *)malloc(sizeof(BiTNode)); 26 (*T)->data = c; 27 CreateBiTree(&(*T)->lchild); 28 CreateBiTree(&(*T)->rchild); 29 } 30 } 31 32 //访问二叉树结点的具体操作,你想干嘛? 33 void visit(char c, int level) 34 { 35 printf("%c 位于第 %d 层\n",c,level); 36 } 37 38 //前序遍历二叉树 39 PreOrderTraverse(BiTree T,int level) 40 { 41 if(T) 42 { 43 visit(T->data, level); 44 PreOrderTraverse(T->lchild, level+1); 45 PreOrderTraverse(T->rchild, level+1); 46 } 47 48 } 49 50 int main() 51 { 52 int level = 1; 53 BiTree T = NULL; 54 55 CreateBiTree(&T); 56 PreOrderTraverse(T,level); 57 58 return 0; 59 }
标签:creat color style 递归 sizeof null 遍历 evel 遍历二叉树
原文地址:https://www.cnblogs.com/ZhengLijie/p/12491391.html