加载数据构建三叉树不能正常工作(C)
Loading data to construct a ternary tree not working properly(C)
我已经构建了一个三叉树结构以及一些函数来对其进行基本操作。其中之一是从文件中逐字读取并构建树!
问题可能出在函数 read_words 中。
当我手动插入数据时,数据工作正常,但是当我尝试从文件中插入时,它只会使树包含最后一个数据输入的内容。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
typedef struct a{
char * word ;
int occurs;
struct a * left;
struct a * same;
struct a * right; } Node;
typedef Node * Node_ptr ;
typedef Node * TriTree ;
void inorder(TriTree x) {
if(x==NULL) return;
inorder(x->left);
printf("%s(%d)--" , x->word, x->occurs);
inorder(x->same);
inorder(x->right);
return;}
void strlower(char * lower){
for (char *p = lower; *p; ++p) *p = tolower(*p);
// printf("%s\n",lower);
};
// 1
Node_ptr create(char * word){
Node_ptr tmp_ptr;
tmp_ptr = (Node_ptr)malloc(sizeof(Node));
tmp_ptr-> word = word;
tmp_ptr-> occurs = 1;
tmp_ptr-> left = NULL;
tmp_ptr-> same = NULL;
tmp_ptr-> right = NULL;
return tmp_ptr;
}
TriTree insert(TriTree x, Node_ptr node_ptr){
if(x==NULL){
// printf("%s\n","Empty Tree!");
x = node_ptr;
return x;
}
int ret;
strlower(x->word);
strlower(node_ptr->word);
ret = strcmp(x->word,node_ptr->word);
if(ret < 0) {
// printf("str1 is less than str2");
x->right = insert(x->right,node_ptr);
} else if(ret > 0) {
// printf("str2 is less than str1");
x->left = insert(x->left,node_ptr);
} else {
// printf("str1 is equal to str2");
x->same = insert(x->same,node_ptr);
}
return x;
} ;
TriTree read_words (FILE *f,TriTree x) {
char c[1024];
while (fscanf(f, " %1023s", c) == 1) {
Node_ptr tmp;
// printf("%s\n",c);
tmp = create(c);
printf("%s\n",tmp->word);
x = insert(x,tmp);
//free(tmp);
}
fclose(f);
return x;
}
int main()
{
TriTree x;
x = NULL;
FILE * fp = fopen("input.txt", "r");
x = read_words(fp,x);
inorder(x);
return 0;
}
input:
hello bye ask life BYE Hello night HeLLO
desired output:
ask bye BYE hello Hello HeLLo life night
my output:
hello hello hello hello hello hello hello hello
在函数中创建
Node_ptr create(char * word){
Node_ptr tmp_ptr;
tmp_ptr = (Node_ptr)malloc(sizeof(Node));
tmp_ptr-> word = word;
//...
您正在将用作参数的指针字分配给数据成员tmp_ptr->字。
但是您在每次从函数调用 create 时都传递了相同的指针 read_words
TriTree read_words (FILE *f,TriTree x) {
char c[1024];
while (fscanf(f, " %1023s", c) == 1) {
Node_ptr tmp;
// printf("%s\n",c);
tmp = create(c);
^^^^^^^^^
这是指向本地数组第一个元素的指针
char c[1024];
而且退出函数后不会存活
您需要使用函数 create 中传递的字符串的副本动态创建一个字符串。
我已经构建了一个三叉树结构以及一些函数来对其进行基本操作。其中之一是从文件中逐字读取并构建树! 问题可能出在函数 read_words 中。 当我手动插入数据时,数据工作正常,但是当我尝试从文件中插入时,它只会使树包含最后一个数据输入的内容。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
typedef struct a{
char * word ;
int occurs;
struct a * left;
struct a * same;
struct a * right; } Node;
typedef Node * Node_ptr ;
typedef Node * TriTree ;
void inorder(TriTree x) {
if(x==NULL) return;
inorder(x->left);
printf("%s(%d)--" , x->word, x->occurs);
inorder(x->same);
inorder(x->right);
return;}
void strlower(char * lower){
for (char *p = lower; *p; ++p) *p = tolower(*p);
// printf("%s\n",lower);
};
// 1
Node_ptr create(char * word){
Node_ptr tmp_ptr;
tmp_ptr = (Node_ptr)malloc(sizeof(Node));
tmp_ptr-> word = word;
tmp_ptr-> occurs = 1;
tmp_ptr-> left = NULL;
tmp_ptr-> same = NULL;
tmp_ptr-> right = NULL;
return tmp_ptr;
}
TriTree insert(TriTree x, Node_ptr node_ptr){
if(x==NULL){
// printf("%s\n","Empty Tree!");
x = node_ptr;
return x;
}
int ret;
strlower(x->word);
strlower(node_ptr->word);
ret = strcmp(x->word,node_ptr->word);
if(ret < 0) {
// printf("str1 is less than str2");
x->right = insert(x->right,node_ptr);
} else if(ret > 0) {
// printf("str2 is less than str1");
x->left = insert(x->left,node_ptr);
} else {
// printf("str1 is equal to str2");
x->same = insert(x->same,node_ptr);
}
return x;
} ;
TriTree read_words (FILE *f,TriTree x) {
char c[1024];
while (fscanf(f, " %1023s", c) == 1) {
Node_ptr tmp;
// printf("%s\n",c);
tmp = create(c);
printf("%s\n",tmp->word);
x = insert(x,tmp);
//free(tmp);
}
fclose(f);
return x;
}
int main()
{
TriTree x;
x = NULL;
FILE * fp = fopen("input.txt", "r");
x = read_words(fp,x);
inorder(x);
return 0;
}
input:
hello bye ask life BYE Hello night HeLLO
desired output:
ask bye BYE hello Hello HeLLo life night
my output:
hello hello hello hello hello hello hello hello
在函数中创建
Node_ptr create(char * word){
Node_ptr tmp_ptr;
tmp_ptr = (Node_ptr)malloc(sizeof(Node));
tmp_ptr-> word = word;
//...
您正在将用作参数的指针字分配给数据成员tmp_ptr->字。
但是您在每次从函数调用 create 时都传递了相同的指针 read_words
TriTree read_words (FILE *f,TriTree x) {
char c[1024];
while (fscanf(f, " %1023s", c) == 1) {
Node_ptr tmp;
// printf("%s\n",c);
tmp = create(c);
^^^^^^^^^
这是指向本地数组第一个元素的指针
char c[1024];
而且退出函数后不会存活
您需要使用函数 create 中传递的字符串的副本动态创建一个字符串。