您的位置:首页 > 编程语言 > C语言/C++

【学习笔记】非递归实现先后根遍历二叉树

2017-10-28 14:09 507 查看
C语言代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/*----方便看代码的定义----*/
typedef int Status;
#define OK 0
#define NotOK 1

#define STACK_INIT_SIZE 100       //栈空间初始分配量
#define STACKINCREMENT 10         //栈空间分配增量
/*------------------------*/

/*-----定义树结构-----*/
typedef struct BiTree{
char data;
struct BiTree *Lchild;
struct BiTree *Rchild;
}BiTree,*TreeNode;
/*--------------------*/

/*-----输入字符串形式建立树(基于先根)-----*/
/*例:输入 ABD#E##F##CG##HI###
将会生成:            A
/     \
B       C
/  \    /  \
D    F  G    H
\          /
E        I           */
TreeNode CreateBiTree(){
char ch;
TreeNode T;
scanf("%c",&ch);
if( ch == '#' ){
T=NULL;
}
else{
T = (TreeNode)malloc(sizeof(BiTree));
T->data = ch;
T->Lchild = CreateBiTree();
T->Rchild = CreateBiTree();
}
return T;
}
/*-----------------------------------------*/

/*------------------------建立栈------------------------*/
typedef struct{
TreeNode *base;     //在栈构造之前和销毁之后,base的值为NULL
TreeNode *top;      //栈顶指针
int stacksize;  //当前已分配的存储空间,以元素为单位
}Stack;

Status InitStack(Stack &S){
//构造一个空栈S
S.base = (TreeNode *)malloc(STACK_INIT_SIZE * sizeof(TreeNode));
if(!S.base){
exit(NotOK);  //存储分配失败
}
S.top = S.base;
S.stacksize = STACK_INIT_SIZE;
return OK;
}

Status GetTop(Stack S, TreeNode &e){
//若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回NotOK
if(S.top == S.base){
return NotOK;
}
e = *(S.top-1);
return OK;
}

Status Push(Stack &S, TreeNode e){
//插入元素e为新的栈顶元素
if(S.top - S.base >= S.stacksize){
//栈满,追加存储空间
S.base = (TreeNode *)realloc(S.base, (S.stacksize + STACKINCREMENT)*sizeof(TreeNode));
if(!S.base){
//存储分配失败
exit(NotOK);
}
S.top = S.base + S.stacksize;
S.stacksize += STACKINCREMENT;
}
*S.top++ = e;
return OK;
}

Status Pop(Stack &S, TreeNode &e){
//若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回NotOK
if(S.top == S.base){
return NotOK;
}
e = * --S.top;
return OK;
}

bool StackIsEmpty(Stack &S){
//若栈为空,返回true;若栈非空,返回false
if(S.top == S.base){
return true;
}
else{
return false;
}
}
/*------------------------------------------------------*/

/*------非递归先根遍历------*/
Status PreOrderTraverse(TreeNode T){
if( T == NULL ){
return NotOK;
}
else{
printf("PreOrderTraverse:");
Stack TreeStack;
InitStack(TreeStack);
while( T!=NULL || !StackIsEmpty(TreeStack)){
while(T != NULL){
printf("%c",T->data);
Push(TreeStack,T);
T = T->Lchild;
}
if(!StackIsEmpty(TreeStack)){
Pop(TreeStack,T);
T = T->Rchild;
}
}
printf("\n");
return OK;
}
}
/*--------------------------*/

/*------非递归后根遍历------*/
Status PostOrderTraverse(TreeNode T){
if( T == NULL ){
return NotOK;
}
else{
printf("PostOrderTraverse:");
Stack TreeStack;
TreeNode last;
TreeNode current;
Push(TreeStack,T);
while(!StackIsEmpty(TreeStack)){
GetTop(TreeStack,current);
if((current->Lchild==NULL && current->Rchild==NULL)
|| (last != NULL && (last == current->Lchild
|| last == current->Rchild))){
printf("%c",current->data);
Pop(TreeStack, last);
}
else{
if(current->Rchild != NULL){
Push(TreeStack,current->Rchild);
}
if(current->Lchild != NULL){
Push(TreeStack,current->Lchild);
}
}
}
printf("\n");
return OK;
}
}
/*--------------------------*/

/*-----主函数-----*/
int main(){
TreeNode TreeRoot;
TreeRoot = CreateBiTree();
PreOrderTraverse(TreeRoot);
PostOrderTraverse(TreeRoot);
}
/*----------------*/


运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c语言 二叉树 遍历
相关文章推荐