C++ 二叉树的先序,中序,后序遍历-递归与非递归方式

12 篇文章 0 订阅
1 篇文章 0 订阅

转载:http://aleeee.com/bitreetraveser1.html


三种遍历方式都分为递归与非递归的方式。三种遍历方式的递归思想相同。后序遍历非递归方法分为两种,具体见代码。


构造方式:

#include<iostream>
#include<stack>
using namespace std;

typedef struct BiTNode{
    char data;
    int lvisited,rvisited;//左、右孩子是否访问过,1表示已访问(此项只在后序非递归2算法中需要)
    struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;

void InitBiTree(BiTree &T)//构造空二叉树
{
    T=NULL;
}
void CreateBiTree(BiTree &T)//生成二叉树
{
    char ch;
    cin>>ch;
    if(ch=='0')//0代表空
        T=NULL;
    else
    {
        T=(BiTree)malloc(sizeof(BiTNode));//生成根结点
        if(!T)
        {
            cout<<"生成结点错误!"<<endl;
            return;
        }
        T->data=ch;
        T->lvisited=0;
        T->rvisited=0;
        CreateBiTree(T->lchild);
        CreateBiTree(T->rchild);
    }
}

三种遍历方式代码:

void PreOrder(BiTree T)//先序递归遍历
{
    if(T!=NULL)
    {
        cout<<T->data<<" ";
        PreOrder(T->lchild);
        PreOrder(T->rchild);
    }
}
void SqlPreOrder(BiTree T)//先序非递归遍历
{
    stack<BiTree> s;
    BiTree p=T;
    while(p || !s.empty())
    {
        if(p)
        {
            cout<<p->data<<" ";
            s.push(p);
            p=p->lchild;
        }
        else
        {
            p=s.top();
            p=p->rchild;
            s.pop();
        }
    }
}



void InOrder(BiTree T)//中序递归遍历
{
    if(T!=NULL)
    {
        InOrder(T->lchild);
        cout<<T->data<<" ";
        InOrder(T->rchild);
    }
}
void SqInOrder(BiTree T)//中序非递归遍历
{
    stack<BiTree> s;
    BiTree p=T;
    while(p || !s.empty())
        if(p)
        {
            s.push(p);
            p=p->lchild;
        }
        else
        {
            p=s.top();
            cout<<p->data<<" ";
            s.pop();
            p=p->rchild;
        }
}



void PostOrder(BiTree T)//后序递归遍历
{
    if(T!=NULL)
    {
        PostOrder(T->lchild);
        PostOrder(T->rchild);
        cout<<T->data<<" ";
    }
}

//后序非递归遍历1思路:因为后序非递归遍历二叉树的顺序是先访问左子树,再访问后子树,最后
//访问根结点。当用堆栈来存储结点,必须分清返回根结点时,是从左子树返回的,还是从右子树
//返回的。所以,使用辅助指针r,其指向最近访问过的结点。
void SqlPostOrder1(BiTree T)//后序非递归遍历1
{
    stack<BiTree> s;
    BiTree p=T,r;
    while(p || !s.empty())
    {
        if(p)                             //走到最左边
        {
            s.push(p);
            p=p->lchild;
        }
        else                             //向右
        {
            p=s.top();//取栈顶结点
            if(p->rchild && p->rchild!=r)//如果右子树存在,且未被访问过
            {
                p=p->rchild;
                s.push(p);
                p=p->lchild;             //再走到最左
            }
            else                         //否则,访问栈顶结点并弹出
            {
                cout<<p->data<<" ";
                r=p;                     //记录该结点
                s.pop();
                p=NULL;                     //结点访问完后,重置p指针
            }
        }
    }
}
//思路2:在结点中增加标志域,记录是否已被访问。
void SqlPostOrder2(BiTree T)//后序非递归遍历2
{
    stack<BiTree> s;
    BiTree p=T;
    while(p || !s.empty())
    {
        if(p && p->lvisited==0)                     //左走,且左子树未被访问
        {
            p->lvisited=1;
            s.push(p);
            p=p->lchild;
        }
        else
        {
            p=s.top();
            if(p->rchild!=NULL && p->rvisited==0)//右子树未被访问,右走一步
            {
                p->rvisited=1;
                p=p->rchild;
            }
            else                                 //访问栈顶元素并弹栈
            {
                cout<<p->data<<" ";
                s.pop();
                if(!s.empty())
                    p=s.top();
                else                             //当最后一个元素弹栈出去后,结束
                    return ;
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是C++数据结构中树的二叉树的前序、中序后序遍历递归非递归代码,包括输入与输出。 假设二叉树的节点定义如下: ``` struct Node { int val; Node* left; Node* right; Node(int x) : val(x), left(NULL), right(NULL) {} }; ``` 前序遍历: 递归版本: ``` void preorderTraversal(Node* root) { if (!root) return; cout << root->val << " "; preorderTraversal(root->left); preorderTraversal(root->right); } ``` 非递归版本: ``` void preorderTraversal(Node* root) { stack<Node*> s; s.push(root); while (!s.empty()) { Node* node = s.top(); s.pop(); if (!node) continue; cout << node->val << " "; s.push(node->right); s.push(node->left); } } ``` 中序遍历: 递归版本: ``` void inorderTraversal(Node* root) { if (!root) return; inorderTraversal(root->left); cout << root->val << " "; inorderTraversal(root->right); } ``` 非递归版本: ``` void inorderTraversal(Node* root) { stack<Node*> s; Node* node = root; while (node || !s.empty()) { while (node) { s.push(node); node = node->left; } node = s.top(); s.pop(); cout << node->val << " "; node = node->right; } } ``` 后序遍历递归版本: ``` void postorderTraversal(Node* root) { if (!root) return; postorderTraversal(root->left); postorderTraversal(root->right); cout << root->val << " "; } ``` 非递归版本: ``` void postorderTraversal(Node* root) { stack<Node*> s; Node* node = root; Node* last = NULL; while (node || !s.empty()) { while (node) { s.push(node); node = node->left; } node = s.top(); if (!node->right || node->right == last) { cout << node->val << " "; s.pop(); last = node; node = NULL; } else { node = node->right; } } } ``` 输入与输出: ``` int main() { Node* root = new Node(1); root->left = new Node(2); root->right = new Node(3); root->left->left = new Node(4); root->left->right = new Node(5); root->right->left = new Node(6); root->right->right = new Node(7); cout << "前序遍历(递归): "; preorderTraversal(root); cout << endl; cout << "前序遍历(非递归): "; preorderTraversal(root); cout << endl; cout << "中序遍历(递归): "; inorderTraversal(root); cout << endl; cout << "中序遍历(非递归): "; inorderTraversal(root); cout << endl; cout << "后序遍历(递归): "; postorderTraversal(root); cout << endl; cout << "后序遍历(非递归): "; postorderTraversal(root); cout << endl; return 0; } ``` 输出结果: ``` 前序遍历(递归): 1 2 4 5 3 6 7 前序遍历(非递归): 1 2 4 5 3 6 7 中序遍历(递归): 4 2 5 1 6 3 7 中序遍历(非递归): 4 2 5 1 6 3 7 后序遍历(递归): 4 5 2 6 7 3 1 后序遍历(非递归): 4 5 2 6 7 3 1 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值