• 非递归先序、中序遍历二叉树


    #include<stdio.h>
    #include<malloc.h>
    //2013-12-23 
    //乾卦
    #define MAX 512
    typedef struct Node
    {
        char data;
        struct Node *rchild;
        struct Node *lchild;
    }BTNode;
    //创建二叉树
    void CreatBT(BTNode *&root,char *str)
    {
        int top=0;
        int j=0;
        BTNode *st[MAX];
        BTNode *p=NULL;
        int flg=0;
        root=NULL;
        while(str[j]!='')
        {
            if(str[j]=='(')
            {
                top++;
                st[top]=p;
                flg=1;
            }
            else if(str[j]==')')
            {
                top--;
            }
            else if(str[j]==',')
            {
                flg=2;
            }
            else
            {
                p=(BTNode*)malloc(sizeof(BTNode));
                if(!p)
                {
                    printf("申请空间失败!
    ");
                    return ;
                }
                p->data=str[j];
                p->lchild=NULL;
                p->rchild=NULL;
                if(!root)
                {
                    root=p;
                }
                else
                {
                    if(flg==1)
                        st[top]->lchild=p;
                    if(flg==2)
                        st[top]->rchild=p;
                }
            }
            j++;
        }
    
        return ;
    }
    //非递归先序遍历
    void PreOrder(BTNode *root)
    {
        BTNode  *st[MAX];
        int top=0;
        //必须定义一个临时变量保存出栈前栈顶
        BTNode  *p;
        if(root)
        {
            top++;
            st[top]=root;
            while(top>0)
            {
                p=st[top];
                top--;
                printf("%c  ",p->data);
                //入栈顺序 先右后左
                if(p->rchild)
                {
                    top++;
                    st[top]=p->rchild;
                }
                if(p->lchild)
                {
                    top++;
                    st[top]=p->lchild;
                }
    
            }
        }
    
        return ;
    }
    void main()
    {
        BTNode *ROOT=NULL;
        CreatBT(ROOT,"A(B(D(,G)),C(E,F))");
        PreOrder(ROOT);
        return ;
    }

    中序遍历:

    void InOrder(BTNode *root)
    {
        //设置标志
        //tag=1:不能直接访问
        //tag=0:可以直接访问
        struct 
        {
            BTNode *p;
            int tag;
        }st[MAX];
        BTNode  *b;
        int top=0;
        top++;
        st[top].p=root;
        st[top].tag=1;
        while(top>0)
        {
            //不能直接访问
            if(1==st[top].tag)
            {
                //保存未出栈的BTNode指针
                b=st[top].p;
                //出栈
                top--;
                if(b)
                {
                    top++;
                    st[top].tag=1;
                    st[top].p=b->rchild;
    
                    top++;
                    st[top].tag=0;
                    st[top].p=b;
    
                    top++;
                    st[top].tag=1;
                    st[top].p=b->lchild;
                }
            }
            //可以直接访问
            if(0==st[top].tag)
            {
                printf("%c ",st[top].p->data);
                top--;
            }
        }
    
    
        return ;
    }
    void main()
    {
        BTNode *ROOT=NULL;
        CreatBT(ROOT,"A(B(D(,G)),C(E,F))");
        printf("先序遍历:
    ");
        PreOrder(ROOT);
        printf("
    中序遍历:
    ");
        InOrder(ROOT);
        return ;
    }

  • 相关阅读:
    【博客大赛】使用LM2677制作的3V至24V数控可调恒压源
    电压跟随器
    运算放大器虚短和虚断
    JTAG TAP Controller
    JTAG Pinouts
    USB Mass Storage Class – Bulk Only Transport
    Send custom commands to Mass Storage device
    USB Mass Storage communication with PassThrough / more than 64K data length
    STLink download/debug interface for Linux.
    SCSI Pass-Through Interface Tool
  • 原文地址:https://www.cnblogs.com/qiangua/p/3488213.html
Copyright © 2020-2023  润新知