简体   繁体   中英

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. 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. 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. 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. 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.

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.

Change your Node definition to:

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

To make nodes an array so you don't have to allocate space for it.

You also have typos in 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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