简体   繁体   English

二叉搜索树根节点保持NULL

[英]Binary Search Tree root node stays NULL

for some reason my root node consistently stays NULL and nothing gets added to it.出于某种原因,我的根节点始终保持 NULL 并且没有添加任何内容。 I am unsure as to why, and was wondering if someone could guide me as to what I am doing wrong.我不确定为什么,并且想知道是否有人可以指导我做错了什么。 I am trying to read a file and perform different actions for every word based on the number I get.我正在尝试读取一个文件并根据我得到的数字对每个单词执行不同的操作。 If it is a 1, I am trying to insert the word into the binary tree unless it is a duplicate in which case, I am trying to get the frequency to go up by 1. If it is a 2, I want to be able to print out the depth and number of times the word appears.如果它是 1,我试图将这个词插入二叉树,除非它是重复的,在这种情况下,我试图让频率上升 1。如果它是 2,我希望能够打印出单词出现的深度和次数。 In the end I want to display the words based on most frequent to least.最后,我想根据最频繁到最少的顺序显示单词。 The part I am stuck at now is that the root node always stays NULL so I am unable to build my tree, I also am unaware as to what the problem really is.我现在陷入困境的部分是根节点始终保持为 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);
        }
    }
}

You need to assign the returned pointer from the function insert to the pointer root您需要将函数插入返回的指针分配给指针根

root = insert(root, string);

and within the function you need to write在你需要写的函数中

root->left = insert(root->left, newWord);

and instead of using root->left in this code snippet而不是在此代码片段中使用 root->left

else if(strcmp(newWord, root->word)>0)
{
    insert(root->left, newWord);
    //...

you need to use the pointer root->right你需要使用指针 root->right

else if(strcmp(newWord, root->word)>0)
{
    root->right = insert(root->right, newWord);
    //...  

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

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