- 根据先序序列创建二叉树(#表示为null):
ABD##EHJ##KL##M#N###CF##G#I##
- 二叉树的先序遍历的
递归
算法 - 二叉树的先序遍历的
非递归
算法 - 二叉树的中序遍历的
递归
算法 - 二叉树的中序遍历的
非递归
算法 - 二叉树的后序遍历的
递归
算法 - 二叉树的后序遍历的
非递归
算法 - 求二叉树的结点个数
- 求二叉树的叶子结点个数
- 求二叉树中结点任意一结点的层次
- 栈的初始化,进栈,入栈
#include <iostream>
typedef char TElementType;
TElementType ch;
typedef struct BTNode{
TElementType data;
struct BTNode *lchild, *rchild;
} BTNode;
#define MaxSize 100
typedef struct{
BTNode data[MaxSize];
int top;
} SqStack;
void InitStack(SqStack *&s){
s = (SqStack *)malloc(sizeof(SqStack));
s->top = -1;
}
bool StackEmpty(SqStack *s){
return (s->top == -1);
}
bool Push(SqStack *&s, BTNode &e){
if (s->top == MaxSize - 1)
return false;
s->top++;
s->data[s->top] = e;
return true;
}
bool Pop(SqStack *s, BTNode &e){
if (s->top == -1)
return false;
e = s->data[s->top];
s->top--;
return true;
}
void CreateBTree(BTNode *&T)
{
cin >> ch;
if (ch == '#')T = NULL;
else{
T = new BTNode();
T->data = ch;
CreateBTree(T->lchild);
CreateBTree(T->rchild);
}
}
BTNode *LchildNode(BTNode *T){
return T->lchild;
}
BTNode *RchildNode(BTNode *T){
return T->rchild;
}
void PreOrder(BTNode *T){
if (T){
cout << T->data << " ";
PreOrder(T->lchild);
PreOrder(T->rchild);
}
}
void PreOrder1(BTNode *T){
SqStack *S = NULL;
InitStack(S);
BTNode *copyT = T;
BTNode *oldT = new BTNode();
while (copyT || !StackEmpty(S)){
if (copyT){
cout << copyT->data << " ";
if (copyT->lchild){
Push(S, *copyT);
copyT = copyT->lchild;
}else{
copyT = copyT->rchild;
}
}else{
Pop(S, *oldT);
copyT = oldT->rchild;
}
}
}
void InOrder(BTNode *T){
if (T){
InOrder(T->lchild);
cout << T->data << " ";
InOrder(T->rchild);
}
}
void InOrder1(BTNode *T){
SqStack *S = NULL;
InitStack(S);
BTNode *copyT = T;
BTNode *oldT = new BTNode();
while (copyT || !StackEmpty(S))
{
if (copyT){
Push(S, *copyT);
copyT = copyT->lchild;
}else{
Pop(S, *oldT);
cout << oldT->data << " ";
copyT = oldT->rchild;
}
}
}
void PostOrder(BTNode *T)
{
if (T)
{
PostOrder(T->lchild);
PostOrder(T->rchild);
cout << T->data << " ";
}
}
void PostOrder1(BTNode *T){
SqStack *S = NULL;
InitStack(S);
BTNode *copyT = T;
BTNode *oldT = new BTNode();
while (copyT || !StackEmpty(S))
{
if (copyT){
Push(S, *copyT);
copyT = copyT->lchild;
}else{
Pop(S, *oldT);
if(oldT->rchild){
copyT = oldT->rchild;
oldT->rchild = NULL;
Push(S, *oldT);
}else{
cout << oldT->data << " ";
}
}
}
}
void DestroyBTree(BTNode *&T){ delete T;}
int Nodes(BTNode *T){
if(T==NULL)return 0;
int leftNum = Nodes(T->lchild);
int rightNum = Nodes(T->rchild);
return leftNum+rightNum+1;
}
int LeafNodes(BTNode *T){
int count = 0;
if(T==NULL)return count;
if(T->rchild==NULL&&T->lchild==NULL)count++;
count += LeafNodes(T->lchild);
count += LeafNodes(T->rchild);
return count;
}
int Level(BTNode *T, TElementType x,int h){
if(T==NULL) return 0;
if (T->data==x) return h;
int l = Level(T->lchild,x,h+1);
if(l !=0) return l;
return Level(T->rchild,x,h+1);
}
int main(void)
{
BTNode *T = NULL;
CreateBTree(T);
cout << T << endl;
cout << "先序递归遍历为:";
PreOrder(T);
cout << endl<< "先序的非递归遍历为:";
PreOrder1(T);
cout << endl<< "中序递归遍历为:";
InOrder(T);
cout << endl<< "中序非递归递归遍历为:";
InOrder1(T);
cout << endl << "后序递归遍历为:";
PostOrder(T);
cout << endl << "后序非递归遍历为:";
PostOrder1(T);
cout << endl << "结点数为:"<<Nodes(T);
cout << endl << "叶子结点数为:"<<LeafNodes(T);
int h = 1;
TElementType x = 'D';
h = Level(T,x,h);
cout << endl << x << "所在的层次为:"<<h;
return 0;
}