二叉树的先中后序的递归非递归遍历以及层次遍历(C++)

//建立 先中后序递归 非递归 层次遍历
#include <iostream>
#include<stack>
#include<stdio.h>
#include<stdlib.h>
#include<queue>
using namespace std;
typedef struct Node 
{
    char data;
    struct Node* lchild, * rchild;
}BiTNode,*Bitree;
void CreateBiTree(Bitree* T)
{//创建二叉树
    char ch;
    ch = getchar();
    if (ch == '#') {	
        *T = NULL;
        return;
    }
    else {
        *T = (Bitree)malloc(sizeof(BiTNode));
        (*T)->data = ch;
        CreateBiTree(&((*T)->lchild));
        CreateBiTree(&((*T)->rchild));

    }
}
//先中后序的递归遍历
void PreOrder(Bitree T)
{
    
    if (T) {
        cout << T->data;
        PreOrder(T->lchild);
        PreOrder(T->rchild);
    }
    else cout << "#";
}
void Inorder(Bitree T)
{
    if (T) {
        Inorder(T->lchild);
        cout << T->data;
        Inorder(T->rchild);
    }
    else cout << "#";
}
void Posorder(Bitree T)
{
    if (T) {
        Posorder(T->lchild);
        Posorder(T->rchild);
        cout << T->data;
    }
    else cout << "#";
}
//先中后序非递归遍历
Bitree p = nullptr;
stack<BiTNode*>s;//建栈
void FPreorder(Bitree T)
{
    
    if (T == nullptr)
    {return;}
    else
    {p = T;}//p为查找指针
    do
    {   
        while (p != NULL)
        {
            cout << p->data;
            if (p->rchild != NULL)
            {
                s.push(p->rchild);//使p的右孩子入栈
            }
            else { s.push(NULL); }//没有右孩子就入栈空
                p = p->lchild; 
        }cout << "#";p = s.top();s.pop();//如果上个结点的右孩子也为空就会跳出这层while 所以输出#并指向栈顶
    } while ((!s.empty())|| (p != NULL));
    cout << "#";//因为最后必为空才会出循环 但并没有输出最后一个右孩子 所以输出# 
}
void FInorder(Bitree T)
{
    if (T == nullptr)
    {return;}
    else
    { p = T;}
    do
    {
        while (p != NULL)
        {
            
            s.push(p);
            p = p->lchild;
        }cout << "#";
           
        if (!s.empty())
        {

            p = s.top();
            s.pop();
            cout << p->data;
            p = p->rchild;
        }
            
    } while ((!s.empty())|| (p!= NULL));
    cout << "#";
}
void FPosorder(Bitree T)
{
    bool flag = true;
    if (T == nullptr)
    {return;}
    else
    {p = T;}
    do
    {
        while (p != NULL)
        {
            s.push(p);p = p->lchild;
        }cout << "#";
        while(!s.empty()&&(p==NULL))
        {   
            p = s.top();s.pop();
            if (flag == true)
            {
                s.push(p);
                p = p->rchild;
                if (p == NULL)
                {
                    s.push(NULL);
                    flag = false;
                }
                
            } 
            else if(p==NULL)
            {
                    cout << "#";
                    p=s.top(); s.pop();
                    cout << p->data;
                    if(p==s.top()->rchild)
                    {p= s.top(); s.pop();
                     cout << p->data;
                     p = NULL;
                     flag = true;
                    }else
                    {p = NULL;flag = true;}
                    
            }
            
        }
       
    } while ((!s.empty()) || (p != NULL));
}
//层次遍历
void Levelorder(Bitree T)
{
    queue<Bitree>queue;//建队列
    if (T == nullptr) { return; }
    p = T;
    queue.push(p);//头节点入队
    while (!queue.empty())
    {
        p = queue.front();
        queue.pop();
        cout << p->data;
        if(p->lchild != nullptr)
        {queue.push(p->lchild);}

        if (p->rchild != nullptr)
        {queue.push(p->rchild);}
       
    }
}
int main()
{
    Bitree T ;
    cout << "创建一颗树,按先序输入,其中A-Z字符代表树的数据,用“#”表示空树:" << endl;
    CreateBiTree(&T);
    cout << "先序递归遍历:" << endl;
    PreOrder(T);
    cout << endl;
    cout << "中序递归遍历:" << endl;
    Inorder(T);
    cout << endl;
    cout << "后序递归遍历:" << endl;
    Posorder(T);
    cout << endl;
    cout << "先序非递归遍历:" << endl;
    FPreorder(T);
    cout << endl;
    cout << "中序非递归遍历:" << endl;
    FInorder(T);
    cout << endl;
    cout << "后序非递归遍历:" << endl;
    //FPosorder(T);
    cout << endl;
    cout << "层次遍历:" << endl;
    Levelorder(T);
    cout << endl;
    return 0;
}

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值