| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 501 人关注过本帖
标题:二叉树的创建和遍历,又是内存操作错误,求指教。。。
只看楼主 加入收藏
Xy_betray
Rank: 2
来 自:河南
等 级:论坛游民
帖 子:22
专家分:12
注 册:2012-9-18
结帖率:33.33%
收藏
 问题点数:0 回复次数:1 
二叉树的创建和遍历,又是内存操作错误,求指教。。。
程序代码:
#include<stdio.h>
#include<stdlib.h>

typedef int Telemtype;


typedef struct bitnode
{
    Telemtype data;
    struct bitnode *lchild,*rchild,*parent;
}bitnode,*bitree;

bitree creat_tree(bitree tree,bitree parent);
void pre_order(bitree tree);


int main(void)
{
    bitree root;
    root=(bitree)malloc(sizeof(bitnode));
    root->parent=NULL;
    root=creat_tree(root,root->parent);
    pre_order(root);


    getchar();
    return 0;
}

bitree creat_tree(bitree tree,bitree parent)
{
    Telemtype temp;
    printf("input the data:  \n");
    scanf("%d",&temp);
    if(temp==0)
       tree=NULL;
    else
    {
        if(!(tree=(bitree)malloc(sizeof(bitnode))))
           exit(-1);
        tree->data=temp;
        tree->parent=parent;
        tree->lchild=creat_tree(tree->lchild,tree);
        tree->rchild=creat_tree(tree->rchild,tree);
    }
    return tree;
}

void pre_order(bitree tree)
{
    if(tree)
    {
        printf("%d   parent:%d   \n",tree->data,tree->parent->data);
        pre_order(tree->lchild);
        pre_order(tree->rchild);
    }
}


编译通过,输入序列为1 2 3 0 0 4 5 0 6 0 0 7 0 0 0 输入的0意味空子树,调用遍历函数的时候提示内存错误。。。求指点啊
搜索更多相关主题的帖子: 二叉树 parent 
2013-06-08 16:36
神经不正常
Rank: 2
等 级:论坛游民
帖 子:16
专家分:52
注 册:2013-5-23
收藏
得分:0 
如果你不会写,那么抄一遍代码,然后再写。

“数据结构”上这种例子是有的,也非常完整。先抄,在理解,最后再写。
2013-06-08 19:02
快速回复:二叉树的创建和遍历,又是内存操作错误,求指教。。。
数据加载中...
 
   



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

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