简体   繁体   English

创建二叉搜索树结构并在 C 中正确初始化

[英]creating a binary search tree struct and initializing it properly in C

So i'm trying to create a binary search tree in C but im a little bit confused and am running into issues.所以我试图在 C 中创建一个二叉搜索树,但我有点困惑并且遇到了问题。 This is what my structures look like:这是我的结构的样子:

typedef struct {
    void* item;
    struct Node** nodes;
}Node;

typedef struct{
    Node* tree_root; 
    int depth; 
    int item_size; 
} BST;
 

So in this case instead of having a left and right node initialized for the tree, I just have a '2D' array Nodes** nodes where nodes[0] is my left node on the tree and nodes[1] is my right node on the tree.所以在这种情况下,我没有为树初始化左右节点,而是有一个“2D”数组Nodes** nodes ,其中nodes[0]是我在树上的左节点, nodes[1]是我的右节点在树上。 I think I'm not properly initializing it because everytime I try to insert an item into the tree I get a sgmentation fault(core dumped) as an error.我认为我没有正确初始化它,因为每次我尝试将一个项目插入树中时,我都会收到一个sgmentation fault(core dumped)作为错误。 Heres how I create a node and initialize the tree: // Create node containing item, return reference of it.下面是我创建节点和初始化树的方法: // 创建包含项目的节点,返回它的引用。

Node* createNode(void* item){
        Node *new_node;

    if ((new_node = malloc(sizeof(Node))) == NULL)
        return NULL;

    new_node->item = item;
    new_node->nodes[0] = NULL;
    new_node->nodes[1] = NULL;

    return new_node;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Initializes the fields on the BST. Returns nothing.
void initBinaryTree(BST* tree, int item_size){
Node* root = NULL;
root = (Node *)malloc(sizeof(Node));
Node* left = NULL;
root = (Node *)malloc(sizeof(Node));
Node* right = NULL;
root = (Node *)malloc(sizeof(Node));

tree->tree_root = root;
tree->tree_root->nodes[0] = left;
tree->tree_root->nodes[1] = right;
tree->item_size = item_size;
tree->depth = 0;

}

So im just trying to create the memory space for the nodes so that I can put stuff in there, im assuming the only reason I cant insert anything is because, i think, somewhere here I didnt do something right.所以我只是想为节点创建 memory 空间,这样我就可以把东西放在那里,我假设我不能插入任何东西的唯一原因是因为,我认为,这里的某个地方我没有做正确的事情。 So I was wondering if you guys could give me any advice on what I am or could potentially be doing wrong here.所以我想知道你们是否可以就我在这里做错或可能做错的事情给我任何建议。 Any help is appreciated!任何帮助表示赞赏!

You allocate a Node , but you never allocate space for the nodes member to point to.您分配了一个Node ,但您从不为nodes成员分配指向的空间。

For example, when you access new_node->nodes[0] you're dereferenceing the nodes member to access a pointer in an array, but there is no array because nodes doesn't point to anything.例如,当您访问new_node->nodes[0]时,您正在取消引用nodes成员以访问数组中的指针,但没有数组,因为nodes不指向任何东西。

Change your Node definition to:将您的Node定义更改为:

typedef struct {
    void* item;
    struct Node *nodes[2];
}Node;

To make nodes an array so you don't have to allocate space for it.使nodes成为一个数组,这样您就不必为它分配空间。

You also have typos in initBinaryTree :您在initBinaryTree中也有拼写错误:

Node* root = NULL;
root = (Node *)malloc(sizeof(Node));
Node* left = NULL;
root = (Node *)malloc(sizeof(Node));
Node* right = NULL;
root = (Node *)malloc(sizeof(Node));

This should be:这应该是:

Node* root = NULL;
root = (Node *)malloc(sizeof(Node));
Node* left = NULL;
left = (Node *)malloc(sizeof(Node));
Node* right = NULL;
right = (Node *)malloc(sizeof(Node));

But... you shouldn't create blank nodes when you initialize the tree.但是......初始化树时不应该创建空白节点。 The root member should start as NULL , then you add nodes as you go. root成员应该以 NULL 开头,然后像NULL一样添加节点。

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

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