Non-recursive tree traversal

Syntax errors may be some small compile time (such as a semicolon, ->, etc.), it is easy to correct yourself, oh, absolutely thinking is entirely correct, so use your own words to try to change to change it, copy and paste it correct, is it too did not write the code experience, and he was more impressive change to change it ( ^ ▽ ^ ) ~ ~ ~ ~;

//非递归遍历二×树
#include<iostream>
using namespace std;
//二叉链存储
typedef struct BiNode{
    char data;
    struct BiNode *lchild,*rchild;
}BiTNode,*BiTree;

//链栈
typedef struct StackNode{
    BiTNode data;
    struct StackNode *next;
}StackNode,*LinkStack;

void CreatBiTree(BiTree &T){
    char ch;
    cin>>ch;
    if(ch=='#') T==NULL;
    else{
        T=new BiTNode;
        T->data=ch;
        CreatBiTree(T->lchild);
        CreatBiTree(T->rchild);
    }
}

void InitStack(LinkStack &S)
{
    //构造一个空栈S,栈顶指针置空
    S=NULL;
}

void StackEmpty(LinkStack S){
    if(!S){
        return true;
    }else{
        return false;
    }
}

void Push(LinkStack &S,BiTree e){
    StackNode *p=new StackNode;
    p->data=*e;
    p->next=S;
    S=p;
}

void pop(LinkStack &S BiTree e){
    if(S!=NULL){
        *e=S->data;
        StackNode *p=S;
        S=S->next;
        delete p;
    }
}
//中序
void InOderTraversel1(BiTree T){
    LinkStack S;BiTree p;
    BiTree q=new BiTNode;
    InitStack(S); p=T;
    while(p||!StackEmpty(S)){
        if(p){
            push(S,p);
            p->lchild;
        }else{
            pop(S,p);
            cout<<q->data;
            p=q->rchild;
        }
    }
}

void main(){
    BiTree tree;
    cout<<"please input\n";
    CreatBiTree(tree);
    cout<<"result:\n";
    InOderTraversel1(tree);
    cout<<endl;
}

Guess you like

Origin www.cnblogs.com/ygjzs/p/11874577.html