二叉树采用二叉链表作存储结构,试编程实现二叉树的如下基本操作:
1. 按先序序列构造一棵二叉链表表示的二叉树T;
2. 对这棵二叉树进行遍历:中序、后序以及层次遍历序列,分别输出结点的遍历序列;
3. 求二叉树的深度/叶结点数目。
/*
输入二叉树的先序序列
例如:"abc de g f "
---------------
*/
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<queue>
#include<stack>
using namespace std;
#define OVERFLOW -2
#defineSTACK_INIT_SIZE 50
#define STACKINTCREMENT10
typedef structBiTNode
{
char data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
intCreateBiTree(BiTree &t)
{
char ch;
scanf("%c",&ch);
if(ch==' ') t=NULL;
else
{
if(!(t=(BiTNode*)malloc(sizeof(BiTNode))))
exit(OVERFLOW);
t->data=ch;
CreateBiTree(t->lchild);
CreateBiTree(t->rchild);
}
return 0;
}