c语言实现二叉树(BiNodeTree)的建立与先序遍历,中序遍历时间:2022-03-08 10:10:21#include <stdio.h>#include <malloc.h>typedef struct BiNode { char data; struct BiNode *lchild, *rchild;}BiNode;BiNode *CreateBiTree(BiNode *T) { char data; scanf("%c", &data); if((data=='#')||(data=='/n')) { T = NULL; } else { T = (BiNode *)malloc(sizeof(BiNode)); if(T==NULL) { printf("memory error"); } T->data = data; T->lchild = CreateBiTree(T->lchild); T->rchild = CreateBiTree(T->rchild); } return T;}void PreOrderTraverse(BiNode *T) { if(T==NULL) { return; } else { printf("%c ", T->data); PreOrderTraverse(T->lchild); PreOrderTraverse(T->rchild); }}void InOrderTraverse(BiNode *T) { if(T==NULL) { return; } else { InOrderTraverse(T->lchild); printf("%c ", T->data); InOrderTraverse(T->rchild); }}int main() { BiNode *T = NULL; T = CreateBiTree(T); printf("先序遍历: /n"); PreOrderTraverse(T); printf("/n"); printf("中序遍历: /n"); InOrderTraverse(T); return 0;}//输入: -+##ab##c## //输出: 先序遍历: // - + a b c // 中序遍历: // + - b a c