二叉树遍历的递归与非递归算法

#include<stdio.h>
#include<stdlib.h>
typedef char datatype ;


typedef struct node
{
     datatype data;/*数据域*/
     struct node *lchild,*rchild; /*左、右孩子域*/
}BTNode;

typedef struct stack{
    BTNode *node[10] ;
    int top ;
}SStack ;

SStack * createemptyStack(){
    SStack *st = (SStack *)malloc(sizeof(SStack)) ;
    st->top = 0 ;
    return st ;
}

void push(SStack *st , BTNode *bt){
    if(st->top == 10) return ;
    st->node[st->top] = bt ;
    st->top ++ ;
}

void pop(SStack *st){
    if(st->top == 0) return ;
    st->top -- ;
}

BTNode* getTop(SStack *st){
    if(st->top == 0) return NULL ;
    return st->node[st->top - 1] ;   
}
/*---------------------------------------------------------------------------------------------------*/
/*按先序建立二叉树的二叉链表。函数的返回值指向根结点*/
BTNode* createbintree( )
{  
    char ch;
    BTNode* bt;
    ch=getchar();/*键盘上输入一个字符*/
    if (ch=='#')
        return(NULL);/*#作为结束标志*/
    else {
        bt=(BTNode*)malloc(sizeof(BTNode));/*生新结点 */
        bt->data=ch;
        bt->lchild= createbintree( );
        bt->rchild= createbintree( );
        return (bt);
    }
}
/*---------------------------------------------------------------------------------------------------*/
/*二叉树中序遍历的递归算法,t为指向根结点*/
void inorder(BTNode* t)
{
    if(t == NULL)
        return ;
    inorder(t->lchild) ;
    printf("%c " , t->data) ;
    inorder(t->rchild) ;
}
/*---------------------------------------------------------------------------------------------------*/
/*二叉树先序遍历的非递归算法,t为指向根结点*/
void  preorder(BTNode* t)
{
    SStack *st = NULL ;
    BTNode *p = NULL ;
    if(t == NULL)
        return ;
    st = createemptyStack() ;
    p = t ;
    do{
        while(p){
            printf("%c " ,p->data) ;
            if(p->rchild)
                push(st , p->rchild) ;
            p = p->lchild ;
        }
        if(st->top != 0){
            p = getTop(st) ;
            pop(st) ;
        }
    }while(st->top != 0 || p) ;
}
/*---------------------------------------------------------------------------------------------------*/
/*求以root为根的二叉树的叶子结点个数,root指向根结点,返回值为叶子结点个数*/
int leaf(BTNode* root)
{
    int leafs = 0 ;
    if(root == NULL) {
        leafs = 0 ;
    }else if(root->lchild == NULL && root->rchild == NULL)
        leafs++ ;   
    else {
        leafs += leaf(root->lchild) ;
        leafs += leaf(root->rchild) ;
    }
    return  leafs ;
}
/*---------------------------------------------------------------------------------------------------*/
void main()
{
    BTNode*bt;
    bt= createbintree( ) ;


    /*按先序遍历二叉树*/
    printf("先序遍历为:") ;
    preorder(bt) ;
    printf("/n") ;

    /*按中序遍历二叉树*/
    printf("中序遍历为:") ;
    inorder(bt) ;
    printf("/n") ;

    /*输出叶子个数*/
    printf("叶子个数为:%d /n",leaf(bt)) ;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值