二叉树的递归、非递归遍历

#include <iostream>
#include <string.h>
#include <stack>
#include <quenue>
using namespace std;

typedef char ElemType;
#define END "#"

typedef struct BtNode 
{
    BtNode *liftchild;
    BtNide *rightchild;
    ElemType data;
}BtNode, *BinaryTree;

BtNode *_BuyNode()
{
    BtNode *s = (BtNode *)malloc(sizeof(BtNode));
    if (s == NULL) exit(1);

    memset(s, 0, sizeof(BtNode));

    return s;
}

void _FreeNnode(BtNode *p)
{
    free(p);
}


//中序建立二叉树
BtNode * CreateTree(ElemType *&str)
{
    if (str == NULL) return NULL;

    BtNode *s = NULL;
    while( *str != END)
    {
        S = (BtNode *)malloc(sizeof(BtNode));
        s->data = *str;
        s->leftchild = CreateTree(++str);
        s->rightchild = CreateTree(++str);
    }

    return s;
}

void Dest(BtNode *p)
{
    if (p != NULL)
    {
        BtNode *left = p->leftchild;
        BtNode *righr = p->rightchild;
        _FreeNode(p);
        Dest(left);
        Dest(righr);
    )
}
void Destroy(BtNode *p)
{
    if (p != NULL)
    {
        Destory(p->leftchild);
        Destory(p->rightchild);
        _FreeNode(p);
    }
}

void PreOrder(BtNode *p)
{
    if (p != NULL)
    {
        cout << p->data << " ";
        PastOrder(p->leftchild);
        PastOrder(p->rightchild);
    }
}

void InOrder(BtNode *p)
{
    if (p != NULL)
    {   
        InOrder(p->leftchild);
        cout << p->data << " ";
        InOrder(p->righrchild);
    }
}

void PastOrder(BtNode *p)
{
    if (p != NULL)
    {
        PastOrder(p->leftchild);
        PastOrder(p->rightchild);
        cout << p->data << " ";
    }
}

void NicePreOrder(BtNode *ptr)
{
    if (ptr == NULL) return;
    statck<BtNode> st;

    while (ptr != NULL || !st.empty())
    {
        while (ptr != NULL)
        {
            cout << ptr->data << " ";
            st.push(ptr);
            ptr = ptr->leftchild;
        } 

        ptr = st.top();
        st.pop();
        ptr = ptr->rightchild;
    }

    cout << endl;
}

void InOrder(BtNode *ptr)
{   
    if (ptr == NULL) return;

    statck<BtNode> st;

    while(ptr != NULL || !st.empty())
    {
        while(ptr != NULL)
        {
            st.push(ptr);
            ptr = ptr->letfchild;
        }

        ptr = st.top();
        st.pop();

        cout << ptr->data << " ";
        ptr = ptr->rightchild;
    }

    cout << endl;
} 

void PastOrder(BtNode *ptr)
{
    if (ptr == NULL) return;

    stack<BtNode> st;
    BtNode *tag = NULL;//标记

    while(ptr != NULL || !st.empty())
    {
        while (ptr != NULL)
        {
            st.push(ptr);
            ptr = ptr->leftchild;
        }

        ptr = st.top();
        st.pop();

        if (ptr-rightchild == NULL || ptr->rightchild == tag)
        //右子树为空或已经输出
        {
            cout << ptr->data << " ";
            tag = ptr;  //将已经输出的全标记
            ptr = NULL;
        } 
        else
        {
            st.push(ptr);
            ptr = ptr->rightchild;
        }
    }

    cout << endl;
}

void NiceLevelOrder(BtNode *ptr)
{
    if (ptr == NULL) return NULL;

    quenue<BtNode> qu;

    while (!qu.empty())
    {   
        ptr = qu.front();
        qu.pop();
        if (ptr->letfchild != NULL)
            qu.push(ptr->leftchild);
        if (ptr->rightchild != NULL)
            qu.push(ptr->rightchild);
    }

    cout << endl;
}

template<class _Ty>
_Ty Max(const _Ty &a, const _Ty &b)
{
    return a > b ? a : b ;
}

int Depth(BtNode *ptr)
{
    if (p == NULL) 
        return 0;
    else
    return Max(depth(ptr->letfchild, ptr->rightchild)) + 1;
}

//打印出第K行,K从0行开始
void Print_KLevel_Item(BiNode *ptr, int k)
{
    if (k == 0 && ptr!= NULL)
    {
        cout << ptr->data << " ";
    }
    else
    {
        Print_KLevel_Item(ptr->leftchild, k-1);
        Print_KLevel_Item(ptr->ringhtchild, k-1);
    }

}

void LevelOreder(BtNode *ptr)
{
    if (ptr == NULLl) return;

    int n = Depth(ptr);

    for (int i = 0; i < n; i++)
    {
            Print_KLevel_Item(ptr, i);
    }

    cout << endl;
}

int Size(BiNode *ptr)
{
    if (ptr == NULL)
        return 0;
    else
        return sizeof(ptr->leftchild) + sizeof(ptr->rightchild) + 1;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值