Реализация двоичного дерева поиска - C (СИ)

Узнай цену своей работы

Формулировка задачи:

Вот, собственно, код:
#ifndef DICTIONARY_H_INCLUDED
#define DICTIONARY_H_INCLUDED
 
#include <string.h>
 
typedef struct dictionary_t {
 
    struct dictionary_t* m_pLeft;
    struct dictionary_t* m_pRight;
 
    char m_key[10];
    char m_value[10];
 
} dictionary_t;
 
dictionary_t* dictionary_ctor(dictionary_t* left, dictionary_t* right, char* key, char* value)
{
    dictionary_t* dic = (dictionary_t*)malloc(sizeof(dictionary_t));
 
    dic->m_pRight = left;
    dic->m_pLeft = right;
    //dic->m_key = key;
    //dic->m_value = value;
    strncpy(dic->m_key, key, 10);
    strncpy(dic->m_value, value, 10);
 
    return dic;
}
 
void dictionary_insert(dictionary_t* dic, char* key, char* value)
{
    if (dic == NULL) {
        dic = dictionary_ctor(NULL, NULL, key, value);
        printf("Added key:%s value:%s\n", key, value);
        return;
    }
 
    if (strcmp(key, dic->m_key) > 0) {
        dictionary_insert(dic->m_pRight, key, value);
        return;
    }
 
    if (strcmp(key, dic->m_key) < 0) {
        dictionary_insert(dic->m_pLeft, key, value);
        return;
    }
 
    // if key == dic->m_key
    //*dic->m_value = *value;
    strncpy(dic->m_value, value, 10);
}
 
char* dictionary_find(dictionary_t* dic, char* key)
{
    char* answer = NULL;
 
    if (dic == NULL) {
        return NULL;
    }
 
    if (*key == *dic->m_key) {
        return dic->m_value;
    }
 
    if (strcmp(key, dic->m_key) > 0) {
        answer = dictionary_find(dic->m_pRight, key);
    }
 
    if (strcmp(key, dic->m_key) < 0) {
        answer = dictionary_find(dic->m_pLeft, key);
    }
 
    return answer;
}
 
#endif // DICTIONATY_H_INCLUDED
При использовании возникает ошибка сегментации. В чем может быть дело?

Решение задачи: «Реализация двоичного дерева поиска»

textual
Листинг программы
#ifndef DICTIONARY_H_INCLUDED
#define DICTIONARY_H_INCLUDED
 
#include <string.h>
 
typedef struct dictionary_t {
 
    struct dictionary_t* m_pLeft;
    struct dictionary_t* m_pRight;
 
    char* m_key;
    char* m_value;
 
} dictionary_t;
 
dictionary_t* dictionary_ctor(dictionary_t* left, dictionary_t* right, char* key, char* value)
{
    dictionary_t* dic = (dictionary_t*)malloc(sizeof(dictionary_t));
 
    dic->m_pRight = left;
    dic->m_pLeft = right;
    dic->m_key = key;
    dic->m_value = value;
 
    return dic;
}
 
void dictionary_insert(dictionary_t** dic, char* key, char* value)
{
    if (*dic == NULL) {
        *dic = dictionary_ctor(NULL, NULL, key, value);
        return;
    }
 
    if (strcmp(key, (*dic)->m_key) > 0) {
        dictionary_insert(&(*dic)->m_pRight, key, value);
        return;
    }
 
    if (strcmp(key, (*dic)->m_key) < 0) {
        dictionary_insert(&(*dic)->m_pLeft, key, value);
        return;
    }
 
    (*dic)->m_value = value;
}
 
char* dictionary_find(dictionary_t* dic, char* key)
{
    char* answer = NULL;
 
    if (dic == NULL) {
        return NULL;
    }
 
    if (*key == *dic->m_key) {
        return dic->m_value;
    }
 
    if (strcmp(key, dic->m_key) > 0) {
        answer = dictionary_find(dic->m_pRight, key);
    }
 
    if (strcmp(key, dic->m_key) < 0) {
        answer = dictionary_find(dic->m_pLeft, key);
    }
 
    return answer;
}
 
#endif // DICTIONATY_H_INCLUDED

Оцени полезность:

15   голосов , оценка 4 из 5
Похожие ответы