Conversation
HW9/AVLTree/AVLTree.h
Outdated
|
|
||
| #include <stdbool.h> | ||
|
|
||
| typedef struct Node Node; |
There was a problem hiding this comment.
Я как пользователь не хочу никаких узлов, я хочу словарь, отображающий строку в строку. Стоило бы хотя бы затайпдефить Node на Dictionary и использовать Dictionary везде в функциях интерфейса модуля, чтобы пользователя не пугать
HW9/AVLTree/AVLTree.c
Outdated
| } | ||
|
|
||
| int getHeight(Node* node) { | ||
| return node ? node->height : -111; |
|
|
||
| newNode->value = malloc(strlen(value) + 1); | ||
| if (newNode->value == NULL) { | ||
| return NULL; |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
|
|
||
| newNode->key = malloc(strlen(key) + 1); | ||
| if (newNode->key == NULL) { | ||
| return NULL; |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
HW9/AVLTree/AVLTree.c
Outdated
| return createNode(value, key); | ||
| } | ||
|
|
||
| int num = atoi(key); |
There was a problem hiding this comment.
Никто не обещал, что ключ конвертируется в число
HW9/AVLTree/AVLTree.c
Outdated
| if (node == NULL) { | ||
| return NULL; | ||
| } | ||
| if (strcmp(node->key, key) == 0) { |
There was a problem hiding this comment.
Вот тут же сравнение правильное, почему-то в add другое (кстати, если add использует один порядок, а search — другой, двоичное дерево поиска вообще работать не будет).
HW9/AVLTree/AVLTree.c
Outdated
| free((char*)root->key); | ||
| free((char*)root->value); | ||
| free(root); |
There was a problem hiding this comment.
Сделали бы отдельную функцию удаления узла
HW9/AVLTree/AVLTree.c
Outdated
| strcpy((char*)root->key, temp->key); | ||
| strcpy((char*)root->value, temp->value); |
There was a problem hiding this comment.
Тут тоже не факт, что это возможно
HW9/AVLTree/AVLTree.h
Outdated
| // small left rotate | ||
| Node* rotateLeft(Node* node); | ||
|
|
||
| // small right rotate | ||
| Node* rotateRight(Node* node); | ||
|
|
||
| // big left rotate | ||
| Node* bigLeftRotate(Node* node); | ||
|
|
||
| // bit right rotate | ||
| Node* bigRightRotate(Node* node); |
There was a problem hiding this comment.
Надо удалить из интерфейса модуля функции, которые к нему не относятся
HW9/AVLTree/main.c
Outdated
| printf("\n\n"); | ||
| switch (choice) | ||
| { | ||
| case(1): |
There was a problem hiding this comment.
| case(1): | |
| case 1: |
HW9/AVLTree/AVLTree.c
Outdated
|
|
||
| newNode->value = strdup(value); | ||
| if (newNode->value == NULL) { | ||
| free((char*)newNode->key); |
There was a problem hiding this comment.
А это плохая идея, он ещё не инициализирован и не забит нулями (потому что malloc, а не calloc), упадёт сразу
HW9/AVLTree/AVLTree.c
Outdated
| free((char*)node->value); | ||
| node->value = strdup(value); | ||
| if (node->value == NULL) { |
There was a problem hiding this comment.
Так мы старый value уже удалим, а новый не создадим. Лучше
char* temp = strdup(value);
if (temp == NULL) {
return NULL;
}
free((char*)node->value);
node->value = strdup(value);Но это не сильно поможет, потому что факт возврата NULL в строках 97 и 100 не проверяется, так что там всё равно дерево пострадает и это даже не сообщат вызывающему.
| root->value = strdup(tmp->value); | ||
| root->left = tmp->left; | ||
| root->right = tmp->right; | ||
| free(tmp); |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
HW9/AVLTree/AVLTree.h
Outdated
| int getHeight(Dictionary* node); | ||
|
|
||
| // function to update node height | ||
| void updateHeight(Dictionary* node); No newline at end of file |
There was a problem hiding this comment.
Думаю, что это пользователю не нужно
HW9/AVLTree/main.c
Outdated
| fgets(key, 100, stdin); | ||
| key[strcspn(key, "\n")] = '\0'; |
There was a problem hiding this comment.
Сделали бы функцию чтения строки. Всего две строчки но десяток раз в программе
HW9/AVLTree/AVLTree.h
Outdated
| typedef struct Dictionary Dictionary; | ||
|
|
||
| // returns the balance of the tree | ||
| int getBalance(Dictionary* node); |
There was a problem hiding this comment.
Это тоже пользователю не нужно, но могло бы быть полезно в тестах, чтобы доказать, что дерево реально АВЛ. Если бы был такой тест, и если бы эта функция проверяла бы баланс по всему дереву, а не только в корне (ну, точнее, это должна быть другая функция, специально для тестов, а getBalance можно убрать в .c)
feat: isAVL function and readString function test: add isAVLTest
yurii-litvinov
left a comment
There was a problem hiding this comment.
Ну почти. Но тут фиксы тривиальны, так что думаю, что можно зачесть
| return NULL; | ||
| } | ||
| free((char*)node->value); | ||
| node->value = strdup(value); |
There was a problem hiding this comment.
| node->value = strdup(value); | |
| node->value = tmp; |
Провал :) Не воспринимайте мои подсказки столь буквально
| if (tmp < 0) { | ||
| node->left = add(node->left, key, value); | ||
| if (node->left == NULL) { | ||
| return NULL; |
There was a problem hiding this comment.
А оно так последовательно забудет указатели на всё левое поддерево, выходя из рекурсии. Надо было примерно как с strdup, запомнить во временную переменную, проверить, и если получилось плохо, дерево не менять.
| #include <string.h> | ||
| #include <stdbool.h> | ||
|
|
||
| bool isAVLTest() { |
There was a problem hiding this comment.
| bool isAVLTest() { | |
| bool isAVLTest(void) { |
No description provided.