对二叉树的遍历进行实现的算法:
(1)定义二叉树的二叉链表存储结构。
(2)编写建立一棵二叉树算法。
(3)编写中序遍历二叉树算法。
(4)编写后序遍历二叉树算法。
(5)编写算法主程序。
(6)调试运行,记录结果。
编写头文件 Bitree.h
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define OK 1
#define OVERFLOW -2
#define INFEASIBLE -1
#define TRUE 1
#define FALSE 0
#define ERROR 0
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef char TElemType;
typedef int Status;
//二叉树的二叉链表存储表示
typedef struct BiTNode {
TElemType data;
struct BiTNode * lchild,* rchild;
} BiTNode,* BiTree;
//创建二叉树
Status CreatBiTree(BiTree &T){
char ch;
scanf("%c",&ch);
if (ch == '#') T=NULL;
else {
if (!(T=(BiTree)malloc(sizeof(BiTNode)))) exit(OVERFLOW);
T->data=ch;
CreatBiTree(T->lchild);
CreatBiTree(T->rchild);
}
return OK;
}
typedef BiTree SElemType;