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

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

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

Вот, собственно, код:
Листинг программы
  1. #ifndef DICTIONARY_H_INCLUDED
  2. #define DICTIONARY_H_INCLUDED
  3. #include <string.h>
  4. typedef struct dictionary_t {
  5. struct dictionary_t* m_pLeft;
  6. struct dictionary_t* m_pRight;
  7. char m_key[10];
  8. char m_value[10];
  9. } dictionary_t;
  10. dictionary_t* dictionary_ctor(dictionary_t* left, dictionary_t* right, char* key, char* value)
  11. {
  12. dictionary_t* dic = (dictionary_t*)malloc(sizeof(dictionary_t));
  13. dic->m_pRight = left;
  14. dic->m_pLeft = right;
  15. //dic->m_key = key;
  16. //dic->m_value = value;
  17. strncpy(dic->m_key, key, 10);
  18. strncpy(dic->m_value, value, 10);
  19. return dic;
  20. }
  21. void dictionary_insert(dictionary_t* dic, char* key, char* value)
  22. {
  23. if (dic == NULL) {
  24. dic = dictionary_ctor(NULL, NULL, key, value);
  25. printf("Added key:%s value:%s\n", key, value);
  26. return;
  27. }
  28. if (strcmp(key, dic->m_key) > 0) {
  29. dictionary_insert(dic->m_pRight, key, value);
  30. return;
  31. }
  32. if (strcmp(key, dic->m_key) < 0) {
  33. dictionary_insert(dic->m_pLeft, key, value);
  34. return;
  35. }
  36. // if key == dic->m_key
  37. //*dic->m_value = *value;
  38. strncpy(dic->m_value, value, 10);
  39. }
  40. char* dictionary_find(dictionary_t* dic, char* key)
  41. {
  42. char* answer = NULL;
  43. if (dic == NULL) {
  44. return NULL;
  45. }
  46. if (*key == *dic->m_key) {
  47. return dic->m_value;
  48. }
  49. if (strcmp(key, dic->m_key) > 0) {
  50. answer = dictionary_find(dic->m_pRight, key);
  51. }
  52. if (strcmp(key, dic->m_key) < 0) {
  53. answer = dictionary_find(dic->m_pLeft, key);
  54. }
  55. return answer;
  56. }
  57. #endif // DICTIONATY_H_INCLUDED
При использовании возникает ошибка сегментации. В чем может быть дело?

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

textual
Листинг программы
  1. #ifndef DICTIONARY_H_INCLUDED
  2. #define DICTIONARY_H_INCLUDED
  3.  
  4. #include <string.h>
  5.  
  6. typedef struct dictionary_t {
  7.  
  8.     struct dictionary_t* m_pLeft;
  9.     struct dictionary_t* m_pRight;
  10.  
  11.     char* m_key;
  12.     char* m_value;
  13.  
  14. } dictionary_t;
  15.  
  16. dictionary_t* dictionary_ctor(dictionary_t* left, dictionary_t* right, char* key, char* value)
  17. {
  18.     dictionary_t* dic = (dictionary_t*)malloc(sizeof(dictionary_t));
  19.  
  20.     dic->m_pRight = left;
  21.     dic->m_pLeft = right;
  22.     dic->m_key = key;
  23.     dic->m_value = value;
  24.  
  25.     return dic;
  26. }
  27.  
  28. void dictionary_insert(dictionary_t** dic, char* key, char* value)
  29. {
  30.     if (*dic == NULL) {
  31.         *dic = dictionary_ctor(NULL, NULL, key, value);
  32.         return;
  33.     }
  34.  
  35.     if (strcmp(key, (*dic)->m_key) > 0) {
  36.         dictionary_insert(&(*dic)->m_pRight, key, value);
  37.         return;
  38.     }
  39.  
  40.     if (strcmp(key, (*dic)->m_key) < 0) {
  41.         dictionary_insert(&(*dic)->m_pLeft, key, value);
  42.         return;
  43.     }
  44.  
  45.     (*dic)->m_value = value;
  46. }
  47.  
  48. char* dictionary_find(dictionary_t* dic, char* key)
  49. {
  50.     char* answer = NULL;
  51.  
  52.     if (dic == NULL) {
  53.         return NULL;
  54.     }
  55.  
  56.     if (*key == *dic->m_key) {
  57.         return dic->m_value;
  58.     }
  59.  
  60.     if (strcmp(key, dic->m_key) > 0) {
  61.         answer = dictionary_find(dic->m_pRight, key);
  62.     }
  63.  
  64.     if (strcmp(key, dic->m_key) < 0) {
  65.         answer = dictionary_find(dic->m_pLeft, key);
  66.     }
  67.  
  68.     return answer;
  69. }
  70.  
  71. #endif // DICTIONATY_H_INCLUDED

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

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

15   голосов , оценка 4 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы