繁体   English   中英

C 中的二叉搜索树实现将根作为参考

[英]Binary Search Tree implementation in C is passing root as reference

我正在尝试从字符串列表创建二叉搜索树,并通过比较每个字符串的前两个字符来比较值之间的比较。 问题是当我遍历文件时,根节点不断获取后续每一行的值。 我在 GDB 中进行了一些挖掘,发现我最初的“插入”function 调用似乎将“line”或“value”参数作为参考传递,因此我将“root”设置为“line”变量。

我正在努力弄清楚如何更正我的程序结构以通过值而不是通过引用传递“行”,以及如何正确创建这个二叉树。 我知道出了什么问题,但我不知道如何解决

Output:

Input: adam 
Create node with value adam 
Root after node creation adam 
Root after insert adam

Input: bobby 
Root before insert bobby <-- This should still be "adam"
Root after compare bobby 
Compare: bo and bo 
Same 
Root after insert bobby

Input: christopher 
Root before insert christopher
Root after compare christopher 
Compare: ch and ch 
Same 
Root after insert christopher
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h>
#include "node.h"

struct node_t *root = NULL;

// Insert node into the binary search tree 
void insert(struct node_t** node, char* value)
{

    char valueParameter[32];
    strcpy( valueParameter, value );

    if(!(*node)) // Node doesn't exist
    {
        printf("Create node with value %s", valueParameter);
        (*node) = (struct node_t*)malloc(sizeof(struct node_t)); // Create a
        (*node)->data = valueParameter;
        (*node)->left = NULL;
        (*node)->right = NULL;
        
        printf("Root after node creation %s", (root)->data);
    }
    else
    {
        printf("Root after compare %s", (*node)->data);
    
        // Grab the first two characters
        char newVal[32];
        strncpy(newVal, value, 2);
        newVal[2] = '\0'; 
        
        // Grab the first two characters
        char rootVal[32];
        strncpy(rootVal, (*node)->data, 2);
        rootVal[2] = '\0'; 
        
        
        printf("Compare: %s and %s\n", newVal, rootVal);
    
        if(strcmp(newVal, rootVal) < 0)
        {
            printf("Less than\n");
            insert(&(*node)->left, value);
        }
        else if(strcmp(newVal, rootVal) > 0)
        {
            printf("Greater than\n");
            insert(&(*node)->right, value);
        }
        else
        {
            printf("Same\n");
        }
    }



};

struct node_t* buildTree( char * path )
{
    
    // Opens the file
    FILE *fptr = fopen("test1.sp2021", "r");
    
    if (fptr == NULL) 
    {
        printf("Invalid input file.\n");
        return 0;
    }

    char line[32];
    while (fgets(line, sizeof(line), fptr))
    {

        printf("Input: %s", line);
        
        if (root != NULL)
        {
            printf("Root before insert %s", (root)->data);
        }
        
        // Inserts the next item into the binary search tree, starts looking for the position at the root
        insert( &root, line );
        
        printf("Root after insert %s", (root)->data);
        printf("\n");
        
    }
    
    printf("Final: %s\n", root->data);


    return root;
}

创建一个带有char valueParameter[32]的新char数组,然后分配(*node)->data = valueParameter; . 因此, valueParameter是一个局部变量, root->data指向insert function 使用的堆栈,该堆栈在insert function 返回时被销毁。 另请参阅问题。

您必须使用malloc valueParameter 您应该在if语句中调用malloc ,因为当执行else块内的代码时您不需要valueParameter并且您会得到 memory 泄漏。

暂无
暂无

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

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