zoukankan      html  css  js  c++  java
  • 二叉树三种非递归遍历算法

    
    1.先序遍历非递归算法
    
    #define maxsize 100
    
    typedef struct
    
    {
    
    Bitree Elem[maxsize];
    
    int top;
    
    }SqStack;
    
    
    void PreOrderUnrec(Bitree t)
    
    {
    
    SqStack s;
    
    StackInit(s);
    
    p=t;
    
    
    while (p!=null || !StackEmpty(s))
    
    {
    
    while (p!=null) //遍历左子树
    
    {
    
    visite(p->data);
    
    push(s,p);
    
    p=p->lchild; 
    
    }//endwhile
    
    
    if (!StackEmpty(s)) //通过下一次循环中的内嵌while实现右子树遍历
    
    {
    
    p=pop(s);
    
    p=p->rchild; 
    
    }//endif
    
    
    }//endwhile 
    
    
    
    }//PreOrderUnrec
    
    
    2.中序遍历非递归算法
    
    #define maxsize 100
    
    typedef struct
    
    {
    
    Bitree Elem[maxsize];
    
    int top;
    
    }SqStack;
    
    
    void InOrderUnrec(Bitree t)
    
    {
    
    SqStack s;
    
    StackInit(s);
    
    p=t;
    
    while (p!=null || !StackEmpty(s))
    
    {
    
    while (p!=null) //遍历左子树
    
    {
    
    push(s,p);
    
    p=p->lchild;
    
    }//endwhile
    
    
    
    if (!StackEmpty(s))
    
    {
    
    p=pop(s);
    
    visite(p->data); //访问根结点
    
    p=p->rchild; //通过下一次循环实现右子树遍历
    
    }//endif 
    
    
    }//endwhile
    
    
    }//InOrderUnrec
    
    
    
    3.后序遍历非递归算法
    
    #define maxsize 100
    
    typedef enum{L,R} tagtype;
    
    typedef struct 
    
    {
    
    Bitree ptr;
    
    tagtype tag;
    
    }stacknode;
    
    
    typedef struct
    
    {
    
    stacknode Elem[maxsize];
    
    
    int top;
    
    }SqStack;
    
    
    void PostOrderUnrec(Bitree t)
    
    {
    
    SqStack s;
    
    stacknode x;
    
    StackInit(s);
    
    p=t;
    
    
    do 
    
    {
    
    while (p!=null) //遍历左子树
    
    {
    
    x.ptr = p; 
    
    x.tag = L; //标记为左子树
    
    push(s,x);
    
    p=p->lchild;
    
    }
    
    
    while (!StackEmpty(s) && s.Elem[s.top].tag==R) 
    
    {
    
    x = pop(s);
    
    p = x.ptr;
    
    visite(p->data); //tag为R,表示右子树访问完毕,故访问根结点 
    
    }
    
    
    if (!StackEmpty(s))
    
    {
    
    s.Elem[s.top].tag =R; //遍历右子树
    
    p=s.Elem[s.top].ptr->rchild; 
    
    } 
    
    
    }while (!StackEmpty(s));
    
    }//PostOrderUnrec
    
  • 相关阅读:
    Foj1675数论
    JSTL与EL之间的千丝万缕
    2013多校联合2 I Warm up 2(hdu 4619)
    ios视图切换之push与present混用
    Ruby设计模式透析之 —— 适配器(Adapter)
    晓说智能指针shared_ptr为何可以实现跨模块分配和释放内存
    CSS的力量
    MySQL-select 1;
    MySQL数据库-语言简介
    Eclipse开发工具提交代码
  • 原文地址:https://www.cnblogs.com/goodhacker/p/2083744.html
Copyright © 2011-2022 走看看