| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 534 人关注过本帖, 1 人收藏
标题:二叉树 输入问题
只看楼主 加入收藏
xjy080
Rank: 2
等 级:论坛游民
帖 子:55
专家分:78
注 册:2008-9-15
结帖率:60%
收藏(1)
已结贴  问题点数:10 回复次数:5 
二叉树 输入问题
程序一直停留在输入函数中 输出语句没有运行
请问这个是甚么原因

#include <stdio.h>
#include <stdlib.h>

typedef struct tree
{
 char  data;
 struct tree *l;/*左儿子*/
 struct tree *r;
}tr;

main()
{
 tr *head=NULL;
 tr *create();/*二叉树的建立*/
 void preorder();
 void inorder();
 void postorder();
 system("cls");
 head=create(head);
 printf("\n\n");
 printf("\nThe preorder is:");
  preorder(head);
         printf("\nThe inorder is:");
  inorder(head);
  printf("\nThe postorder is:");
  postorder(head);
}
 
tr *create(tr *t)/*二叉树的建立*/
{   
  tr *k=NULL;
  char ch;
  scanf("%s",&ch);
  if(ch=='!')t=NULL;
  else { t=(tr *)malloc(sizeof(tr));
    t->data=ch;
    t->l=t->r=NULL;
    t->l=create(k);
    t->r=create(k);
   }
 return(t);
}
 
void preorder(tr *head)/*先续遍例*/
{ tr *t=NULL;
 t=head;
 if(t)
 { printf("%c\t",t->data);
  preorder(t->l);
  preorder(t->l);
 }
 
}
void inorder(tr *head)/*中续遍例*/
{ tr *t=NULL;
 t=head;
 if(t)
 {
  inorder(t->l);
  printf("%c\t",t->data);
  inorder(t->l);
 }
}
 
void postorder(tr *head)/*后续遍例*/
{ tr *t=NULL;
 t=head;
 if(t)
 {
  postorder(t->l);
  postorder(t->l);
  printf("%c\t",t->data);
 }
}
搜索更多相关主题的帖子: 二叉树 输入 
2010-04-16 19:54
tdy1006
Rank: 4
等 级:业余侠客
帖 子:173
专家分:240
注 册:2009-5-13
收藏
得分:5 
tr *create();/*二叉树的建立*/
tr *create(tr *t)/*二叉树的建立*/
{   
  tr *k=NULL;
  char ch;
…………
2010-04-16 22:11
xjy080
Rank: 2
等 级:论坛游民
帖 子:55
专家分:78
注 册:2008-9-15
收藏
得分:0 
可不可以说的清楚一点……
2010-04-17 15:43
玩出来的代码
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:河南新乡
等 级:贵宾
威 望:11
帖 子:742
专家分:2989
注 册:2009-10-12
收藏
得分:5 
程序代码:
#include <stdio.h>
#include <stdlib.h>

typedef struct tree
{
    char  data;
    struct tree *l;/*左儿子*/
    struct tree *r;
}tr;

tr *create()/*二叉树的建立*/
{
    tr *k=NULL;                   //这里改了下,有返回值就不用传参数了,
    char ch;
    scanf("%c",&ch);
    if (ch=='!') k=NULL;
    else
    {
        k=(tr *)malloc(sizeof(tr));
        k->data=ch;
        k->l=k->r=NULL;
        k->l=create();
        k->r=create();
    }
    return(k);
}

void preorder(tr *head)/*先续遍例*/
{
    tr *t=NULL;
    t=head;
    if (t)
    {
        printf("%c\t",t->data);
        preorder(t->l);                       
        preorder(t->r);                              //////这里你都写错了。
    }

}
void inorder(tr *head)/*中续遍例*/
{
    tr *t=NULL;
    t=head;
    if (t)
    {
        inorder(t->l);
        printf("%c\t",t->data);
        inorder(t->r);
    }
}

void postorder(tr *head)/*后续遍例*/
{
    tr *t=NULL;
    t=head;
    if (t)
    {
        postorder(t->l);
        postorder(t->r);
        printf("%c\t",t->data);
    }
}
main()
{
    tr *head=NULL;
    head=create();
    printf("\n\n");
    printf("\nThe preorder is:");
    preorder(head);
    printf("\nThe inorder is:");
    inorder(head);
    printf("\nThe postorder is:");
    postorder(head);
}

离恨恰如春草,更行更远还生。
2010-04-17 16:08
xjy080
Rank: 2
等 级:论坛游民
帖 子:55
专家分:78
注 册:2008-9-15
收藏
得分:0 
我用vc编译 通过了 但是输入的时候 问题还是没有解决 请问这个事甚么原因 啊
2010-04-20 22:02
xjy080
Rank: 2
等 级:论坛游民
帖 子:55
专家分:78
注 册:2008-9-15
收藏
得分:0 
#include <stdio.h>
#include <stdlib.h>

typedef struct tree
{
    char  data;
    struct tree *l;/*左儿子*/
    struct tree *r;
}tr;

main()
{
    tr *head=NULL;
    tr *create();/*二叉树的建立*/
    void preorder();
    void inorder();
    void postorder();
    system("cls");
    head=create(head);
    printf("\n\n");

    printf("\nThe preorder is:");
    preorder(head);

    printf("\nThe inorder is:");
    inorder(head);

    printf("\nThe postorder is:");
    postorder(head);
}

tr *create(tr *t)/*二叉树的建立*/
{   
    tr *k=NULL;
    char ch;
    scanf("%c",&ch);
    if(ch=='!')t=NULL;
    else
    {
        t=(tr *)malloc(sizeof(tr));
        t->data=ch;
        t->l=t->r=NULL;
        t->l=create(k);
        t->r=create(k);
   }
    return(t);
}

void preorder(tr *head)/*先续遍例*/
{
    tr *t=NULL;
    t=head;
    if(t)
    {
        printf("%c\t",t->data);
        preorder(t->l);
        preorder(t->r);
    }

}

void inorder(tr *head)/*中续遍例*/
{
    tr *t=NULL;
    t=head;
    if(t)
    {
        inorder(t->l);
        printf("%c\t",t->data);
        inorder(t->r);
    }
}

void postorder(tr *head)/*后续遍例*/
{
    tr *t=NULL;
    t=head;
    if(t)
    {
        postorder(t->l);
        postorder(t->r);
        printf("%c\t",t->data);
    }
}





在输入过程中每个结点加两个!!即可
2010-05-07 17:18
快速回复:二叉树 输入问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015824 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved