简体   繁体   English

二进制树中的根始终为NULL

[英]root in binary tree always NULL

The program should read a txt, store all the words alphabetically and print them in order, with the number of times the word appears on the txt. 程序应该读取一个txt,按字母顺序存储所有单词并按顺序打印它们,单词出现在txt上的次数。

The problem seems to be in the Insert method, because it never prints TEST, so it seems the pAux is always NULL for some reason. 问题似乎是在Insert方法中,因为它从不打印TEST,所以看起来pAux由于某种原因总是为NULL。 And because of that, the Print method returns in his first call. 因此,Print方法在第一次调用时返回。

What am I doing wrong? 我究竟做错了什么?

tree.h tree.h中

#ifndef TREE_H_
#define TREE_H_

typedef struct Item{
    char* key;
    int no;
} TItem;

typedef struct No{
    TItem item;
    struct No* pLeft;
    struct No* pRight;
} TNo;

void TTree_Insert (TNo**, char[]);
void TTree_Print (TNo*);

#endif

tree.c tree.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "tree.h"

TNo* TNo_Create (char* c){
    TNo* pNo = malloc(sizeof(TNo));
    pNo->item.key = malloc(sizeof(char)*strlen(c));
    strcpy(pNo->item.key, c);
    pNo->item.no = 1;
    pNo->pLeft = NULL;
    pNo->pRight = NULL;
    return pNo;
}

void TTree_Insert (TNo** pRoot, char word[80]){
    char* c = malloc(sizeof(char)*strlen(word));
    strcpy(c, word);
    TNo** pAux;
    pAux = pRoot;
    while (*pAux != NULL){
        if (strcmp(c, (*pAux)->item.key) < 0) pAux = &((*pAux)->pLeft);
        else if (strcmp(c, (*pAux)->item.key) > 0) pAux = &((*pAux)->pRight);
        else{
            (*pAux)->item.no++;
            return;
        }
    }
    *pAux = TNo_Create(c);
    return;
}

void TTree_Print (TNo *p){
    if (p == NULL) return;
    TTree_Print (p->pLeft);
    printf("%s - %d", p->item.key, p->item.no);
    TTree_Print (p->pRight);
}

main.c main.c中

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "tree.h"

int main(){
    TNo* pRoot = NULL;
    FILE* txt = fopen("Loremipsum.txt", "r");
    char aux[80];
    int c, x = 0;

    while ((c = fgetc(txt)) != EOF){
        while (!(isalpha((char)c))) c = fgetc(txt);
        while (isalpha((char)c)) {
            if (isupper((char)c)) c = c+32;
            if (islower((char)c)) aux[x++] = (char)c;
            c = fgetc(txt);
        }
        aux[x] = '\0';
        TTree_Insert(&pRoot, aux);
        x = 0;
        aux[0] = '\0';
    }
    TTree_Print(pRoot);
    fclose(txt);
    return 0;
}

I did not look through all your code. 我没有查看你的所有代码。 I will answer only your question. 我只回答你的问题。 You have to pass pRoot to TTree_Insert by reference. 您必须通过引用将pRoot传递给TTree_Insert。 Otherwise you pass its copy to the function and any changes of the copy within the function do not influence the original value. 否则,将其副本传递给函数,函数中副本的任何更改都不会影响原始值。

For example 例如

void TTree_Insert ( TNo **pRoot, char word[80] ){
    char* c = malloc(sizeof(char)*strlen(word) + 1 ); // <==
    strcpy( c, word ); // <==
    TNo* pAux;
    pAux = *pRoot;        
    //...

And in main you have to call the function like 在主要你必须调用函数

TTree_Insert( &pRoot, aux );

Take into account that you have to adjust all other code of the function. 考虑到您必须调整该函数的所有其他代码。 For example 例如

void TTree_Insert( TNo **pRoot, const char word[80] )
{
    char* c = malloc( sizeof( char ) * strlen( word ) + 1 );

    strcpy( c, word );

    TNo **pAux = pRoot;

    while ( *pAux != NULL )
    {
        printf("TESTE");
        if ( strcmp(c, ( *pAux )->item.key ) < 0 )
        {
            pAux = &pAux->pLeft;
        }
        else if ( strcmp(c,  ( *pAux )->item.key ) > 0 )
        {
             pAux = &pAux->pRight;
        }
        else
        { 
            ( *pAux )->item.no++;
            break;
        }
    }

    if ( *pAux == NULL ) *pAux = TNo_Create(c);

    return;
}

I hope it will work.:) 我希望它能奏效。:)

pRoot is initially NULL , and you never change it later. pRoot最初为NULL ,您以后永远不会更改它。

so it seems the pAux is always NULL for some reason 所以看起来pAux由于某种原因总是为NULL

Well, that's the reason... why don't you use a debugger or do some printing? 那么,这就是为什么你不使用调试器或进行打印?

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

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