免费注册 查看新帖 |

Chinaunix

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

[C] 关于一个二叉树的问题,请高手们回答 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-12-21 21:03 |只看该作者 |倒序浏览
    在很久很久以前听说这里的高手很多,今天遇到了个问题。想在这里请教一下,希望有人回答一下。
在LINUX环境:
在编译出现了一个段错误,看来很久还是找不到原因,求解感激不尽!!,源码在附件中。



main.c

#include "treeHead.h"
//#include "orther.h"
//#define form "%c"
//char nil=' ';

void visitT(tElemType e)
{
        printf(form" ",e);
}
void main(void)
{
        int i;
        biTree T,p,c;
        tElemType e1,e2;
        initBiTree(&T);

        printf("构造空二叉树后,树空否?%d  (1:是 0:否) 树的深度=%d\n",\
                        biTreeEmpty(T),biTreeDepth(T));
        /*二叉树的根节点*/
        e1=root(T);
        if(e1!=' ')
                printf("二叉树的根为:"form"\n",e1);
        else
                printf("树空,无跟\n");

        printf("请先输入二叉树(如:ab三个空格表示a为根结点,\
                b为左子树的二叉树)\n");
        /*向二叉树插入数据*/
        createBiTree(&T);
        printf("构造空二叉树后,树空否?%d  (1:是 0:否) 树的深度=%d\n",\
                        biTreeEmpty(T),biTreeDepth(T));
        /*二叉树的根节点*/
        e1=root(T);
        if(e1!=' ')
                printf("二叉树的根为:"form"\n",e1);
        else
                printf("树空,无跟\n");


        printf("中序递归遍历二叉树:\n");
        inOrderTraverse(T,visitT);
        printf("\n");

        printf("\n后序递归遍历二叉树:\n");
        postOrderTraverse(T,visitT);

        printf("\n请输入一个结点的值: ");
        scanf("%s",&e1);
        printf("\ne1=%c \n",e1);

        p=point(T,&e1);/*p为e1的指针*/
        printf("结点的值为"form"\n",value(p));
}
//------------------------------------------------------------------//
function.c
#include "treeHead.h"
//#include "orther.h"
tElemType nil=' ';

void initBiTree(biTree *T)
{
        *T=NULL;
}
tElemType biTreeEmpty(biTree T)/*判断树是否为空*/
{
        if(T)
                return FALSE;
        else
                return TRUE;
}
tElemType biTreeDepth(biTree T)/*判断树的深度是否为空*/
{
        tElemType i,j;
        if(!T)
                return 0;/*空树深度为0*/
        if(T->lchild)
                i=biTreeDepth(T->lchild);/*i为左子树的深度*/
        else
                i=0;

        if(T->rchild)
                j=biTreeDepth(T->rchild);/*j为右子树的深度*/
        else
                j=0;
        return i>j?i+1:j+1;/*T的深度未其左右子树的深度中的大者+1*/
}

tElemType root(biTree T)
{//初始条件:二叉树T存在.操作结果:返回T的根
        if(biTreeEmpty(T))
                return nil;
        else
                return T->data;
}
void createBiTree(biTree *T)
{
        tElemType ch;
        scanf(form,&ch);
        if(ch==nil)/*空*/
                *T=NULL;
        else
        {/*生成根结点*/
                *T=(biTree)malloc(sizeof(struct biTNode));
                if(!*T)
                        exit(OVERFLOW);
                (*T)->data=ch;/*给根结点赋值*/
                createBiTree(&(*T)->lchild);/*构造左子树*/
                createBiTree(&(*T)->rchild);/*构造右子树*/
        }
}

/*中序递归遍历二叉树*/
void inOrderTraverse(biTree T,void(*visit)(tElemType))
{
        if(T)
        {
                inOrderTraverse(T->lchild,visit);/*先中序遍历左子树*/
                visit(T->data);/*打印结点*/
                inOrderTraverse(T->rchild,visit);/*最后中序遍历右子树*/
        }
}

/*后序递归遍历二叉树*/
void postOrderTraverse(biTree T,void(*visit)(tElemType))
{
        if(T)
        {
                postOrderTraverse(T->lchild,visit);/*先后序遍历左子树*/
                postOrderTraverse(T->rchild,visit);/*再后序遍历右子树*/
                visit(T->data);/*打印结点*/
        }
}

//----------------------------------------------------------------------------

/*bo3-2.cpp 链队列(存储结构由c3-2.h定义)的基本操作(9个)*/
void initQueue(struct linkQueue *q)
{/*构造一个空队列q*/
        if(!((*q).front=(*q).rear=(queuePtr)malloc(sizeof(struct qNode))))
                exit(OVERFLOW);
        (*q).front->next=NULL;
}

/*插入元素e为Q的新的队尾元素 |Bo3-2.cpp|*/
void enQueue(struct linkQueue *q,qElemType e)
{
        queuePtr p;
        if(!(p=(queuePtr)malloc(sizeof(struct qNode))))/*存储分配失败*/
                exit(OVERFLOW);
        p->data=e;
        p->next=NULL;
        (*q).rear->next=p;
        (*q).rear=p;
}

/*若队列不空,删除Q的队头元素,用e返回其值,并返回OK,否则返回ERROR*/
int deQueue(struct linkQueue *q,qElemType *e)
{
        queuePtr p;
        if((*q).front==(*q).rear)
                return ERROR;
        p=(*q).front->next;
        *e=p->data;
        (*q).front->next=p->next;
        if((*q).rear==p)
                (*q).rear=(*q).front;
        free(p);
        return OK;
}

int queueEmpty(struct linkQueue q)
{
        if(q.front->next==NULL)
                return TRUE;
        else
                return FALSE;
}

/*查找*/
biTree point(biTree T,tElemType *s)
{
        qElemType a;
        struct linkQueue q;

        if(T)/*非空树*/
        {
                initQueue(&q);
                enQueue(&q,T);
                while(!queueEmpty(q))/*队不空*/
                {
                        deQueue(&q,&a);/*出队,队列元素赋给a*/
                        if(a->data==*s)
                        {
                                return a;
                        }
                        if(a->lchild)/*有左孩子*/
                        {
                                enQueue(&q,a->lchild);/*入队左孩子*/
                        }
                        if(a->rchild)
                        {
                                enQueue(&q,a->rchild);/*入队右孩子*/
                        }
                }
        }
        return NULL;
}

tElemType value(biTree p)
{
        return p->data;
}
//------------------------------------------------------------------//
treeHead.h
#include<string.h>
#include<ctype.h>
#include<malloc.h> // malloc()等
#include<limits.h> // INT_MAX等
#include<stdio.h> // EOF(=^Z或F6),NULL
#include<stdlib.h> // atoi()
#include<io.h> // eof()
#include<math.h> // floor(),ceil(),abs()
//#include<process.h> // exit()
//#include<iostream.h> // cout,cin

// 函数结果状态代码
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
//#define OVERFLOW -2

typedef char tElemType;

//tElemType nil=' ';
#define form "%c"

typedef struct biTNode
{
        tElemType data;
        struct biTNode *lchild,*rchild;
}biTNode,*biTree;


typedef biTree qElemType; // 设队列元素为二叉树的指针类型
typedef struct qNode
{
        qElemType data;
        struct qNode *next;
}*queuePtr;

struct linkQueue
{
        queuePtr front,rear; // 队头、队尾指针
};
               

/*
#define CHAR 1
//#define INT 1
#ifdef CHAR
        typedef char tElemType;
        tElemType nil=' ';
#define form "%c"
#endif

#ifdef INT
        typedef int tElemType;
        tElemType nil=0;
#define form "%d"
#endif
*/
//------------------------------------------------------------------//
orther.h

char nil=' ';
//------------------------------------------------------------------//
Makefile

AIM:=main
$(AIM):main.c function.c treeHead.h
        gcc $^ -o $@
.PHONY:c
c:
        -rm $(AIM) *~


code.rar

6.72 KB, 下载次数: 0

论坛徽章:
12
巳蛇
日期:2013-09-16 15:32:242015年辞旧岁徽章
日期:2015-03-03 16:54:152015年亚洲杯之约旦
日期:2015-02-11 14:38:37双鱼座
日期:2015-01-05 11:05:47戌狗
日期:2014-12-08 09:41:18戌狗
日期:2014-08-15 09:29:29双子座
日期:2014-08-05 09:17:17卯兔
日期:2014-06-08 15:32:18巳蛇
日期:2014-01-27 08:47:08白羊座
日期:2013-11-28 21:04:15巨蟹座
日期:2013-11-13 21:58:012015年亚洲杯之科威特
日期:2015-04-17 16:51:51
2 [报告]
发表于 2014-12-22 09:07 |只看该作者
用gdb跟一下就好了。gdb的用法可以搜索一下,基本用法(需要在编译的时候打开调试信息,关闭优化):

gdb ./main    (调试main程序)
r                  (开始执行,对于你的情况,程序会自动中断在段错误的地方)

然后按Ctrl-x a  (先按ctrl + x,松开再按a),就可以看到在什么地方段错误的,可以用p查看变量的值。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP