C 二叉树实现中的分段错误
我提前就这一个的源代码是多长时间表示歉意。 我不完全确定我在这里出了什么错。我编写了一个二叉树实现和一些树遍历函数。将一个项目添加到树中会起作用,但添加多个项目会导致分段错误。DDD在回溯中列出了几个函数:isfull、addToTree和isEmpty 该程序跨越三个源文件(对不起)C 二叉树实现中的分段错误,c,segmentation-fault,C,Segmentation Fault,我提前就这一个的源代码是多长时间表示歉意。 我不完全确定我在这里出了什么错。我编写了一个二叉树实现和一些树遍历函数。将一个项目添加到树中会起作用,但添加多个项目会导致分段错误。DDD在回溯中列出了几个函数:isfull、addToTree和isEmpty 该程序跨越三个源文件(对不起) /*------------------------------------Tree.h----------------------------------*/ #ifndef树 #定义树 #定义最大高度4 类型
/*------------------------------------Tree.h----------------------------------*/
#ifndef树
#定义树
#定义最大高度4
类型定义结构类别{
字符*名称;
智力年龄;
}项目;
类型定义结构节点{
物品;
}节点;
类型定义结构树{
节点*根;
结构树*左,*右;
内部叶数;
}树木;
void initializeTree(树*);
树(树*);
int等空(常数树*);
int是完整的(常数树*);
树*searchTree(项*thing,树*t,int(*)(常量项*,常量项*);
int addToTree(项*,树*,int(*)(常数项*,常数项*);
int removeFromTree(项*,树*);
int itemComp(常数项*,常数项*);
#恩迪夫
/*----------------------------------树----------------------------------*/
#包括“Tree.h”
#包括
#包括
#包括
#包括
节点*复制节点(项*);
/*定义:树
节点*根;
内部叶数*/
节点*copyToNode(项*thing){
Node*tmp=malloc(sizeof(Node));
if(tmp==NULL)
返回tmp;
tmp->thing=*thing;
返回tmp;
}
void initializeTree(树*t){
t->root=NULL;
t->right=t->left=NULL;
t->叶数=0;
}
int为空(常数树*t){
返回t->root==NULL;
}
int是完整的(常数树*t){
返回t->leafcount==(int)pow(2,MAXHEIGHT)-1;
}
int addToTree(Item*thing,Tree*t,int(*fp)(常数项*,常数项*){
如果(已满(t))
返回0;
if(isEmpty(t)){
节点*当前=复制节点(对象);
如果(当前==NULL){
放置(“无法复制到树上!”);
返回0;
}
t->root=当前;
t->leafcount++;
返回1;
}
if(fp(thing,&t->root->thing)left,fp);
其他的
返回addToTree(thing,t->right,fp);
}
树*searchTree(项*thing,树*t,int(*fp)(常数项*,常数项*){
内部温度;
如果(t->root==NULL)
返回NULL;
否则如果((temp=fp(&t->root->thing,thing))==0)
返回t;
否则如果(温度=-1)
返回searchTree(thing,t->left,fp);
其他的
返回searchTree(thing,t->right,fp);
}
int removeFromTree(项*thing,树*t){
/*Tree*tmp=searchTree(thing,t);
未完成*/
}
void closeTree(树*t){
返回;
}
/*------------------------------树状驱动程序-------------------------------*/
#包括“Tree.h”
#包括
#包括
#包括
#定义MAXNAME 30
int提示用户(无效);
int itemComp(常数项*,常数项*);
Item*createUserItem();
int main(){
int用户选择;
Item*userItem=NULL;
树用户树;
initializeTree(&userTree);
做{
userChoice=promptUser();
开关(用户选择){
案例1:
puts(“输入要添加的cat信息”);
userItem=createUserItem();
if(addToTree(userItem和userTree,itemComp))
puts(“已成功添加Cat!”);
其他的
puts(“无法添加cat!”);
打破
案例2:
puts(“输入要搜索的cat信息”);
userItem=createUserItem();
if(searchTree(userItem,&userTree,itemComp))
放置(“找到猫!”);
其他的
放置(“未找到猫!”);
打破
案例3:
if(isEmpty(&userTree))
puts(“树是空的!”);
其他的
放置(“树不是空的!”);
打破
案例4:
if(isFull(&userTree))
放(“树满了!”);
其他的
放置(“树未满!”);
打破
案例0:
打破
违约:
看跌期权(“非期权!”);
打破
}
}while(用户选择);
}
int itemComp(常数项*物项1,常数项*物项2){
整数比较;
如果(比较=strcmp(东西一->名称,东西二->名称))
收益比较;
返回!(第一件事->年龄==第二件事->年龄);
}
int promptUser(){
int tmp;
puts(“-----菜单------”);
看跌期权(“1.创建cat”);
puts(“2.搜索cat”);
看跌期权(“3.勾选空”);
看跌期权(“4.全额支票”);
卖出(“0.退出”);
scanf(“%d”和tmp);
而(getchar()!='\n')
;
返回tmp;
}
Item*createUserItem(){
项目*tmp=malloc(项目规模);
静态字符名buf[MAXNAME];
printf(“输入猫名:”);
if(fgets(namebuf、MAXNAME、stdin)=NULL)
返回NULL;
tmp->name=malloc(strlen(namebuf));
strcpy(tmp->name,namebuf);
printf(“输入cat年龄:\t”);
scanf(“%d”和&tmp->age);
而(getchar()!='\n')
;
返回tmp;
}
我不太清楚如何解释调试器的回溯。我哪里出错了?
这可能与我如何处理在驱动程序文件中为用户输入创建虚拟项有关吗?我不认为我处理得很好(或者这个项目的其他部分)
感谢免责声明:由于您指定错误存在于您的
addToTree
函数中,因此我没有在其他任何地方查找错误(结构定义除外)
addToTree
函数中有两个错误。首先,您没有正确处理leafcount
。当您将节点推送到树(我的so)中时,您会发现它是不正确的
/*----------------------------------Tree.h----------------------------------*/
#ifndef TREE
#define TREE
#define MAXHEIGHT 4
typedef struct cat{
char *name;
int age;
}Item;
typedef struct node{
Item thing;
}Node;
typedef struct tree{
Node *root;
struct tree *left, *right;
int leafcount;
}Tree;
void initializeTree(Tree *);
void closeTree(Tree *);
int isEmpty(const Tree *);
int isFull(const Tree*);
Tree *searchTree(Item *thing, Tree *t, int (*)(const Item *, const Item *));
int addToTree(Item *, Tree *, int (*)(const Item *, const Item *));
int removeFromTree(Item *, Tree *);
int itemComp(const Item *, const Item *);
#endif
/*----------------------------------Tree.c----------------------------------*/
#include "Tree.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
Node *copyToNode(Item *);
/*Definition: Tree
Node *root;
int leafcount; */
Node *copyToNode(Item *thing){
Node *tmp = malloc(sizeof(Node));
if(tmp == NULL)
return tmp;
tmp->thing = *thing;
return tmp;
}
void initializeTree(Tree *t){
t->root = NULL;
t->right = t->left = NULL;
t->leafcount = 0;
}
int isEmpty(const Tree *t){
return t->root == NULL;
}
int isFull(const Tree *t){
return t->leafcount == (int)pow(2,MAXHEIGHT) - 1;
}
int addToTree(Item *thing, Tree *t, int (*fp)(const Item *, const Item *)){
if(isFull(t))
return 0;
if(isEmpty(t)){
Node *current = copyToNode(thing);
if(current == NULL){
puts("Couldn't copy to tree!");
return 0;
}
t->root = current;
t->leafcount++;
return 1;
}
if(fp(thing, &t->root->thing) <= 0)
return addToTree(thing, t->left, fp);
else
return addToTree(thing, t->right, fp);
}
Tree *searchTree(Item *thing, Tree *t, int (*fp)(const Item *, const Item *)){
int temp;
if(t->root == NULL)
return NULL;
else if((temp = fp(&t->root->thing, thing)) == 0)
return t;
else if(temp = -1)
return searchTree(thing, t->left, fp);
else
return searchTree(thing, t->right, fp);
}
int removeFromTree(Item *thing, Tree *t){
/*Tree *tmp = searchTree(thing, t);
Not finished*/
}
void closeTree(Tree *t){
return;
}
/*------------------------------TreeDriver.c-------------------------------*/
#include "Tree.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXNAME 30
int promptUser(void);
int itemComp(const Item *, const Item *);
Item * createUserItem();
int main(){
int userChoice;
Item * userItem = NULL;
Tree userTree;
initializeTree(&userTree);
do{
userChoice = promptUser();
switch(userChoice){
case 1:
puts("Enter cat information to add");
userItem = createUserItem();
if(addToTree(userItem, &userTree, itemComp))
puts("Cat successfully added!");
else
puts("Could not add cat!");
break;
case 2:
puts("Enter cat information to search for");
userItem = createUserItem();
if(searchTree(userItem, &userTree, itemComp))
puts("Cat found!");
else
puts("Cat not found!");
break;
case 3:
if(isEmpty(&userTree))
puts("Tree is empty!");
else
puts("Tree is not empty!");
break;
case 4:
if(isFull(&userTree))
puts("Tree is full!");
else
puts("Tree is not full!");
break;
case 0:
break;
default:
puts("Not an option!");
break;
}
}while(userChoice);
}
int itemComp(const Item *thing_one, const Item *thing_two){
int comparison;
if(comparison = strcmp(thing_one->name, thing_two->name))
return comparison;
return !(thing_one->age == thing_two->age);
}
int promptUser(){
int tmp;
puts("--------MENU---------");
puts("1. Create cat");
puts("2. Search for cat");
puts("3. Check empty");
puts("4. Check full");
puts("0. Quit");
scanf("%d", &tmp);
while(getchar() != '\n')
;
return tmp;
}
Item * createUserItem(){
Item *tmp = malloc(sizeof(Item));
static char namebuf[MAXNAME];
printf("Enter cat name: ");
if(fgets(namebuf, MAXNAME, stdin) == NULL)
return NULL;
tmp->name = malloc(strlen(namebuf));
strcpy(tmp->name, namebuf);
printf("Enter cat age:\t");
scanf("%d", &tmp->age);
while(getchar() != '\n')
;
return tmp;
}
int addToTree( Item *thing, Tree **t, int ( *fp )( const Item *, const Item * ) ) {
if( isFull( *t ) == true) return 0;
if( isEmpty( *t ) == true ) {
Node *current = copyToNode( thing );
if( current == NULL ) {
puts( "Couldn't copy to tree!" );
return 0;
}
( *t ) = current;
return 1;
}
if( fp( thing, t->root->thing ) <= 0 )
return addToTree( thing, &( ( *t )->left ), fp );
else
return addToTree( thing, &( ( *t )->right ), fp );
}
struct BstTree {
struct Tree *root;
int height;
};