免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1836 | 回复: 5
打印 上一主题 下一主题

二叉树的先序创建和遍历 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-11-30 17:04 |只看该作者 |倒序浏览
#include <stdio.h>
#include <stdlib.h>

typedef char elemtype;

typedef struct node {
        elemtype data;
        struct node *lchild;
       struct node  *rchild;
}BinTNode,*BinTree;



void CreatBinTree (BinTree *t);
void preorder (BinTree t);


void main ()
{  BinTree  t;
     
        CreatBinTree(&t);
        
        printf ("Print Previous Order:\n");
        preorder (t);
}



void CreatBinTree (BinTree *t)
{
        char ch;
      
        scanf ("%c", &ch);
        if (ch == ' ')
           t = NULL;
        else {
               *t = malloc (sizeof (BinTNode));
               (*t)->data=ch;
                CreatBinTree (&((*t) -> lchild));
                CreatBinTree (&((*t) -> rchild));
        }        
}




void preorder (BinTree t)
{
        if (t!=NULL){
                printf (" %c ", t -> data);
                preorder (t -> lchild);
                preorder (t -> rchild);
        }
        
}

论坛徽章:
0
2 [报告]
发表于 2006-12-01 13:31 |只看该作者
没问题, 写得好!

论坛徽章:
0
3 [报告]
发表于 2007-03-14 01:20 |只看该作者
好像是死循环。

论坛徽章:
0
4 [报告]
发表于 2007-03-14 08:10 |只看该作者
原帖由 ktzlj 于 2006-11-30 17:04 发表
               *t = malloc (sizeof (BinTNode));
               (*t)->data=ch;
                CreatBinTree (&((*t) -> lchild));
                CreatBinTree (&((*t) -> rchild));



看错了看错了,嘿嘿,没看清定义,不好意思

[ 本帖最后由 jemy.zhang 于 2007-3-14 13:15 编辑 ]

论坛徽章:
2
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:56:11
5 [报告]
发表于 2007-03-14 10:53 |只看该作者


  1. void CreatBinTree (BinTree *t)
  2. {
  3.         char ch;
  4.       
  5.         scanf ("%c", &ch);
  6.         if (ch == ' ')
  7.            *t = NULL;
  8.         else {
  9.                *t = malloc (sizeof (BinTNode));
  10.                (*t)->data=ch;
  11.                 CreatBinTree (&(*t) -> lchild);
  12.                 CreatBinTree (&(*t) -> rchild);
  13.         }        
  14. }
复制代码

论坛徽章:
0
6 [报告]
发表于 2007-03-14 15:49 |只看该作者
收了,顺便顶下!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP