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

来源:互联网 发布:js canvas api 编辑:程序博客网 时间:2024/05/22 13:09
#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;}

0 0
原创粉丝点击