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;
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章