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

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);
}
/*----------------*/

运行结果:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值