二叉搜索树根节点保持为 NULL
Binary Search Tree root node stays NULL
出于某种原因,我的根节点始终保持为 NULL,并且没有向其中添加任何内容。我不确定为什么,想知道是否有人可以指导我做错什么。我正在尝试读取一个文件并根据我得到的数字对每个单词执行不同的操作。如果它是 1,我试图将这个词插入二叉树,除非它是重复的,在这种情况下,我试图让频率增加 1。如果它是 2,我希望能够打印出单词出现的深度和次数。最后我想根据最频繁到最少显示单词。我现在卡住的部分是根节点总是保持 NULL 所以我无法构建我的树,我也不知道问题到底是什么。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSIZE 50
typedef struct node
{
int depth;
int frequency;
struct node *left, *right;
char word[MAXSIZE];
} node;
node* insert(node* root, char newWord[MAXSIZE])
{
node *temp;
printf("insert Function is being called\n %s\n", newWord);
if(root == NULL)
{
temp = (node*) malloc(sizeof(node));
temp->frequency = 1;
temp->depth = 1;
temp->left = NULL;
temp->right = NULL;
strcpy(temp->word, newWord);
root = temp;
printf("You should only recieve this message once.\n");
}
else if(strcmp(newWord, root->word)<0)
{
printf("Word being added towards left\n");
insert(root->left, newWord);
root->depth +=1;
}
else if(strcmp(newWord, root->word)>0)
{
insert(root->left, newWord);
printf("Word being added towards right\n");
root->depth +=1;
}
else if(strcmp(newWord, root->word)==0)
{
printf("Word being duplicated");
root->frequency +=1;
}
return(root);
}
int inOrder(node* root)
{
if(root != NULL)
{
inOrder(root->left);
printf("BADAM %s\t", root->word);
printf("%d\n", root->frequency);
inOrder(root->right);
}
}
void main()
{
FILE *infile;
FILE *outfile;
char string[MAXSIZE];
int i, c, n, j, k;
node *root;
root = NULL;
infile = fopen("in.txt", "r");
fscanf(infile, "%d\n", &n);
for(i=0;i<n;i++)
{
printf("%d\n", i);
fscanf(infile, "%d %s\n", &c, string);
printf("the action is %d\n", c);
switch (c)
{
case 1:
printf("insert %s\n", string);
insert(root, string);
break;
case 2:
printf("print frequency of %s\n", string);
break;
default:
printf("error in input\n");
break;
inOrder(root);
}
}
}
您需要将函数insert返回的指针赋值给指针root
root = insert(root, string);
并在您需要编写的函数中
root->left = insert(root->left, newWord);
而不是在此代码段中使用 root->left
else if(strcmp(newWord, root->word)>0)
{
insert(root->left, newWord);
//...
你需要使用指针root->right
else if(strcmp(newWord, root->word)>0)
{
root->right = insert(root->right, newWord);
//...
出于某种原因,我的根节点始终保持为 NULL,并且没有向其中添加任何内容。我不确定为什么,想知道是否有人可以指导我做错什么。我正在尝试读取一个文件并根据我得到的数字对每个单词执行不同的操作。如果它是 1,我试图将这个词插入二叉树,除非它是重复的,在这种情况下,我试图让频率增加 1。如果它是 2,我希望能够打印出单词出现的深度和次数。最后我想根据最频繁到最少显示单词。我现在卡住的部分是根节点总是保持 NULL 所以我无法构建我的树,我也不知道问题到底是什么。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSIZE 50
typedef struct node
{
int depth;
int frequency;
struct node *left, *right;
char word[MAXSIZE];
} node;
node* insert(node* root, char newWord[MAXSIZE])
{
node *temp;
printf("insert Function is being called\n %s\n", newWord);
if(root == NULL)
{
temp = (node*) malloc(sizeof(node));
temp->frequency = 1;
temp->depth = 1;
temp->left = NULL;
temp->right = NULL;
strcpy(temp->word, newWord);
root = temp;
printf("You should only recieve this message once.\n");
}
else if(strcmp(newWord, root->word)<0)
{
printf("Word being added towards left\n");
insert(root->left, newWord);
root->depth +=1;
}
else if(strcmp(newWord, root->word)>0)
{
insert(root->left, newWord);
printf("Word being added towards right\n");
root->depth +=1;
}
else if(strcmp(newWord, root->word)==0)
{
printf("Word being duplicated");
root->frequency +=1;
}
return(root);
}
int inOrder(node* root)
{
if(root != NULL)
{
inOrder(root->left);
printf("BADAM %s\t", root->word);
printf("%d\n", root->frequency);
inOrder(root->right);
}
}
void main()
{
FILE *infile;
FILE *outfile;
char string[MAXSIZE];
int i, c, n, j, k;
node *root;
root = NULL;
infile = fopen("in.txt", "r");
fscanf(infile, "%d\n", &n);
for(i=0;i<n;i++)
{
printf("%d\n", i);
fscanf(infile, "%d %s\n", &c, string);
printf("the action is %d\n", c);
switch (c)
{
case 1:
printf("insert %s\n", string);
insert(root, string);
break;
case 2:
printf("print frequency of %s\n", string);
break;
default:
printf("error in input\n");
break;
inOrder(root);
}
}
}
您需要将函数insert返回的指针赋值给指针root
root = insert(root, string);
并在您需要编写的函数中
root->left = insert(root->left, newWord);
而不是在此代码段中使用 root->left
else if(strcmp(newWord, root->word)>0)
{
insert(root->left, newWord);
//...
你需要使用指针root->right
else if(strcmp(newWord, root->word)>0)
{
root->right = insert(root->right, newWord);
//...