繁体   English   中英

分割故障二叉树遍历

[英]Segmentation Fault Binary Tree Traversal

因此,我一直在运行和测试我的代码,直到我为树的预遍历和后遍历添加了2个更多函数之前,一切似乎都一直在工作。

任务是为具有随机数字集的输入文件创建链接列表和树。 链接列表和树遍历都需要在单独的函数中打印出来,而我找不到我哪里出错了。

我不断

分段故障(核心已转储)

这是我的代码:

 //Tristan Shepherd #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> struct node { int num; struct node *next; }; struct tree { int numt; struct tree *left; struct tree *right; }; typedef struct node LINK; typedef struct tree branch; int searchList(int number, LINK *head) { LINK *current; current = head; while (current != NULL) { if (current->num == number) return 1; // Found it. current = current->next; } return 0; // Did not find it. } LINK *insertList(int number, LINK *head) { LINK *current, *temp; if (searchList(number, head) == 1) return head; temp = (LINK *)malloc(sizeof(LINK)); temp->num = number; if (head == NULL) { head = temp; temp->next = NULL; return head; } current = head; if (current->num == number) { temp->next = current; head = temp; return head; } current = head; while (current != NULL) { if (current->next == NULL || current->next->num == number) { temp->next = current->next; current->next = temp; return head; } current = current->next; } } void printList(LINK *head) { LINK *current; current = head; while (current != NULL) { printf("%i\\n", current->num); current = current->next; } } LINK *freeList(LINK *head) { LINK *current, *temp; current = head; while (current != NULL) { temp = current; current = current->next; free(temp); } free(head); head = NULL; return head; } void freeTree(branch *leaf) { if(leaf != 0) { freeTree(leaf->left); freeTree(leaf->right); free(leaf); } } void insert(int new, branch **leaf) { if(*leaf == 0) { *leaf = (struct tree*) malloc(sizeof(struct tree)); (*leaf)->numt = new; (*leaf)->left = 0; (*leaf)->right = 0; } else if(new < (*leaf)->numt) { insert(new, &(*leaf)->left); } else if(new > (*leaf)->numt) { insert(new, &(*leaf)->right); } } void printInorder(branch *leaf) { if (leaf == NULL) return; printInorder(leaf->left); printf("%i ", leaf->numt); printInorder(leaf->right); } void printPreorder(branch *leaf) { if (leaf == NULL) return; printf("%i ", leaf->numt); printInorder(leaf->left); printInorder(leaf->right); } void printPostorder(branch *leaf) { if (leaf == NULL) return; printInorder(leaf->left); printInorder(leaf->right); printf("%i ", leaf->numt); } int main (void) { int t; FILE *stream = fopen("hw9.data", "r"); LINK *head; branch *leaf; head = NULL; int number; while (1) { fscanf(stream, "%i", &number); if (feof(stream)) break; insert(number, &leaf); head = insertList(number, head); } fclose(stream); printf("\\nPrinting List: \\n"); printList(head); printf("\\n\\nPrinting in order\\n"); printInorder(leaf); printf("\\n\\nPrinting Pre order\\n"); printPreorder(leaf); printf("\\n\\nPrinting Post order\\n"); printPostorder(leaf); head = freeList(head); freeTree(leaf); head = NULL; return 0; } 

branch *leaf; 未初始化。 您的代码期望叶子指针为NULL 尝试branch *leaf = NULL;

而且,看起来freeList释放head两次,这是未定义的行为。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM