Skip to content

Comments

search tree#32

Open
Andrw-404 wants to merge 2 commits intomainfrom
hw8-searchTree
Open

search tree#32
Andrw-404 wants to merge 2 commits intomainfrom
hw8-searchTree

Conversation

@Andrw-404
Copy link
Owner

No description provided.

void main(void) {
setlocale(LC_ALL, "RUS");
if (!runTests()) {
return;

Choose a reason for hiding this comment

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

Возвращаёте не 0

if (!runTests()) {
return;
}
Node* root = NULL;

Choose a reason for hiding this comment

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

Хочется в клиентском коде не использовать такие низкоуровневые термины, как root

printf("\n\n");
switch (choice)
{
case(1):

Choose a reason for hiding this comment

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

Suggested change
case(1):
case 1:

И ниже


bool testForCreateNode() {
Node* root = createNode(4, "dq");
return root != NULL;

Choose a reason for hiding this comment

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

Так удалить бы ещё


#include <stdbool.h>

bool testForCreateNode();

Choose a reason for hiding this comment

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

Нужны комментарии

Comment on lines 31 to 32
newNode->string = malloc(strlen(string) + 1);
strcpy(newNode->string, string);

Choose a reason for hiding this comment

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

strdup

Node* add(Node* root, int item, char* string) {
Node* existingNode = search(root, item);
if (existingNode != NULL) {
strcpy(existingNode->string, string);

Choose a reason for hiding this comment

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

Никто не обещал, что в existingNode->string поместится string

}

Node* add(Node* root, int item, char* string) {
Node* existingNode = search(root, item);

Choose a reason for hiding this comment

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

Это делается при каждом рекурсивном вызове add, причём search работает за логарифм, так что суммарная трудоёмкость как-то не очень


bool checkTheKey(Node* root, int item);

Node* findMin(Node* root);

Choose a reason for hiding this comment

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

Далеко не все функции нужны в хедере


typedef Node* Position;

Node* createNode(int value, const char* string);

Choose a reason for hiding this comment

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

Нужны комментарии ещё

#include <stdbool.h>

// test for the node creation function
bool testForCreateNode();

Choose a reason for hiding this comment

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

Suggested change
bool testForCreateNode();
bool testForCreateNode(void);

Тут и ниже

bool testForAdd() {
Node* root = createNode(8, "q");

root = add(root, 3, "1");

Choose a reason for hiding this comment

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

Конкретно у Вас так нельзя. add в случае неудачи выделения памяти вернёт NULL не удалив предыдущее дерево, так что если его перезаписать тут, оно всё потеряется и память утечёт. Так что тут каждый add должен работать как realloc — сначала сохраняем результат во временную переменную, затем проверяем её на NULL, и если что, удаляем дерево, потом присваиваем в root.

Было бы удобнее иметь другой инвариант у add — в случае неуспешной аллокации дерево не меняется и никакая дополнительная память не остаётся выделенной. Но тогда нужен какой-то другой механизм сообщения вызывающему об ошибке — скорее всего, код возврата (его вызывающий тоже должен проверять, но он может это сделать после серии добавлений один раз).

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