stack 实现二叉树的非递归遍历——C语言

  • 二叉树
  • 非递归遍历

stack.h

#ifndef STACK_H
#define STACK_H
#include"BinaryTree.h"

#define STACKSIZE 100
typedef BtNode * StackType;
typedef struct   //定义栈结构
{
    StackType * data;    //存放结点的地址
    int top;             //栈顶
    int maxsize;         //栈最大容量
}Stack;

void Init_Stack(Stack *sp);            //初始化
bool full(Stack *sp);                  //满
bool empty(Stack *sp);                 //空
bool push(Stack *sp, StackType x);      //入栈
StackType top(Stack *sp);               //返回栈顶的地址
void pop(Stack *sp);                    //出栈

#endif

BinaryTree.h

#ifndef BINARYTREE_H
#define BINARYTREE_H

typedef char ElemType;                   // 定义数据为char
typedef struct BtNode // BinaryTreeNode     //定义结点类型
{ 
    BtNode *leftchild;
    BtNode *rightchild;
    ElemType data;
}BtNode, *BinaryTree;

struct RetNode                //下文函数 返回两个孩子 及路长
{
    BtNode *child1;
    BtNode *child2;
    int path;
};

BtNode * Buynode();               //申请结点
void Freenode(BtNode *p);            //释放结点

BtNode * FindNearParent(BtNode *ptr,BtNode *child1,BtNode *child2);  //寻找两个结点的最近公共双亲

int MaxPath(BtNode *ptr);   //计算最远的路长
RetNode RetTwoPMaxPath(BtNode *ptr);    //返回最远的两个结点及路长
int RetTwoPMaxPath(BtNode *ptr,BtNode *&child1,BtNode *&child2);   //求指定两结点的路长
int SizeBinaryBrch(BtNode *ptr);   //双分支结点的个数
int SizeOneBrch(BtNode *ptr);      //单分支结点的个数
int SizeOneLBrch(BtNode *ptr);     //左单分支结点的个数
int SizeOneRBrch(BtNode *ptr);     //右单分支结点的个数

void NicePerOrder(BtNode *ptr);   //非递归前序
void NiceInOrder(BtNode *ptr);      //非递归中序
void NicePastOrder(BtNode *ptr);    //非递归后序

bool Is_Full_BinaryTree(BtNode *ptr);   //满
bool Is_Comp_BinaryTree(BtNode *ptr);   //完全
bool Is_Balance_BinaryTree(BtNode *ptr);  //平衡

#endif

Std.h

#ifndef STD_H
#define STD_H

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

int Max(int a,int b);

#endif

Std.cpp

#include"Std.h"
int Max(int a,int b)
{
    return a>b? a:b;
}

Stack.cpp

#include"Std.h"
#include"Stack.h"

#define STACKBASE -1
void Init_Stack(Stack *sp)
{
    if(sp == NULL) return ;
    sp->top = -1;
    sp->maxsize = STACKSIZE;
    sp->data = (StackType*)malloc(sizeof(StackType)*sp->maxsize);
    memset(sp->data,0,sizeof(StackType)*sp->maxsize);  //memset 按字节初始化 0 
}

bool full(Stack *sp)                 //满
{
    if(sp == NULL || sp->maxsize == sp->top +1)
        return true;
    else
        return false;
}
bool empty(Stack *sp)                 //空
{
    if(sp == NULL || sp->maxsize == STACKBASE)
        return true;
    else
        return false;

}
bool push(Stack *sp, StackType x)   //入栈
{
    bool res = false;
    if(!full(sp))
    {
        sp->data[++sp->top] = x;
        res = true;
    }
    return res;
}
StackType top(Stack *sp)               //返回栈顶的地址
{
    return sp->data[sp->top];
}
void pop(Stack *sp)                    //出栈
{
    if(sp != NULL && sp->top >= 0)
    {
        sp->top--;
    }
}

BinaryTree.cpp

#include"Std.h"
#include"BinaryTree.h"
#include"Stack.h"

BtNode * Buynode()
{
    BtNode *s = (BtNode*)malloc(sizeof(BtNode));
    if(s == NULL) exit(1);
    memset(s,0,sizeof(BtNode));
    return s;
}
void Freenode(BtNode *p)
{
    free(p);
}

//非递归 前序
void NicePerOrder(BtNode *ptr)
{
    if(NULL == ptr) return ; // 避免少写==将NULL赋值给ptr
    Stack st;
    Init_Stack(&st);        //top=-1
    push(&st,ptr);          //将头入栈
    while(!empty(&st))
    {
        ptr = top(&st);       //返回栈顶的地址
        pop(&st);              //出栈
        printf("%c ",ptr->data);    
        if(ptr->rightchild != NULL)        //右孩子入栈
            push(&st,ptr->rightchild);
        if(ptr->leftchild != NULL)          //左孩子入栈
            push(&st,ptr->leftchild);
    }
}
//非递归中序遍历
void NiceInOrder(BtNode *ptr)
{
    if(ptr == NULL) return ;
    Stack st; // BtNode * 栈中放指针
    Init_Stack(&st);

    while(ptr != NULL || !empty(&st))
    {
        while(ptr != NULL)
        {
            push(&st,ptr);     //入栈
            ptr = ptr->leftchild;     //有左孩子即入栈 
        }
        ptr = top(&st);       //遍历完左孩子 得栈顶元素
        pop(&st);               //出栈
        printf("%c ",ptr->data);
        ptr = ptr->rightchild;     //赋值 开始遍历右孩子
    }
}

//非递归后序遍历  
void NicePastOrder(BtNode *ptr)
{
    if(ptr == NULL) return ;
    Stack st; // BtNode *;
    Init_Stack(&st);
    BtNode *tag = NULL;

    while(ptr != NULL || !empty(&st))
    {
        while(ptr != NULL)
        {
            push(&st,ptr);
            ptr = ptr->leftchild;          //遍历 左
        }
        ptr = top(&st);       //出栈顶
        pop(&st);
        if(ptr->rightchild == NULL || ptr->rightchild == tag)
        {
            printf("%c ",ptr->data);
            tag = ptr;
            ptr = NULL;
        }
        else
        {
            push(&st,ptr);
            ptr = ptr->rightchild;
        }
    }
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值