赞
踩
- #include <stdio.h>
- #include<stdlib.h>
- typedef struct Tree{
-
- char data; // 存放数据域
- struct Tree *lchild; // 遍历左子树指针
- struct Tree *rchild; // 遍历右子树指针
-
- }Tree,*BiTree;
- void CreateBiTree(BiTree &T){ //建立二叉树
- char ch;
- scanf("%c",&ch);
- if(ch=='#') T=NULL;
- else {
- T=(BiTree )malloc(sizeof(Tree));
- T->data=ch;
- CreateBiTree(T->lchild);
- CreateBiTree(T->rchild);
- }
-
- }
- void preOrderTraverse(BiTree T){ //先序输出
- if(T){
- printf("%c",T->data);
- preOrderTraverse(T->lchild);
- preOrderTraverse(T->rchild);
- }
- }
-
- void InOrderTraverse(BiTree T){ //中序输出
- if(T){
- InOrderTraverse(T->lchild);
- printf("%c",T->data);
- InOrderTraverse(T->rchild);
- }
- }
-
- void postOrderTraverse(BiTree T){ //后序输出
- if(T){
- postOrderTraverse(T->lchild);
- postOrderTraverse(T->rchild);
- printf("%c",T->data);
- }
- }
- int main(){
- BiTree T;
- printf("\n请输入构造二叉树的字符序列:");
- CreateBiTree(T);
- printf("\n");
- printf("\n该二叉树的先序是:");
- preOrderTraverse(T);
- printf("\n");
- printf("\n该二叉树的中序是:");
- InOrderTraverse(T);
- printf("\n");
- printf("\n该二叉树的后序是:");
- postOrderTraverse(T);
- printf("\n");
- return 0;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。