Skip to content

Added 2-nd homework - #3

Open
rmnsmnsk wants to merge 8 commits into
mainfrom
hw_2
Open

Added 2-nd homework#3
rmnsmnsk wants to merge 8 commits into
mainfrom
hw_2

Conversation

@rmnsmnsk

Copy link
Copy Markdown
Owner

No description provided.

Comment thread src/hw_2/avl-test.c
Comment on lines +13 to +14
assert(tree->root == NULL);
assert(tree->size == 0);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Тесты не должны лезть во внутреннее представление данных — это нарушает принцип сокрытия деталей реализации, и приводит к необходимости переписывать тесты при каждом изменении внутренней структуры данных. Тестировать надо только внешнее наблюдаемое поведение.

Comment thread src/hw_2/avl-test.c
Comment on lines +6 to +7
int testsPassed = 0;
int testsFailed = 0;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Глобальные переменные зло, они сильно запутывают поток данных. тут лучше их явно в тесты передавать.

Comment thread src/hw_2/avl-test.c
Comment on lines +185 to +188
if (testsFailed > 0) {
return 1;
}
return 0;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (testsFailed > 0) {
return 1;
}
return 0;
return testsFailed > 0;

Comment thread src/hw_2/avl-tree.c
Comment on lines +9 to +15
Tree* tree = malloc(sizeof(Tree));
if (tree == NULL) {
return NULL;
}
tree->root = NULL;
tree->size = 0;
return tree;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Tree* tree = malloc(sizeof(Tree));
if (tree == NULL) {
return NULL;
}
tree->root = NULL;
tree->size = 0;
return tree;
return calloc(1, sizeof(Tree));

Comment thread src/hw_2/avl-tree.c
Comment on lines +51 to +59
if (getBalance(node) > 1 && getBalance(node->left) >= 0) {
node = smallRotateRight(node);
} else if (getBalance(node) < -1 && getBalance(node->right) <= 0) {
node = smallRotateLeft(node);
} else if (getBalance(node) > 1 && getBalance(node->left) < 0) {
node = bigRotateRight(node);
} else if (getBalance(node) < -1 && getBalance(node->right) > 0) {
node = bigRotateLeft(node);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Это лучше вынести в отдельную функцию, потому что по смыслу это определение типа вращения, ну и в удалении узла это тоже нужно делать.

Comment thread src/hw_2/avl-tree.h
struct Node* left;
struct Node* right;
int height;
} Node;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А вообще, этой структуре не место в .h (даже предварительному объявлению — иначе нарушение принципа сокрытия деталей реализации, для внешнего мира АВЛ-дерево — это просто словарь).

Comment thread src/hw_2/avl-tree.h
typedef struct Tree {
Node* root;
int size;
} Tree;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

И это полностью определять в заголовочном файле не надо, достаточно предварительного объявления

Comment thread src/hw_2/avl-tree.h

Tree* createTree(void);
Node* createNode(char* code, char* name);
Node* insertRecursive(Node* node, char* code, char* name);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Хорошая новость, правда, в том, что далеко не все функции должны быть в заголовочном файле, а только интерфейс модуля.

Comment thread src/hw_2/main.c
saveRecursive(tree->root, file);
fclose(file);
printf("База сохранена: %d аэропортов.\n", tree->size);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Непонятно, как это всё тестировать. Я бы отдельным модулем сделал.

Comment thread src/hw_2/avl-tree.h
int size;
} Tree;

Tree* createTree(void);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А вот комментарии к каждой функции в заголовочном файле — нужны

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants