我正在尝试将文本文件中的单词加载到二叉搜索树中
I am trying to load words from a text file into a binary search tree
我正在尝试将文本文件中的单词加载到二叉搜索树中。
文件的每一行包含一个词。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node node;
struct node {
node *left, *right;
char *key;
};
node *newnode(char *key)
{
node *n = malloc(sizeof(node));
n->key = malloc(sizeof(key));
strcpy(n->key, key);
n->right = n->left = NULL;
return n;
}
node *insirtNode(node *root, char *key)
{
if (!root)
return newnode(key);
if (strcmp(key, root->key) < 0)
root->left = insirtNode(root->left, key);
else if (strcmp(key, root->key) > 0)
root->right = insirtNode(root->right, key);
return root;
}
void readFromFileToTree(const char *fname, node **root)
{
// read each line
FILE *s = fopen(fname, "r");
if (!s) {
printf("101");
exit(0);
}
char *t = NULL;
do {
char temp[100];
t = fgets(temp, 40, s); // printf("%s ",temp); to check what words are being insirting and whats not
*root = (node *)insirtNode(*root, temp);
} while (t);
fclose(s);
}
int main()
{
node *root = NULL;
readFromFileToTree("anas.txt", &root);
return 0;
}
代码在从文件中读取每个单词之前停止
和 returns -1073740940
为newnode()
中的单词分配的内存大小不正确:n->key = malloc(sizeof(key));
分配的是指针的大小,而不是字符串的长度加上空终止符的额外字节。
您应该使用 strdup()
在单个调用中分配和复制字符串:
node *newnode(const char *key) {
node *n = malloc(sizeof(node));
n->key = strdup(key);
n->right = n->left = NULL;
return n;
}
还要注意这些问题:
printf("101");
是一条神秘的错误消息。你应该使用
fprintf(stderr, "cannot open %s: %s\n", fname, strerror(errno));
并以 non-zero 状态退出;
readFromFileToTree()
中的循环不正确:您将在文件末尾将空指针传递给 insirtNode()
。你应该改用这个:
char temp[100];
while(fgets(temp, sizeof temp, s)) {
*root = insirtNode(*root, temp);
}
这是修改后的版本:
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node node;
struct node {
node *left, *right;
char *key;
};
node *newnode(const char *key) {
node *n = malloc(sizeof(*n));
n->right = n->left = NULL;
n->key = strdup(key);
return n;
}
node *insertNode(node *root, const char *key) {
if (!root)
return newnode(key);
int cmp = strcmp(key, root->key);
if (cmp < 0)
root->left = insertNode(root->left, key);
else
if (cmp > 0)
root->right = insertNode(root->right, key);
return root;
}
node *readFromFileToTree(const char *fname) {
node *root = NULL;
FILE *fp = fopen(fname, "r");
if (!fp) {
fprintf(stderr, "cannot open %s: %s\n", fname, strerror(errno));
return NULL;
}
char temp[100];
while (fgets(temp, sizeof temp, fp)) {
root = insertNode(root, temp);
}
fclose(fp);
return root;
}
void freeTree(node *n) {
if (n) {
freeTree(n->left);
freeTree(n->right);
free(n);
}
}
int main() {
node *root = readFromFileToTree("anas.txt");
freeTree(root);
return 0;
}
我正在尝试将文本文件中的单词加载到二叉搜索树中。
文件的每一行包含一个词。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node node;
struct node {
node *left, *right;
char *key;
};
node *newnode(char *key)
{
node *n = malloc(sizeof(node));
n->key = malloc(sizeof(key));
strcpy(n->key, key);
n->right = n->left = NULL;
return n;
}
node *insirtNode(node *root, char *key)
{
if (!root)
return newnode(key);
if (strcmp(key, root->key) < 0)
root->left = insirtNode(root->left, key);
else if (strcmp(key, root->key) > 0)
root->right = insirtNode(root->right, key);
return root;
}
void readFromFileToTree(const char *fname, node **root)
{
// read each line
FILE *s = fopen(fname, "r");
if (!s) {
printf("101");
exit(0);
}
char *t = NULL;
do {
char temp[100];
t = fgets(temp, 40, s); // printf("%s ",temp); to check what words are being insirting and whats not
*root = (node *)insirtNode(*root, temp);
} while (t);
fclose(s);
}
int main()
{
node *root = NULL;
readFromFileToTree("anas.txt", &root);
return 0;
}
代码在从文件中读取每个单词之前停止 和 returns -1073740940
为newnode()
中的单词分配的内存大小不正确:n->key = malloc(sizeof(key));
分配的是指针的大小,而不是字符串的长度加上空终止符的额外字节。
您应该使用 strdup()
在单个调用中分配和复制字符串:
node *newnode(const char *key) {
node *n = malloc(sizeof(node));
n->key = strdup(key);
n->right = n->left = NULL;
return n;
}
还要注意这些问题:
printf("101");
是一条神秘的错误消息。你应该使用fprintf(stderr, "cannot open %s: %s\n", fname, strerror(errno));
并以 non-zero 状态退出;
readFromFileToTree()
中的循环不正确:您将在文件末尾将空指针传递给insirtNode()
。你应该改用这个:char temp[100]; while(fgets(temp, sizeof temp, s)) { *root = insirtNode(*root, temp); }
这是修改后的版本:
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node node;
struct node {
node *left, *right;
char *key;
};
node *newnode(const char *key) {
node *n = malloc(sizeof(*n));
n->right = n->left = NULL;
n->key = strdup(key);
return n;
}
node *insertNode(node *root, const char *key) {
if (!root)
return newnode(key);
int cmp = strcmp(key, root->key);
if (cmp < 0)
root->left = insertNode(root->left, key);
else
if (cmp > 0)
root->right = insertNode(root->right, key);
return root;
}
node *readFromFileToTree(const char *fname) {
node *root = NULL;
FILE *fp = fopen(fname, "r");
if (!fp) {
fprintf(stderr, "cannot open %s: %s\n", fname, strerror(errno));
return NULL;
}
char temp[100];
while (fgets(temp, sizeof temp, fp)) {
root = insertNode(root, temp);
}
fclose(fp);
return root;
}
void freeTree(node *n) {
if (n) {
freeTree(n->left);
freeTree(n->right);
free(n);
}
}
int main() {
node *root = readFromFileToTree("anas.txt");
freeTree(root);
return 0;
}