Я работаю над своим двоичным деревом, написанным на c, но когда я начал искать внутри него, у меня начались проблемы, я не смог ничего найти в нем, сначала я думал, что я просто поиск по-другому, но когда я печатал обе стороны корня, я узнал, что оба они были NULL, даже когда память была выделена для этого узла.Поиск двоичного дерева
Я хотел бы знать, где память, которую я выделил, если она не находится на дереве и почему она выделяется в другой части памяти, прямо сейчас меня интересует только то, что дерево работает, но если у вас есть полезный совет, это тоже будет здорово.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "tree.h"
int main()
{
Tree* root = initTree();
insert(root, "John", 10);
insert(root, "Tom", 11);
printf("%d\n", root->value);
printf("%p %p\n", root->left, root->right);
if(root->left != NULL)
printf("Left : %d\n", root->left->value);
else if(root->right != NULL)
printf("Right : %d\n", root->right->value);
else
printf("Both sides are NULL\n");
destroyTree(root);
return 0;
}
Tree* initTree()
{
Tree* root;
root = malloc(sizeof(Tree));
strcpy(root->key, "");
root->left = NULL;
root->right = NULL;
return root;
}
void insert(Tree* root, char* key, int value)
{
if(root == NULL){
root = malloc(sizeof(Tree));
root->left = NULL;
root->right = NULL;
strcpy(root->key, key);
root->value = value;
printf("Node added, (%s), %p\n",root->key, root);
return;
}
if(!strcmp(root->key, "")){
strcpy(root->key, key);
root->value = value;
printf("Added key %s\n", root->key);
return;
}
if(strcmp(root->key, key) > 0){
insert(root->left, key, value);
return;
}
else if(strcmp(root->key, key) < 0){
insert(root->right, key, value);
return;
}
printf("I dont know where to put this\n");
}
int find(Tree* root, char* key)
{
if(root == NULL) return -1;
if(!strcmp(root->key, key)){
return root->value;
}
if(strcmp(root->key, key) > 0){
return find(root->left, key);
}
else if(strcmp(root->key, key) < 0){
return find(root->right, key);
}
if(root->left == NULL && root->right == NULL)
return 0;
return 0;
}
void destroyTree(Tree* root)
{
/*If we dont have a valid pointer to destroy then we return*/
if(root == NULL) return;
/*If any of the sides from the Tree have data storaged in
*we proceed to destoy it*/
if(root->left != NULL || root->right != NULL){
/*We give priority to the right site always in order
*to destroy everithing organized*/
if(root->right != NULL)
destroyTree(root->right);
if(root->left != NULL)
destroyTree(root->left);
}
/*If we reach the end of the tree, we free the block of memory
*stored there*/
else if(root->left == NULL && root->right == NULL){
free(root);
return;
}
}
Это tree.h
#ifndef TREE_H
#define TREE_H
typedef struct Tree Tree;
struct Tree
{
char key[16];
int value;
Tree* left;
Tree* right;
};
Tree* initTree();
void insert(Tree* root , char* key, int value);
int find(Tree* root, char* key);
void destroyTree(Tree* root);
#endif
'корень = таНос (SizeOf (Tree));' только записывает значение параметра, но не за пределами '' функции вставки(). Вам нужна подпись, например 'void insert (Tree ** root, char * key, int value);' чтобы ваши выделения памяти были видны снаружи. –