[Data structure] a binary tree is traversed in ascending order before non-recursion.

Source: Internet
Author: User

The data structure is recursive. After a deep understanding, write a three-Order Non-recursive version.

 

//测试数据:abd##eg##h##c#f###include <cstdio>#include <iostream>typedef char ElemType;typedef struct Node{    ElemType elem;    struct Node *lchild,*rchild;}Node,*BiTree;typedef struct{    BiTree *base;    BiTree *top;    int stacksize;}SqStack;void InitStack(SqStack &S){    S.base=(BiTree *)malloc(100*sizeof(BiTree));    if(!S.base) exit(0);    S.top=S.base;    S.stacksize=100;}void GetTop(SqStack S,BiTree &e){    if(S.top==S.base)exit(0);    e=*(S.top-1);}void Pop(SqStack &S,BiTree &e){    if(S.top==S.base)exit(0);    e=*--S.top;  //先自减,再取值}void Push(SqStack &S,BiTree e)//严谨的话还应该有if(S.top-S.base>=S.stacksize)的判断,此处略去{    *S.top++=e;//先赋值,再自加}int Empty(SqStack S){    if(S.top==S.base)return 1;    else return 0;}BiTree CreateTree(BiTree &T){    char ch;    ch=getchar();    if(ch==‘#‘)T=NULL;    else    {        T=(BiTree)malloc (sizeof(Node));        T->elem=ch;        T->lchild=(CreateTree(T->lchild));        T->rchild=(CreateTree(T->rchild));    }    return T;}void Middleordertraverse(BiTree T) //中序非递归遍历{    SqStack S;    InitStack(S);    BiTree p=T;    while(p || !Empty(S))    {        if(p)        {            Push(S,p);            p=p->lchild;        }        else        {            Pop(S,p);            printf("%c   \n",p->elem);            p=p->rchild;        }    }}void Firstordertraverse(BiTree T)//先序非递归遍历{    SqStack S;    InitStack(S);    BiTree p=T;    while(p || !Empty(S))    {        while(p)        {            Push(S,p);            printf("%c   \n",p->elem);            p=p->lchild;        }        if(!Empty(S))        {            Pop(S,p);            p=p->rchild;        }    }}void BehindorderTraverse(BiTree T) //后序非递归遍历{    SqStack S;    InitStack(S);    BiTree pre;//设置标志位,标记是否访问过    BiTree p=T;    while(p || !Empty(S))    {        while(p)  //左孩子向下压栈  直到最左下        {            Push(S,p);            p=p->lchild;        }        GetTop(S,p);   //取当前栈顶  即当前节点最左下        if(!p->rchild || p->rchild==pre)//没右孩子或右孩子被遍历过        {            printf("%c   \n",p->elem);            pre=p; //标记此节点已经走过            Pop(S,p);            if( Empty(S))return ;            //p回到其父节点(前面肯定了没左子树,而进入此循环显然没右孩子)            p=NULL;  //不对p左子树遍历        }        else p=p->rchild;    }    return ;}int main(){    //freopen("in.txt","r",stdin);    printf("请给出二叉树的节点序列:  \n");    BiTree T;    CreateTree(T);    printf(" 中序遍历二叉树结果是 :  \n");    Middleordertraverse(T);    printf(" 先序遍历二叉树结果是 :  \n");    Firstordertraverse(T);    printf(" 后序遍历二叉树结果是 :  \n");    BehindorderTraverse(T);    return 0;}

 

[Data structure] a binary tree is traversed in ascending order before non-recursion.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.