二进制树不从文件中添加字符串

时间:2013-11-17 18:16:42

标签: c file tree binary-tree

#include <stdio.h>
#include <stdlib.h>

struct treeNode
{
    char *word;
    int NumberCnt; 
    struct treeNode *rightPTR, *leftPTR; 

};
typedef struct treeNode node;

node *rootPTR = NULL;

void freeTree(node *currPTR)
{
    if (currPTR!= NULL)
   {
        freeTree(currPTR -> leftPTR);
        free(currPTR);
        freeTree(currPTR -> rightPTR);
   }
}

void printTree(node *currPTR)
{
    if (currPTR != NULL)
        {
            printTree(currPTR ->leftPTR);   
            printf("%s appeared:%d times\n", currPTR->word, currPTR->NumberCnt);
            printTree(currPTR ->rightPTR);  
        }
}



int insertNode (char* input)
{

    node *tempPTR = malloc(sizeof(node));
    tempPTR -> word = input;
    tempPTR -> NumberCnt=0;
    tempPTR -> leftPTR = NULL;
    tempPTR -> rightPTR = NULL;

    if (rootPTR == NULL)
    {   
        rootPTR = tempPTR;
        rootPTR -> NumberCnt++;
    }

    else 
    {

        node *currPTR = rootPTR;
        node *prevPTR = NULL;

            while (currPTR != NULL)
            {

             int comp = strcmp(input, (currPTR->word));  
                if (comp == 0)
                {
                    printf ("Entry already exists\n");
                    currPTR->NumberCnt++;
                    return 1;   
                }
                else if (comp < 0)
                {
                    prevPTR = currPTR;
                    currPTR = currPTR->leftPTR;
                }
                else if (comp > 0)
                {

                    prevPTR = currPTR;
                    currPTR = currPTR->rightPTR;
                }

            }

        int comp = strcmp(input, (prevPTR ->word));
        if (comp < 0)
        {
             prevPTR->leftPTR = tempPTR;
             prevPTR ->NumberCnt++;

        }

        else if (comp > 0)
        {
            prevPTR->rightPTR = tempPTR;
            prevPTR->NumberCnt++;

        }

         return 0;  
    }
     printf("root1%s\n",rootPTR->word);

    return 2;
 }

 int search(char* input) 
{
     if (input == rootPTR ->word)
    {
        printf("Node found %s\n", rootPTR->word);
        return 0;
    }
    else
    {
        if (input < rootPTR ->word)
            {

                node *currPTR = rootPTR->leftPTR;

                while (currPTR != NULL)
                {
                    if (input == currPTR->word)
                    {
                        printf("Node found %s\n", currPTR->word);
                        return 0;
                    }
                    else if (input < currPTR->word)
                    {
                    currPTR = (currPTR -> leftPTR); 
                    }
                    else if (input > currPTR->word)
                    {
                        currPTR = (currPTR -> rightPTR);
                    } 
                 }
                printf ("Node not in tree\n");
                return 1;
            }

            if (input > rootPTR ->word)
            {

                node *currPTR = rootPTR->rightPTR;

                while (currPTR != NULL)
                {

                    if (input == currPTR->word)
                    {
                        printf ("Node found %s\n", currPTR->word);
                        return 0;
                    }

                    else if (input < currPTR->word)
                    {
                        currPTR = (currPTR -> leftPTR); 
                    }

                    else if (input > currPTR->word)
                    {
                        currPTR = (currPTR ->rightPTR);
                    }
                }
                 printf ("Node not in tree\n");
                return 1;
            }

    } 

 return 2;
 }

 void fixWord(char* buff)
{
    char* unfixed = buff;
    char* fixed = buff;

     while (*unfixed)
     {

             if (isalpha(*unfixed))
        {
            *fixed=tolower(*unfixed);
            *fixed++;

        }   
            *unfixed++;


    }
    *fixed=0;

}


int main()
{   
FILE *ptr_file;
char buff [100];
//ptr_file = fopen ("sherlock.txt", "r");
ptr_file = fopen ("input.txt", "r");
if (!ptr_file)
    printf("File read error");


    while(fscanf(ptr_file, "%s ", buff ) != EOF)
    {
        int comparison = strcmp(buff, "endoffile");
        if (comparison == 0)
        {
            return 0;
        }

         fixWord(buff);
         insertNode(buff);

    }


fclose(ptr_file);
printf("root:%s\n", rootPTR->word);


return 0;
 }

好的我有这个二进制树,它从文件中获取字符串输入。如果我将字符串直接传递给树,它就可以工作,但是当我尝试传递它时,我读取的字符串形式是它继续替换根节点的文件,并且不会将它们正确地添加到树中。

2 个答案:

答案 0 :(得分:1)

您将buff传递给插入函数,并将其存储在节点中。因此,所有节点最终都会指向同一地址,即buff中的主要地址。

您需要为每个节点中的每个字符串分配存储空间,并将输入复制到该节点中。并且记得在删除树时正确解除分配。

如果您的图书馆拥有它,

strdup可以很方便。

答案 1 :(得分:1)

buff是当前行值并在每行读取时覆盖:

insertNode(buff);

insertNode指定覆盖缓冲区。

int insertNode (char* input)
{
    node *tempPTR = malloc(sizeof(node));
    tempPTR -> word = input;
    ....

因此,您应该将输入值的动态分配为:

int insertNode (char* input)
{
    node *tempPTR = malloc(sizeof(node));
    tempPTR -> word = strdup(input);
    ....