数据结构二叉树非递归中序遍历,层次遍历

非递归中序遍历(栈)

Status Inordernone(BiTree T)
{
    LinkStack S;
    BiTree p;
    InitStack(S);
    Push(S,T);
    while(!StackEmpty(S))
    {
        while(GetTop(S,p)&&p)
            Push(S,p->lchild);
        Pop(S,p);
        if(!StackEmpty(S))
        {
            Pop(S,p);
            printf("%c",p->data);
            Push(S,p->rchild);
        }
    }
    return OK;
}

层次遍历(队列)

Status LevelOrder(BiTree T)
{
    LinkQueue Q;
    InitQueue(Q);
    BiTree b;
    if(T!=NULL)
    {
        EnQueue(Q,T);
        while(!QueueEmpty(Q))
        {
            DeQueue(Q,b);
            printf("%c",b->data);
            if(b->lchild!=NULL)
                EnQueue(Q,b->lchild);
            if(b->rchild!=NULL)
                EnQueue(Q,b->rchild);
        }
    }
    return OK;
}

posted on 2017-11-30 10:46  TUTWO  阅读(759)  评论(0编辑  收藏  举报

导航