码迷,mamicode.com
首页 > 其他好文 > 详细

二叉树的先序、中序、后序的递归与非递归实现

时间:2014-10-30 22:45:09      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:二叉树   递归   数据结构   

#include <iostream>
#include <cstdlib>
#include <stack>

using namespace std;

struct BinTree {
    int data;
    struct BinTree *left;
    struct BinTree *right;
};

struct BinPost {
    BinTree *pTree;
    bool isFirst;
};

void CreateBinTree(BinTree* &root)
{
    int data;
    cin>>data;
    if(data == 0)
        root = NULL;
    else {
        root = (BinTree*)malloc(sizeof(BinTree));
        root->data = data;
        CreateBinTree(root->left);
        CreateBinTree(root->right);
    }
}

void preOrderFirst(BinTree *root)
{
    if(root) {
        cout<<root->data<<' ';
        preOrderFirst(root->left);
        preOrderFirst(root->right);
    }
}

void inOrderFirst(BinTree *root)
{
    if(root) {
        inOrderFirst(root->left);
        cout<<root->data<<' ';
        inOrderFirst(root->right);
    }
}

void postOrderFirst(BinTree *root)
{
    if(root) {
        postOrderFirst(root->left);
        postOrderFirst(root->right);
        cout<<root->data<<' ';
    }
}
void preOrderSecond(BinTree *root)
{
    BinTree *p;
    stack<BinTree*> stk;
    stk.push(root);

    while(!stk.empty()) {
        p = stk.top();
        while(p != NULL) {
            cout<<p->data<<' ';
            p = p->left;
            stk.push(p);
        }
        stk.pop();
        if(!stk.empty()) {
            p = stk.top(); stk.pop();
            if(p) {
                p = p->right;
                stk.push(p);
            }
        }
    }
}

void inOrderSecond(BinTree *root)
{
    BinTree *p;
    stack<BinTree*> stk;
    stk.push(root);

    while(!stk.empty()) {
        p = stk.top();
        while(p != NULL) {
            p = p->left;
            stk.push(p);
        }
        stk.pop();
        if(!stk.empty()) {
            p = stk.top(); stk.pop();
            if(p) {
                cout<<p->data<<' ';
                p = p->right;
                stk.push(p);
            }
        }
    }
}


void postOrderSecond(BinTree *root)
{
    BinTree *p;
    BinPost *q;
    BinPost *post = (BinPost*)malloc(sizeof(BinPost));
    stack<BinPost*> stk;

    post->isFirst = true;
    post->pTree = root;
    stk.push(post);
    while(!stk.empty()) {
        q = stk.top();
        p = q->pTree;
        while(p != NULL) {
            p = p->left;
            post = (BinPost*)malloc(sizeof(BinPost));
            post->isFirst = true;
            post->pTree = p;
            stk.push(post);
        }

        stk.pop();

        if(!stk.empty()) {
            q = stk.top();
            p = q->pTree;
            if(q->isFirst) {
                q->isFirst = false;
                p = p->right;
                post = (BinPost*)malloc(sizeof(BinPost));
                post->isFirst = true;
                post->pTree = p;
                stk.push(post);
            } else {
                cout<<p->data<<' ';
                q->pTree = NULL;
            }
        }


    }
}

int main()
{
    BinTree *root;
    CreateBinTree(root);
    preOrderFirst(root);
    cout<<endl;
    preOrderSecond(root);
    cout<<endl;
    inOrderFirst(root);
    cout<<endl;
    inOrderSecond(root);
    cout<<endl;
    postOrderFirst(root);
    cout<<endl;
    postOrderSecond(root);
    cout<<endl;
    return 0;
}

二叉树的先序、中序、后序的递归与非递归实现

标签:二叉树   递归   数据结构   

原文地址:http://blog.csdn.net/u012637838/article/details/40626367

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!