前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >树的遍历非递归实现

树的遍历非递归实现

作者头像
ShenduCC
发布2020-05-25 17:27:35
7640
发布2020-05-25 17:27:35
举报
文章被收录于专栏:算法修养算法修养
代码语言:javascript
复制
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <math.h>


using namespace std;

struct Node
{
    int val;
    Node* left;
    Node* right;
    Node(int value)
    {
        this->val = value;
        this->left=NULL;
        this->right=NULL;
    }
};

void preOrder(Node* root)
{
    if(root==NULL)
        return;
    cout<<root->val;
    preOrder(root->left);
    preOrder(root->right);
}

void inOrder(Node* root)
{
    if(root==NULL)
        return;

    inOrder(root->left);
    cout<<root->val;
    inOrder(root->right);
}

void postOrder(Node* root)
{
    if(root==NULL)
        return;

    postOrder(root->left);

    postOrder(root->right);
    cout<<root->val;

}
void buildTree(Node* &tree)
{
    tree = new Node(4);
    tree->left = new Node(3);
    tree->left->left = new Node(5);
    //tree->left->left->right = new Node(10);
    //tree->left->left->right->left = new Node(11);
    tree->left->right = new Node(6);
    tree->right = new Node(7);
    tree->right->left = new Node(8);
    tree->right->right = new Node(9);
}

int main()
{

    Node* tree;
    buildTree(tree);
    preOrder(tree);
    cout<<endl;
    stack<Node*> s;
    s.push(tree);

    while(!s.empty())
    {
        Node* temp = s.top();
        s.pop();

        cout<<temp->val;
        if(temp->right!=NULL)
            s.push(temp->right);
        if(temp->left!=NULL)
            s.push(temp->left);
    }

    cout<<endl;

    Node* tree2=tree;
    cout << tree->val;
    s.push(tree);

    while(!s.empty()) {

        Node *temp = s.top();

        if (temp->left != NULL) {
            cout << temp->left->val;
            s.push(temp->left);
            temp->left=NULL;
            continue;
        }

        s.pop();
        if (temp->right != NULL) {
            cout << temp->right->val;
            s.push(temp->right);
            temp->right=NULL;
            continue;
        }


    }
    cout<<endl;

    buildTree(tree);
    Node* temp = tree;
    while(!s.empty()||temp!=NULL)
    {
        if(temp!=NULL)
        {
            cout<<temp->val;
            s.push(temp);
            temp=temp->left;
        }
        else
        {
            temp = s.top();
            s.pop();
            temp = temp->right;
        }
    }
    cout<<endl;

    inOrder(tree);

    cout<<endl;

    s.push(tree);

    while(!s.empty())
    {
        Node* temp = s.top();
        if(temp->left!=NULL)
        {
            s.push(temp->left);
            temp->left=NULL;
            continue;
        }
        cout<<temp->val;
        s.pop();
        if(temp->right!=NULL)
        {
            s.push(temp->right);
            temp->right=NULL;
            continue;
        }
    }

    cout<<endl;
    buildTree(tree);
    temp = tree;
    while(!s.empty()||temp!=NULL)
    {
        if(temp!=NULL)
        {
            s.push(temp);
            temp=temp->left;
        } else{
            temp = s.top();
            cout<<temp->val;
            s.pop();
            temp = temp->right;
        }
    }
    cout<<endl;
    buildTree(tree);

    postOrder(tree);
    cout<<endl;
    s.push(tree);

    while(!s.empty())
    {
        Node* temp = s.top();
        if(temp->left!=NULL)
        {
            s.push(temp->left);
            temp->left=NULL;
            continue;
        }
        if(temp->right!=NULL)
        {
            s.push(temp->right);
            temp->right=NULL;
            continue;
        }
        s.pop();
        cout<<temp->val;
    }

    cout<<endl;
    buildTree(tree);

    s.push(tree);

    vector<int> ans;

    while(!s.empty())
    {
        Node* temp = s.top();
        s.pop();

        ans.push_back(temp->val);
        if(temp->left!=NULL)
            s.push(temp->left);
        if(temp->right!=NULL)
            s.push(temp->right);
    }

    for(int i=ans.size()-1;i>=0;i--)
        cout<<ans[i];

    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-05-19 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档