我的函数bst_insert_node()
中遇到了分割错误。左边是节点的左"子节点",其电话值始终小于其父级。右节点是节点的正确"子节点",其电话价值始终大于其父节点。Bst 代表 二叉搜索树。我的主要功能在另一个文件中。主要问题应该是我的函数bst_insert_node()
。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "introprog_telefonbuch.h"
// Adds a node with the given phone number (phone) and the name of the owner of that phone number
// into the binary search tree.
void bst_insert_node(bstree* bst, unsigned long phone, char *name) {
bst_node* new = malloc(sizeof(bst_node));
name = malloc(sizeof(char) *MAX_STR);
snprintf(name, MAX_STR, "%s",name);
new->left = NULL;
new->right = NULL;
new->parent = NULL;
new->phone = phone;
if(bst == NULL){
bst->root = new;
}
bst_node* finder = bst->root;
while(finder != NULL){
if(finder->phone == phone){
printf("This phone number already exist");
return;
}
if(phone < finder->phone){
if(finder->left == NULL){
new->parent = finder;
finder->left = new;
return;
}
finder = finder->left;
}
if(phone > finder->phone){
if(finder->right == NULL){
new->parent = finder;
finder->right = new;
return;
}
finder = finder->right;
}
}
}
// This function returns a pointer that points to the node that contains the phone
// number that is being searched. If such a phone number is non existent,
// you return NULL.
bst_node* find_node(bstree* bst, unsigned long phone) {
bst_node* finder = bst->root;
while(finder != NULL){
if(finder->phone == phone){
return finder;
}
if(finder->phone > phone){
finder = finder->left;
}
else{
finder = finder->right;
}
}
return NULL;
}
// print a sub-tree in "in-order" order
void bst_in_order_walk_node(bst_node* node) {
if (node == NULL){
return;
}
else{
bst_in_order_walk_node(node->left);
print_node(node);
bst_in_order_walk_node(node->right);
}
}
// the same as bst_in_order_walk_node just that you do it for the whole tree.
void bst_in_order_walk(bstree* bst) {
if (bst != NULL) {
bst_in_order_walk_node(bst->root);
}
}
// deletes the all sub-tree of node
void bst_free_subtree(bst_node* node) {
if(node == NULL){
return;
}
else{
bst_free_subtree(node->left);
bst_free_subtree(node->right);
free(node->name);
free(node);
}
}
// Deletes the whole tree
void bst_free_tree(bstree* bst) {
if(bst != NULL && bst->root != NULL) {
bst_free_subtree(bst->root);
bst->root = NULL;
}
}
这是主函数和界面功能:
// Kreiert eine Benutzeroberfläche
bstree* interface(bstree *bst) {
help();
char *operation;
unsigned long phone;
char *name;
read_line_context in;
open_stdin(&in);
printf("> ");
while (read_line(&in, &operation, &phone, &name) == 0) {
if (operation != NULL) {
if (operation[0] == '?' && phone > 0) {
find_and_print(bst, phone);
} else if (operation[0] == '+' && phone > 0 && strlen(name) > 0) {
bst_insert_node(bst, phone, name);
} else if (operation[0] == 'd') {
debug(bst);
} else if (operation[0] == 'p') {
bst_in_order_walk(bst);
} else if (operation[0] == 'q') {
break;
} else {
printf("Inkorrekte Eingabenn");
help();
}
}
printf("> ");
phone = -1;
}
printf("Exiting...n");
close_file(&in);
return bst;
}
int main(int argc, char** argv) {
// Create an empty search tree
bstree bst;
bst.root = NULL;
bst.count = 0;
if (argc != 2)
{
printf("Nutzung: %s <Dateiname>n",argv[0]);
return 1;
}
// reading the txt file
read_file(argv[1], &bst);
// creating the interface
interface(&bst);
bst_free_tree(&bst);
return 0;
}
该程序应该获取一个包含所有电话号码和名称的文本文件,并将其放入二叉搜索树中。我的程序正在编译,但由于某种原因,当我按"p"(按 p 应该显示"按顺序"排序的电话号码)时,没有显示电话号码,程序只是再次等待另一个输入。
一个可能的段错误在这里:
if(bst == NULL){
bst->root = new;
}
您不能将任何内容分配给 bst 的成员,因为它是空的。也许你的意思是在这里构建一个新的BST?其他一些注意事项...
- 就个人而言,我会避免使用令牌
new
因为它会C++程序员感到困惑。 - 应该检查 malloc 的结果以防它失败,但更重要的是
snprintf()
不应该写入它正在读取的缓冲区。