Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7539367
  • 博文数量: 961
  • 博客积分: 15795
  • 博客等级: 上将
  • 技术积分: 16612
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-07 14:23
文章分类

全部博文(961)

文章存档

2016年(1)

2015年(61)

2014年(41)

2013年(51)

2012年(235)

2011年(391)

2010年(181)

分类: C/C++

2011-05-31 10:34:05

/*

 *  二叉树简单实现、递归方式

 *  Lzy     2011-5-24

 */

#include

#define ElemType int

 

typedef struct node

{

    ElemType data;              /*数据域*/

    struct node *left;          /*左子树指针*/

    struct node *right;         /*右子树指针*/

}BTree;

 

void BTreeCreate(BTree **tp)        /*二叉树的构建*/

{

    int x;

    printf("输入(小于0不输入):");

    scanf("%d",&x);

    if(x < 0)                   /*递归条件结束*/

    {

        tp = NULL;

        return ;

    }

    (*tp) = (BTree *)malloc(sizeof(BTree));     /*分配内存空间*/

    (*tp)->data = x;                                /*数据域赋值*/

 

    BTreeCreate(&((*tp)->left));                        /*递归调用*/

    BTreeCreate(&((*tp)->right));

    return ;

}

 

void PreOrder(BTree *tp)                /*前序遍历、递归方式*/

{

    if(tp == NULL)

        return ;

    else

    {

        printf("%d ",tp->data);         /*访问根节点*/

        PreOrder(tp->left);             /*遍历左子树*/

        PreOrder(tp->right);            /*遍历右子树*/

    }

}

 

int main(void)

{

    BTree *root;

    BTreeCreate(&root);

    PreOrder(root);

}

阅读(1534) | 评论(0) | 转发(2) |
0

上一篇:二分查找

下一篇:归并排序_递归方法实现

给主人留下些什么吧!~~