二叉树的遍历(递归)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
typedef int datatype;
typedef struct _bitree_node_ {
    datatype data;
    struct _bitree_node_ *lchild;
    struct _bitree_node_ *rchild;
}bitree;
bitree *creat_bitree_node(datatype value)
{
    bitree *root = (bitree *)malloc(sizeof(bitree));
    memset(root,0,sizeof(bitree));
    root->data = value;
    return root;
}
 
bitree *creat_bitree(datatype min, datatype max)
{
    bitree *root = creat_bitree_node(min);
    if(min * 2 <= max)
        root->lchild = creat_bitree(min * 2,max);
    if(min *2 + 1 <= max)
        root->rchild = creat_bitree(min * 2 + 1,max);
    return root;
}
 
void pre_order(bitree * root)
{
    if(NULL == root)
        return ;
    printf("%3d",root->data);
    pre_order(root->lchild);
    pre_order(root->rchild);
}
void in_order(bitree * root)
{
    if(NULL == root)
        return ;
    in_order(root->lchild);
    printf("%3d",root->data);
    in_order(root->rchild);
}
void post_order(bitree * root)
{
    if(NULL == root)
        return ;
    post_order(root->lchild);
    post_order(root->rchild);
    printf("%3d",root->data);
}
int main()
{
    bitree *root = creat_bitree(1,10);
    puts("pre_order :");
    pre_order(root);
    putchar(10);
    puts("in_order :");
    in_order(root);
    putchar(10);
    puts("post_order :");
    post_order(root);
    putchar(10);
    return 0;

}

原文地址:http://my.oschina.net/u/130360/blog/67743

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值