Я пытаюсь реализовать bst и все функции, необходимые для его работы (вставка, удаление, перемещение, удаление_три).
Кажется, все работает нормально.Но Valgrind жалуется на неинициализированные значения, но как и что мне нужно инициализировать в моем примере.Я прочитал несколько других тем, в которых говорилось об этой проблеме, но у меня возникли проблемы с применением ее к моему примеру.
Каждая помощь очень ценится.Спасибо
Вот мой код:
node* insert(node* root, int key)
{
if(root == NULL){
root = malloc(sizeof(node));
if(root != NULL){
root->key_ = key;
}
}
else{
if(key == root->key){
return NULL;
}
else if(key < root->key){
if(root->left_ != NULL ){
insert(root->left_, key);
}
else{
root->left_ = NULL;
node* newNode = malloc(sizeof(node));
if(newNode != NULL){
newNode ->key_ = key;
root->left_ = newNode;
}
}
}
else{
if(root->right_ != NULL){
insert(root->right_, key);
}
else{
root->right_ = NULL;
node* newNode = malloc(sizeof(node));
if(newNode != NULL){
newNode ->key_ = key;
root->right_ = newNode;
}
}
}
}
return root;
}
node* search(node* root, int key)
{
if(key == root->key){
return root;
}
else{
if(key < root->key{
if(root->left_ != NULL){
search(root->left_,key);
}
else{
return NODE_NOT_FOUND;
}
}
else{
if(root->right_ != NULL){
search(root->right_,key);
}
else{
root->right_ = NULL;
return NODE_NOT_FOUND;
}
}
}
return root;
}
node* findMin(node* root){
while(root->left_ != NULL){
root = root->left_;
}
return root;
}
node* delete(node* root, int key)
{
if(key < root->key){
root->left_ = delete(root->left_,key);
}
else if (key > root->key){
root->right_ = delete(root->right_,key);
}
else{
if(root->left_ == NULL && root->right_ == NULL){
free(root);
root = NULL;
}
else if(root->left_ == NULL){
node* temp = root;
root = root->right_;
free(temp);
}
else if(root->right_ == NULL){
node* temp = root;
root = root->left_;
free(temp);
}
else{
node* temp = findMin(root->right_);
root->key_= temp->key_;
root->right_= delete(root->right_,temp->key_);
}
}
return root;
}
void delete_tree(node* root)
{
if(root != NULL){
delete_tree(root->left_);
delete_tree(root->right_);
root->left_ = NULL;
root->right_ = NULL;
free(root);
}
}
Выход Valgrind:
==6289== Memcheck, a memory error detector
==6289== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==6289== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==6289== Command: ./bst.out
==6289==
==6289== Conditional jump or move depends on uninitialised value(s) [gone]
==6289== at 0x108816: insert (bst.c:64)
==6289== by 0x108B42: main (main.c:12)
==6289==
==6289== Conditional jump or move depends on uninitialised value(s) [gone]
==6289== at 0x1087B7: insert (bst.c:50)
==6289== by 0x108B57: main (main.c:13)
==6289==
==6289== Conditional jump or move depends on uninitialised value(s) [still here]
==6289== at 0x1089DA: delete (bst.c:139)
==6289== by 0x1089BE: delete (bst.c:136)
==6289== by 0x108C0B: main (main.c:17)
==6289==
==6289== Conditional jump or move depends on uninitialised value(s) [still here]
==6289== at 0x1089E7: delete (bst.c:139)
==6289== by 0x1089BE: delete (bst.c:136)
==6289== by 0x108C0B: main (main.c:17)
==6289==
==6289== Conditional jump or move depends on uninitialised value(s) [still here]
==6289== at 0x1089DA: delete (bst.c:139)
==6289== by 0x10897E: delete (bst.c:133)
==6289== by 0x108C20: main (main.c:18)
==6289==
==6289== Conditional jump or move depends on uninitialised value(s) [still here]
==6289== at 0x1089E7: delete (bst.c:139)
==6289== by 0x10897E: delete (bst.c:133)
==6289== by 0x108C20: main (main.c:18)**
==6289==
==6289==
==6289== HEAP SUMMARY:
==6289== in use at exit: 0 bytes in 0 blocks
==6289== total heap usage: 3 allocs, 3 frees, 72 bytes allocated
==6289==
==6289== All heap blocks were freed -- no leaks are possible
==6289==
==6289== For counts of detected and suppressed errors, rerun with: -v
==6289== Use --track-origins=yes to see where uninitialised values come from
==6289== ERROR SUMMARY: 6 errors from 6 contexts (suppressed: 0 from 0)