- 论坛徽章:
- 0
|
在很久很久以前听说这里的高手很多,今天遇到了个问题。想在这里请教一下,希望有人回答一下。
在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) *~
|
|