From 5c0f1529376a4519eb24738c0fa59e7f287b53c2 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Mon, 22 Nov 2021 23:44:46 +0300 Subject: [PATCH 1/7] writing functions for adding and removing nodes --- AVL/AVLTree/AVLTree.sln | 31 ++ AVL/AVLTree/AVLTree/AVLTree.c | 495 ++++++++++++++++++++ AVL/AVLTree/AVLTree/AVLTree.h | 26 + AVL/AVLTree/AVLTree/AVLTree.vcxproj | 153 ++++++ AVL/AVLTree/AVLTree/AVLTree.vcxproj.filters | 36 ++ AVL/AVLTree/AVLTree/AVLTreeTest.c | 82 ++++ AVL/AVLTree/AVLTree/AVLTreeTest.h | 4 + AVL/AVLTree/AVLTree/Main.c | 10 + 8 files changed, 837 insertions(+) create mode 100644 AVL/AVLTree/AVLTree.sln create mode 100644 AVL/AVLTree/AVLTree/AVLTree.c create mode 100644 AVL/AVLTree/AVLTree/AVLTree.h create mode 100644 AVL/AVLTree/AVLTree/AVLTree.vcxproj create mode 100644 AVL/AVLTree/AVLTree/AVLTree.vcxproj.filters create mode 100644 AVL/AVLTree/AVLTree/AVLTreeTest.c create mode 100644 AVL/AVLTree/AVLTree/AVLTreeTest.h create mode 100644 AVL/AVLTree/AVLTree/Main.c diff --git a/AVL/AVLTree/AVLTree.sln b/AVL/AVLTree/AVLTree.sln new file mode 100644 index 0000000..718b45e --- /dev/null +++ b/AVL/AVLTree/AVLTree.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31410.357 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "AVLTree", "AVLTree\AVLTree.vcxproj", "{C1059FEC-D8BB-43B7-99BC-52AE57080933}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {C1059FEC-D8BB-43B7-99BC-52AE57080933}.Debug|x64.ActiveCfg = Debug|x64 + {C1059FEC-D8BB-43B7-99BC-52AE57080933}.Debug|x64.Build.0 = Debug|x64 + {C1059FEC-D8BB-43B7-99BC-52AE57080933}.Debug|x86.ActiveCfg = Debug|Win32 + {C1059FEC-D8BB-43B7-99BC-52AE57080933}.Debug|x86.Build.0 = Debug|Win32 + {C1059FEC-D8BB-43B7-99BC-52AE57080933}.Release|x64.ActiveCfg = Release|x64 + {C1059FEC-D8BB-43B7-99BC-52AE57080933}.Release|x64.Build.0 = Release|x64 + {C1059FEC-D8BB-43B7-99BC-52AE57080933}.Release|x86.ActiveCfg = Release|Win32 + {C1059FEC-D8BB-43B7-99BC-52AE57080933}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {A9B119A2-2026-4F5D-A0A8-2558BFF3D344} + EndGlobalSection +EndGlobal diff --git a/AVL/AVLTree/AVLTree/AVLTree.c b/AVL/AVLTree/AVLTree/AVLTree.c new file mode 100644 index 0000000..0f30a55 --- /dev/null +++ b/AVL/AVLTree/AVLTree/AVLTree.c @@ -0,0 +1,495 @@ +#include "AVLTree.h" +#include +#include +#include + +typedef struct Node +{ + struct Node* leftSon; + struct Node* rightSon; + struct Node* parent; + int key; + char* value; + int rightHeight; + int leftHeight; + int balance; +} Node; + +typedef enum Direction +{ + left, + right +} Direction; + +Node* createTree() +{ + return NULL; +} + +void deleteTreeRecursive(Node* root) +{ + if (root == NULL) + { + return; + } + deleteTreeRecursive(root->leftSon); + deleteTreeRecursive(root->rightSon); + free(root->value); + free(root); +} + +void deleteTree(Node** root) +{ + deleteTreeRecursive(*root); + *root = NULL; +} + +void attach(Node* parent, Node* child, Direction direction) +{ + if (direction == left) + { + parent->leftSon = child; + } + else + { + parent->rightSon = child; + } + if (child != NULL) + { + child->parent = parent; + } +} + +int maxHeight(Node* x) +{ + if (x == NULL) + { + return -1; + } + return x->leftHeight > x->rightHeight ? x->leftHeight : x->rightHeight; +} + +void updateBalance(Node* root) +{ + root->balance = root->rightHeight - root->leftHeight; +} + +void checkHeight(Node* x) +{ + x->balance = x->rightHeight - x->leftHeight; + while (x->parent != NULL) + { + if (x->parent->leftSon == x) + { + x->parent->leftHeight = maxHeight(x) + 1; + } + else if (x->parent->rightSon == x) + { + x->parent->rightHeight = maxHeight(x) + 1; + } + x->parent->balance = x->parent->rightHeight - x->parent->leftHeight; + if (x->parent->balance == 2 || x->parent->balance == -2) + { + return; + } + x = x->parent; + } +} + +Node* leftSmallRotation(Node* lastRoot) +{ + Node* newRoot = lastRoot->rightSon; + Node* rootFather = lastRoot->parent; + attach(lastRoot, newRoot->leftSon, right); + attach(newRoot, lastRoot, left); + if (rootFather != NULL) + { + if (rootFather->leftSon == lastRoot) + { + rootFather->leftSon = newRoot; + } + else if (rootFather->rightSon == lastRoot) + { + rootFather->rightSon = newRoot; + } + } + newRoot->parent = rootFather; + lastRoot->rightHeight = maxHeight(lastRoot->rightSon) + 1; + updateBalance(lastRoot); + newRoot->leftHeight = maxHeight(lastRoot) + 1; + updateBalance(newRoot); + checkHeight(newRoot); + return newRoot; +} + +Node* rightSmallRotation(Node* lastRoot) +{ + Node* father = lastRoot->parent; + Node* newRoot = lastRoot->leftSon; + attach(lastRoot, newRoot->rightSon, left); + attach(newRoot, lastRoot, right); + if (father != NULL) + { + if (father->leftSon == lastRoot) + { + father->leftSon = newRoot; + } + else if (father->rightSon == lastRoot) + { + father->rightSon = newRoot; + } + } + newRoot->parent = father; + lastRoot->leftHeight = maxHeight(lastRoot->leftSon) + 1; + updateBalance(lastRoot); + newRoot->rightHeight = maxHeight(lastRoot) + 1; + updateBalance(newRoot); + checkHeight(newRoot); + return newRoot; +} + +Node* largeLeftRotation(Node* lastRoot) +{ + Node* father = lastRoot->parent; + Node* rightSon = lastRoot->rightSon; + Node* newRoot = rightSon->leftSon; + Node* newLeftSonForRightSon = rightSon->leftSon->rightSon; + Node* newRightSonForLastRoot = rightSon->leftSon->leftSon; + attach(lastRoot, newRightSonForLastRoot, right); + attach(rightSon, newLeftSonForRightSon, left); + attach(newRoot, lastRoot, left); + attach(newRoot, rightSon, right); + if (father != NULL) + { + if (father->leftSon == lastRoot) + { + father->leftSon = newRoot; + } + else if (father->rightSon == lastRoot) + { + father->rightSon = newRoot; + } + } + newRoot->parent = father; + rightSon->leftHeight = maxHeight(newLeftSonForRightSon) + 1; + updateBalance(rightSon); + lastRoot->rightHeight = maxHeight(newRightSonForLastRoot) + 1; + updateBalance(lastRoot); + newRoot->leftHeight = maxHeight(lastRoot) + 1; + newRoot->rightHeight = maxHeight(rightSon) + 1; + updateBalance(newRoot); + checkHeight(newRoot); + return newRoot; +} + +Node* largeRightRotation(Node* lastRoot) +{ + Node* father = lastRoot->parent; + Node* leftSon = lastRoot->leftSon; + Node* newRoot = leftSon->rightSon; + Node* newLeftSonForLastRoot = leftSon->rightSon->rightSon; + Node* newRightSonForLeftSon = leftSon->rightSon->leftSon; + attach(lastRoot, newLeftSonForLastRoot, left); + attach(leftSon, newRightSonForLeftSon, right); + attach(newRoot, lastRoot, right); + attach(newRoot, leftSon, left); + if (father != NULL) + { + if (father->leftSon == lastRoot) + { + father->leftSon = newRoot; + } + else if (father->rightSon == lastRoot) + { + father->rightSon = newRoot; + } + } + newRoot->parent = father; + leftSon->rightHeight = maxHeight(newRightSonForLeftSon) + 1; + updateBalance(leftSon); + lastRoot->leftHeight = maxHeight(newLeftSonForLastRoot) + 1; + updateBalance(lastRoot); + newRoot->leftHeight = maxHeight(leftSon) + 1; + newRoot->rightHeight = maxHeight(lastRoot) + 1; + updateBalance(newRoot); + checkHeight(newRoot); + return newRoot; +} + +Node* checkBalance(Node* root) +{ + while (root->parent != NULL) + { + if (root->parent->balance == -2) + { + if (root->leftHeight >= root->rightHeight) + { + root = rightSmallRotation(root->parent); + } + else + { + root = largeRightRotation(root->parent); + } + } + else if (root->parent->balance == 2) + { + if (root->rightHeight >= root->leftHeight) + { + root = leftSmallRotation(root->parent); + } + else + { + root = largeLeftRotation(root->parent); + } + } + if (root->parent == NULL) + { + break; + } + root = root->parent; + } + return root; +} + +Node* createNewNode(Node* root, int key, char* value, int* error) +{ + char* copyValue = calloc(strlen(value) + 1, sizeof(char)); + if (copyValue == NULL) + { + *error = 1; + return root; + } + strcpy(copyValue, value); + if (root != NULL && key == root->key) + { + free(root->value); + root->value = copyValue; + return root; + } + Node* newRoot = (Node*)calloc(1, sizeof(Node)); + if (newRoot == NULL) + { + *error = 1; + free(copyValue); + return root; + } + newRoot->key = key; + newRoot->value = copyValue; + return newRoot; +} + +void startCheckingHeights(Node* root, Node* newRoot, Direction direction) +{ + direction == left ? root->leftHeight++ : root->rightHeight++; + checkHeight(newRoot); +} + +Node* addNode(Node* root, int key, char* value, int* error) +{ + if (*error != 0) + { + return root; + } + if (root == NULL) + { + return createNewNode(root, key, value, error); + } + while(root != NULL) + { + if (key > root->key) + { + if (root->rightSon == NULL) + { + Node* newRoot = createNewNode(root, key, value, error); + if (*error == 1) + { + return root; + } + attach(root, newRoot, right); + startCheckingHeights(root, newRoot, right); + return checkBalance(newRoot); + } + root = root->rightSon; + } + else if (key < root->key) + { + if (root->leftSon == NULL) + { + Node* newRoot = createNewNode(root, key, value, error); + if (*error == 1) + { + return root; + } + attach(root, newRoot, left); + startCheckingHeights(root, newRoot, left); + return checkBalance(newRoot); + } + root = root->leftSon; + } + else + { + return createNewNode(root, key, value, error); + if (*error == 1) + { + return root; + } + } + } + return root; +} + +Node* search(Node* root, int key) +{ + Node* newRoot = root; + while (newRoot != NULL) + { + if (key > newRoot->key) + { + newRoot = newRoot->rightSon; + } + else if (key < newRoot->key) + { + newRoot = newRoot->leftSon; + } + else + { + return newRoot; + } + } + return NULL; +} + +void deleteNode(Node** root, int key, int* error) +{ + if (*error != 0) + { + return; + } + Node* searchResult = search(*root, key); + if (searchResult == NULL) + { + return; + } + *root = searchResult; + if ((*root)->rightSon == NULL && (*root)->leftSon == NULL) + { + Node* parent = (*root)->parent; + if (parent != NULL) + { + if ((*root)->parent->rightSon == (*root)) + { + (*root)->parent->rightSon = NULL; + parent->rightHeight = 0; + } + else + { + (*root)->parent->leftSon = NULL; + parent->leftHeight = 0; + } + } + checkHeight(parent); + Node* newRoot = checkBalance(*root); + free((*root)->value); + free(*root); + *root = parent == NULL ? NULL : newRoot; + return; + } + if ((*root)->rightSon != NULL && (*root)->leftSon != NULL) + { + Node* currentRoot = *root; + currentRoot = currentRoot->leftSon; + while (currentRoot->rightSon != NULL) + { + currentRoot = currentRoot->rightSon; + } + if (currentRoot == (*root)->leftSon) + { + Node* parent = (*root)->parent; + attach(currentRoot, (*root)->rightSon, right); + if (parent == NULL) + { + currentRoot->parent = NULL; + } + else + { + parent->rightSon == (*root) ? attach((*root)->parent, currentRoot, right) : attach((*root)->parent, currentRoot, left); + } + currentRoot->rightHeight = (*root)->rightHeight; + free((*root)->value); + free(*root); + checkHeight(currentRoot); + *root = checkBalance(currentRoot->rightSon); + return; + } + Node* currentRootParent = currentRoot->parent; + char* newValue = calloc(strlen((currentRoot)->value) + 1, sizeof(char)); + if (newValue == NULL) + { + return; + } + (*root)->key = currentRoot->key; + free((*root)->value); + strcpy(newValue, currentRoot->value); + (*root)->value = newValue; + currentRootParent->rightSon = NULL; + currentRootParent->rightHeight = 0; + checkHeight(currentRootParent); + Node* newRoot = checkBalance(currentRoot); + free(currentRoot->value); + free(currentRoot); + *root = newRoot; + return; + } + if ((*root)->parent == NULL) + { + *root = (*root)->rightSon == NULL ? (*root)->leftSon : (*root)->rightSon; + if ((*root)->parent != NULL) + { + free((*root)->parent->value); + } + free((*root)->parent); + (*root)->parent = NULL; + return; + } + Node* parent = (*root)->parent; + Node* newRoot = NULL; + if ((*root)->rightSon == NULL) + { + parent->leftSon == *root ? attach(parent, (*root)->leftSon, left) + : attach(parent, (*root)->leftSon, right); + checkHeight((*root)->leftSon); + newRoot = checkBalance((*root)->leftSon); + } + else + { + parent->leftSon == *root ? attach(parent, (*root)->rightSon, left) + : attach(parent, (*root)->rightSon, right); + checkHeight((*root)->rightSon); + newRoot = checkBalance((*root)->rightSon); + } + free((*root)->value); + free(*root); + *root = newRoot; + return; +} + +bool inTree(Node* root, int key) +{ + Node* searchResult = search(root, key); + return searchResult != NULL; +} + +char* getValue(Node* root, int key) +{ + Node* searchResult = search(root, key); + if (searchResult == NULL) + { + return NULL; + } + return searchResult->value; +} + +bool invariant(Node* node) +{ + return abs(node->rightHeight - node->leftHeight) < 2; +} \ No newline at end of file diff --git a/AVL/AVLTree/AVLTree/AVLTree.h b/AVL/AVLTree/AVLTree/AVLTree.h new file mode 100644 index 0000000..eef3586 --- /dev/null +++ b/AVL/AVLTree/AVLTree/AVLTree.h @@ -0,0 +1,26 @@ +#pragma once +#include + +// the structure representing the AVL tree +typedef struct Node Node; + +// function for adding a node to a tree +Node* addNode(Node* root, int key, char* value, int* error); + +// function for removing a node from a tree +void deleteNode(Node** root, int key, int* error); + +// function for deleting a tree +void deleteTree(Node** root); + +// function for creating a tree +Node* createTree(); + +// function for getting tree values +char* getValue(Node* root, int key); + +// function for checking the invariant of the tree +bool invariant(Node* node); + +// function for checking the presence of a key +bool inTree(Node* root, int key); \ No newline at end of file diff --git a/AVL/AVLTree/AVLTree/AVLTree.vcxproj b/AVL/AVLTree/AVLTree/AVLTree.vcxproj new file mode 100644 index 0000000..2fc10f5 --- /dev/null +++ b/AVL/AVLTree/AVLTree/AVLTree.vcxproj @@ -0,0 +1,153 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {c1059fec-d8bb-43b7-99bc-52ae57080933} + AVLTree + 10.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + + + false + + + + Level3 + true + WIN32;_DEBUG;_CRT_SECURE_NO_WARNINGS;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/AVL/AVLTree/AVLTree/AVLTree.vcxproj.filters b/AVL/AVLTree/AVLTree/AVLTree.vcxproj.filters new file mode 100644 index 0000000..f5c5eff --- /dev/null +++ b/AVL/AVLTree/AVLTree/AVLTree.vcxproj.filters @@ -0,0 +1,36 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Исходные файлы + + + Исходные файлы + + + Исходные файлы + + + + + Файлы заголовков + + + Файлы заголовков + + + \ No newline at end of file diff --git a/AVL/AVLTree/AVLTree/AVLTreeTest.c b/AVL/AVLTree/AVLTree/AVLTreeTest.c new file mode 100644 index 0000000..d17c397 --- /dev/null +++ b/AVL/AVLTree/AVLTree/AVLTreeTest.c @@ -0,0 +1,82 @@ +#include "AVLTree.h" +#include "AVLTreeTest.h" + +bool addNodeTest() +{ + Node* tree = createTree(); + int error = 0; + tree = addNode(tree, 12, "firstString", &error); + tree = addNode(tree, 4, "secondString", &error); + bool firstResult = invariant(tree); + tree = addNode(tree, 45, "value", &error); + bool secondResult = invariant(tree); + tree = addNode(tree, 34, "hello world", &error); + bool thirdResult = invariant(tree); + tree = addNode(tree, 29, "4 points", &error); + bool fourthResult = invariant(tree); + tree = addNode(tree, 223, "AVLTree", &error); + bool fifthResult = invariant(tree); + tree = addNode(tree, 223, "addNode", &error); + bool sixthResult = invariant(tree); + tree = addNode(tree, 52, "i want a session", &error); + bool seventhResult = invariant(tree); + tree = addNode(tree, 24, "twenty", &error); + bool eighthResult = invariant(tree); + tree = addNode(tree, 2, "lastValue", &error); + bool ninthResult = invariant(tree); + deleteTree(&tree); + return firstResult && secondResult && thirdResult && fourthResult && fifthResult + && sixthResult && seventhResult && eighthResult && ninthResult; +} + +bool firstDeleteNodeTest() +{ + Node* tree = createTree(); + int error = 0; + tree = addNode(tree, 3, "firstValue", &error); + tree = addNode(tree, 28, "assdf", &error); + tree = addNode(tree, 12, "xzcv", &error); + tree = addNode(tree, 344, "htrhrth", &error); + tree = addNode(tree, 21, "wertyy", &error); + tree = addNode(tree, 223, "fghgf", &error); + tree = addNode(tree, 52, "asfasf", &error); + tree = addNode(tree, -123, "qwrrwq", &error); + deleteNode(&tree, 52, &error); + deleteNode(&tree, 344, &error); + bool firstResult = invariant(tree); + deleteNode(&tree, 28, &error); + bool secondResult = invariant(tree); + deleteNode(&tree, 21, &error); + bool thirdResult = invariant(tree); + deleteNode(&tree, 223, &error); + bool fourthResult = invariant(tree); + deleteTree(&tree); + return firstResult && secondResult && thirdResult && fourthResult && error == 0; +} + +bool secondDeleteNodeTest() +{ + Node* tree = createTree(); + int error = 0; + tree = addNode(tree, 46, "frt", &error); + tree = addNode(tree, 14, "luej", &error); + tree = addNode(tree, 23, " ", &error); + tree = addNode(tree, -3, "(adsdsd)", &error); + tree = addNode(tree, 1, "firstValue", &error); + tree = addNode(tree, -22, "fcv", &error); + tree = addNode(tree, 5, "fi", &error); + tree = addNode(tree, -13, "kek", &error); + deleteNode(&tree, 21, &error); + bool firstResult = invariant(tree); + deleteNode(&tree, 12, &error); + deleteNode(&tree, 152, &error); + bool secondResult = invariant(tree); + deleteNode(&tree, 344, &error); + bool thirdResult = invariant(tree); + deleteTree(&tree); + return firstResult && secondResult && thirdResult && error == 0; +} +bool allTestResult() +{ + return addNodeTest() && secondDeleteNodeTest() && firstDeleteNodeTest(); +} \ No newline at end of file diff --git a/AVL/AVLTree/AVLTree/AVLTreeTest.h b/AVL/AVLTree/AVLTree/AVLTreeTest.h new file mode 100644 index 0000000..e7a55b8 --- /dev/null +++ b/AVL/AVLTree/AVLTree/AVLTreeTest.h @@ -0,0 +1,4 @@ +#pragma once +#include + +bool allTestResult(); \ No newline at end of file diff --git a/AVL/AVLTree/AVLTree/Main.c b/AVL/AVLTree/AVLTree/Main.c new file mode 100644 index 0000000..663d362 --- /dev/null +++ b/AVL/AVLTree/AVLTree/Main.c @@ -0,0 +1,10 @@ +#include "AVLTree.h" +#include "AVLTreeTest.h" + +int main() +{ + if (!allTestResult()) + { + return -1; + } +} \ No newline at end of file From 4c2c79d91017439595b38cd521cf49ae88296008 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Wed, 24 Nov 2021 17:22:41 +0300 Subject: [PATCH 2/7] interactive mode for avl tree --- .../AVLTree/AVLTree.sln" | 0 .../AVLTree/AVLTree/AVLTree.c" | 0 .../AVLTree/AVLTree/AVLTree.h" | 0 .../AVLTree/AVLTree/AVLTree.vcxproj" | 0 .../AVLTree/AVLTree/AVLTree.vcxproj.filters" | 0 .../AVLTree/AVLTree/AVLTreeTest.c" | 0 .../AVLTree/AVLTree/AVLTreeTest.h" | 0 .../AVLTree/AVLTree/Main.c" | 0 .../InteractiveModeForAVLTree.sln" | 31 ++++ .../InteractiveModeForAVLTree.c" | 0 .../InteractiveModeForAVLTree.vcxproj" | 150 ++++++++++++++++++ ...InteractiveModeForAVLTree.vcxproj.filters" | 17 ++ 12 files changed, 198 insertions(+) rename AVL/AVLTree/AVLTree.sln => "Homework \342\204\2268/AVLTree/AVLTree.sln" (100%) rename AVL/AVLTree/AVLTree/AVLTree.c => "Homework \342\204\2268/AVLTree/AVLTree/AVLTree.c" (100%) rename AVL/AVLTree/AVLTree/AVLTree.h => "Homework \342\204\2268/AVLTree/AVLTree/AVLTree.h" (100%) rename AVL/AVLTree/AVLTree/AVLTree.vcxproj => "Homework \342\204\2268/AVLTree/AVLTree/AVLTree.vcxproj" (100%) rename AVL/AVLTree/AVLTree/AVLTree.vcxproj.filters => "Homework \342\204\2268/AVLTree/AVLTree/AVLTree.vcxproj.filters" (100%) rename AVL/AVLTree/AVLTree/AVLTreeTest.c => "Homework \342\204\2268/AVLTree/AVLTree/AVLTreeTest.c" (100%) rename AVL/AVLTree/AVLTree/AVLTreeTest.h => "Homework \342\204\2268/AVLTree/AVLTree/AVLTreeTest.h" (100%) rename AVL/AVLTree/AVLTree/Main.c => "Homework \342\204\2268/AVLTree/AVLTree/Main.c" (100%) create mode 100644 "Homework \342\204\2268/InteractiveModeForAVLTree/InteractiveModeForAVLTree.sln" create mode 100644 "Homework \342\204\2268/InteractiveModeForAVLTree/InteractiveModeForAVLTree/InteractiveModeForAVLTree.c" create mode 100644 "Homework \342\204\2268/InteractiveModeForAVLTree/InteractiveModeForAVLTree/InteractiveModeForAVLTree.vcxproj" create mode 100644 "Homework \342\204\2268/InteractiveModeForAVLTree/InteractiveModeForAVLTree/InteractiveModeForAVLTree.vcxproj.filters" diff --git a/AVL/AVLTree/AVLTree.sln "b/Homework \342\204\2268/AVLTree/AVLTree.sln" similarity index 100% rename from AVL/AVLTree/AVLTree.sln rename to "Homework \342\204\2268/AVLTree/AVLTree.sln" diff --git a/AVL/AVLTree/AVLTree/AVLTree.c "b/Homework \342\204\2268/AVLTree/AVLTree/AVLTree.c" similarity index 100% rename from AVL/AVLTree/AVLTree/AVLTree.c rename to "Homework \342\204\2268/AVLTree/AVLTree/AVLTree.c" diff --git a/AVL/AVLTree/AVLTree/AVLTree.h "b/Homework \342\204\2268/AVLTree/AVLTree/AVLTree.h" similarity index 100% rename from AVL/AVLTree/AVLTree/AVLTree.h rename to "Homework \342\204\2268/AVLTree/AVLTree/AVLTree.h" diff --git a/AVL/AVLTree/AVLTree/AVLTree.vcxproj "b/Homework \342\204\2268/AVLTree/AVLTree/AVLTree.vcxproj" similarity index 100% rename from AVL/AVLTree/AVLTree/AVLTree.vcxproj rename to "Homework \342\204\2268/AVLTree/AVLTree/AVLTree.vcxproj" diff --git a/AVL/AVLTree/AVLTree/AVLTree.vcxproj.filters "b/Homework \342\204\2268/AVLTree/AVLTree/AVLTree.vcxproj.filters" similarity index 100% rename from AVL/AVLTree/AVLTree/AVLTree.vcxproj.filters rename to "Homework \342\204\2268/AVLTree/AVLTree/AVLTree.vcxproj.filters" diff --git a/AVL/AVLTree/AVLTree/AVLTreeTest.c "b/Homework \342\204\2268/AVLTree/AVLTree/AVLTreeTest.c" similarity index 100% rename from AVL/AVLTree/AVLTree/AVLTreeTest.c rename to "Homework \342\204\2268/AVLTree/AVLTree/AVLTreeTest.c" diff --git a/AVL/AVLTree/AVLTree/AVLTreeTest.h "b/Homework \342\204\2268/AVLTree/AVLTree/AVLTreeTest.h" similarity index 100% rename from AVL/AVLTree/AVLTree/AVLTreeTest.h rename to "Homework \342\204\2268/AVLTree/AVLTree/AVLTreeTest.h" diff --git a/AVL/AVLTree/AVLTree/Main.c "b/Homework \342\204\2268/AVLTree/AVLTree/Main.c" similarity index 100% rename from AVL/AVLTree/AVLTree/Main.c rename to "Homework \342\204\2268/AVLTree/AVLTree/Main.c" diff --git "a/Homework \342\204\2268/InteractiveModeForAVLTree/InteractiveModeForAVLTree.sln" "b/Homework \342\204\2268/InteractiveModeForAVLTree/InteractiveModeForAVLTree.sln" new file mode 100644 index 0000000..5195587 --- /dev/null +++ "b/Homework \342\204\2268/InteractiveModeForAVLTree/InteractiveModeForAVLTree.sln" @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31410.357 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "InteractiveModeForAVLTree", "InteractiveModeForAVLTree\InteractiveModeForAVLTree.vcxproj", "{C81F3454-6012-4747-8EDD-BECE10840E5F}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {C81F3454-6012-4747-8EDD-BECE10840E5F}.Debug|x64.ActiveCfg = Debug|x64 + {C81F3454-6012-4747-8EDD-BECE10840E5F}.Debug|x64.Build.0 = Debug|x64 + {C81F3454-6012-4747-8EDD-BECE10840E5F}.Debug|x86.ActiveCfg = Debug|Win32 + {C81F3454-6012-4747-8EDD-BECE10840E5F}.Debug|x86.Build.0 = Debug|Win32 + {C81F3454-6012-4747-8EDD-BECE10840E5F}.Release|x64.ActiveCfg = Release|x64 + {C81F3454-6012-4747-8EDD-BECE10840E5F}.Release|x64.Build.0 = Release|x64 + {C81F3454-6012-4747-8EDD-BECE10840E5F}.Release|x86.ActiveCfg = Release|Win32 + {C81F3454-6012-4747-8EDD-BECE10840E5F}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {AD07C14B-BBC3-4FCB-9D79-3E1BC602B70B} + EndGlobalSection +EndGlobal diff --git "a/Homework \342\204\2268/InteractiveModeForAVLTree/InteractiveModeForAVLTree/InteractiveModeForAVLTree.c" "b/Homework \342\204\2268/InteractiveModeForAVLTree/InteractiveModeForAVLTree/InteractiveModeForAVLTree.c" new file mode 100644 index 0000000..e69de29 diff --git "a/Homework \342\204\2268/InteractiveModeForAVLTree/InteractiveModeForAVLTree/InteractiveModeForAVLTree.vcxproj" "b/Homework \342\204\2268/InteractiveModeForAVLTree/InteractiveModeForAVLTree/InteractiveModeForAVLTree.vcxproj" new file mode 100644 index 0000000..f1e4018 --- /dev/null +++ "b/Homework \342\204\2268/InteractiveModeForAVLTree/InteractiveModeForAVLTree/InteractiveModeForAVLTree.vcxproj" @@ -0,0 +1,150 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + + 16.0 + Win32Proj + {c81f3454-6012-4747-8edd-bece10840e5f} + InteractiveModeForAVLTree + 10.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + + + false + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + diff --git "a/Homework \342\204\2268/InteractiveModeForAVLTree/InteractiveModeForAVLTree/InteractiveModeForAVLTree.vcxproj.filters" "b/Homework \342\204\2268/InteractiveModeForAVLTree/InteractiveModeForAVLTree/InteractiveModeForAVLTree.vcxproj.filters" new file mode 100644 index 0000000..153170c --- /dev/null +++ "b/Homework \342\204\2268/InteractiveModeForAVLTree/InteractiveModeForAVLTree/InteractiveModeForAVLTree.vcxproj.filters" @@ -0,0 +1,17 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + \ No newline at end of file From 878f6ad3bf7e10a2123e277dc6538cef82c8dbf3 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Wed, 24 Nov 2021 17:55:03 +0300 Subject: [PATCH 3/7] interactive mode for avl tree --- .../InteractiveModeForAVLTree.c" | 125 ++++++++++++++++++ .../InteractiveModeForAVLTree.vcxproj" | 43 +++--- ...InteractiveModeForAVLTree.vcxproj.filters" | 13 ++ 3 files changed, 160 insertions(+), 21 deletions(-) diff --git "a/Homework \342\204\2268/InteractiveModeForAVLTree/InteractiveModeForAVLTree/InteractiveModeForAVLTree.c" "b/Homework \342\204\2268/InteractiveModeForAVLTree/InteractiveModeForAVLTree/InteractiveModeForAVLTree.c" index e69de29..9c5c307 100644 --- "a/Homework \342\204\2268/InteractiveModeForAVLTree/InteractiveModeForAVLTree/InteractiveModeForAVLTree.c" +++ "b/Homework \342\204\2268/InteractiveModeForAVLTree/InteractiveModeForAVLTree/InteractiveModeForAVLTree.c" @@ -0,0 +1,125 @@ +#include "../../AVLTree/AVLTree/AVLTree.h" +#include +#include + +int main() +{ + setlocale(LC_ALL, "rus"); + printf("0 \n"); + printf("1 \n"); + printf("2 \n"); + printf("3 \n"); + printf("4 \n"); + Node* tree = createTree(); + int number = 1; + int error = 0; + while (number != 0) + { + printf(" \n"); + while (scanf_s("%d", &number) == 0) + { + printf(" 0 4\n"); + while (getchar() != '\n') + { + continue; + } + } + switch (number) + { + case 0: + { + deleteTree(&tree); + return 0; + } + case 1: + { + printf(" \n"); + int key = 0; + const int firstScanfResult = scanf_s("%d", &key); + if (firstScanfResult == 0) + { + printf(" \n"); + break; + } + printf(" ( 100 \n"); + char string[100] = { '\0' }; + const int secondScanfResult = scanf_s("%s", string, (unsigned)sizeof(string)); + if (secondScanfResult == 0) + { + printf(" \n"); + break; + } + tree = addNode(tree, key, string, &error); + if (error == 1) + { + deleteTree(&tree); + printf(" \n"); + return -1; + } + printf(" \n"); + break; + } + case 2: + { + printf(" \n"); + int key = 0; + const int firstScanfResult = scanf_s("%d", &key); + if (firstScanfResult == 0) + { + printf(" \n"); + break; + } + printf("%s\n", getValue(tree, key)); + break; + } + case 3: + { + printf(" \n"); + int key = 0; + const int firstScanfResult = scanf_s("%d", &key); + if (firstScanfResult == 0) + { + printf(" \n"); + break; + } + bool result = inTree(tree, key); + if (!result) + { + printf(" \n"); + break; + } + printf(" \n"); + break; + } + case 4: + { + printf(" \n"); + int key = 0; + const int firstScanfResult = scanf_s("%d", &key); + if (firstScanfResult == 0) + { + printf(" \n"); + break; + } + deleteNode(&tree, key, &error); + if (error == 1) + { + printf(" \n"); + return -1; + } + if (error == 2) + { + printf(" \n"); + break; + } + printf(" \n"); + break; + } + default: + { + printf(" 0 4\n"); + break; + } + } + } +} \ No newline at end of file diff --git "a/Homework \342\204\2268/InteractiveModeForAVLTree/InteractiveModeForAVLTree/InteractiveModeForAVLTree.vcxproj" "b/Homework \342\204\2268/InteractiveModeForAVLTree/InteractiveModeForAVLTree/InteractiveModeForAVLTree.vcxproj" index f1e4018..26749da 100644 --- "a/Homework \342\204\2268/InteractiveModeForAVLTree/InteractiveModeForAVLTree/InteractiveModeForAVLTree.vcxproj" +++ "b/Homework \342\204\2268/InteractiveModeForAVLTree/InteractiveModeForAVLTree/InteractiveModeForAVLTree.vcxproj" @@ -17,7 +17,6 @@ Release x64 - 16.0 @@ -53,25 +52,23 @@ true Unicode - - + + + + + + + + + + + + + - - - - - - - - - - - - - true @@ -85,12 +82,11 @@ false - Level3 true - WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + WIN32;_DEBUG;_CRT_SECURE_NO_WARNINGS;_CONSOLE;%(PreprocessorDefinitions) true @@ -142,9 +138,14 @@ true - - + + + + + + + - + \ No newline at end of file diff --git "a/Homework \342\204\2268/InteractiveModeForAVLTree/InteractiveModeForAVLTree/InteractiveModeForAVLTree.vcxproj.filters" "b/Homework \342\204\2268/InteractiveModeForAVLTree/InteractiveModeForAVLTree/InteractiveModeForAVLTree.vcxproj.filters" index 153170c..c1a5e51 100644 --- "a/Homework \342\204\2268/InteractiveModeForAVLTree/InteractiveModeForAVLTree/InteractiveModeForAVLTree.vcxproj.filters" +++ "b/Homework \342\204\2268/InteractiveModeForAVLTree/InteractiveModeForAVLTree/InteractiveModeForAVLTree.vcxproj.filters" @@ -14,4 +14,17 @@ rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + Исходные файлы + + + Исходные файлы + + + + + Файлы заголовков + + \ No newline at end of file From d455822e627d3b4c1e2691358ae9ccff02fd621e Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Wed, 24 Nov 2021 17:55:52 +0300 Subject: [PATCH 4/7] changed the function for removing the element --- "Homework \342\204\2268/AVLTree/AVLTree/AVLTree.c" | 1 + 1 file changed, 1 insertion(+) diff --git "a/Homework \342\204\2268/AVLTree/AVLTree/AVLTree.c" "b/Homework \342\204\2268/AVLTree/AVLTree/AVLTree.c" index 0f30a55..8a34e97 100644 --- "a/Homework \342\204\2268/AVLTree/AVLTree/AVLTree.c" +++ "b/Homework \342\204\2268/AVLTree/AVLTree/AVLTree.c" @@ -368,6 +368,7 @@ void deleteNode(Node** root, int key, int* error) Node* searchResult = search(*root, key); if (searchResult == NULL) { + *error = 2; return; } *root = searchResult; From 4ed2ee8f135f41b242dc91f00020e6e9f32e6fde Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sun, 5 Dec 2021 12:15:50 +0300 Subject: [PATCH 5/7] The key has been changed to string, variable names have been corrected, comments have been added --- .../AVLTree/AVLTree/AVLTree.c" | 103 +++++++++--------- .../AVLTree/AVLTree/AVLTree.h" | 8 +- .../AVLTree/AVLTree/AVLTreeTest.c" | 72 ++++++------ .../AVLTree/AVLTree/AVLTreeTest.h" | 1 + 4 files changed, 94 insertions(+), 90 deletions(-) diff --git "a/Homework \342\204\2268/AVLTree/AVLTree/AVLTree.c" "b/Homework \342\204\2268/AVLTree/AVLTree/AVLTree.c" index 8a34e97..96d79ca 100644 --- "a/Homework \342\204\2268/AVLTree/AVLTree/AVLTree.c" +++ "b/Homework \342\204\2268/AVLTree/AVLTree/AVLTree.c" @@ -1,3 +1,4 @@ +#define _CRT_SECURE_NO_WARNINGS #include "AVLTree.h" #include #include @@ -8,11 +9,10 @@ typedef struct Node struct Node* leftSon; struct Node* rightSon; struct Node* parent; - int key; + char* key; char* value; int rightHeight; int leftHeight; - int balance; } Node; typedef enum Direction @@ -35,6 +35,7 @@ void deleteTreeRecursive(Node* root) deleteTreeRecursive(root->leftSon); deleteTreeRecursive(root->rightSon); free(root->value); + free(root->key); free(root); } @@ -69,14 +70,8 @@ int maxHeight(Node* x) return x->leftHeight > x->rightHeight ? x->leftHeight : x->rightHeight; } -void updateBalance(Node* root) -{ - root->balance = root->rightHeight - root->leftHeight; -} - void checkHeight(Node* x) { - x->balance = x->rightHeight - x->leftHeight; while (x->parent != NULL) { if (x->parent->leftSon == x) @@ -87,8 +82,8 @@ void checkHeight(Node* x) { x->parent->rightHeight = maxHeight(x) + 1; } - x->parent->balance = x->parent->rightHeight - x->parent->leftHeight; - if (x->parent->balance == 2 || x->parent->balance == -2) + if (x->parent->rightHeight - x->parent->leftHeight == 2 + || x->parent->rightHeight - x->parent->leftHeight == -2) { return; } @@ -115,9 +110,7 @@ Node* leftSmallRotation(Node* lastRoot) } newRoot->parent = rootFather; lastRoot->rightHeight = maxHeight(lastRoot->rightSon) + 1; - updateBalance(lastRoot); newRoot->leftHeight = maxHeight(lastRoot) + 1; - updateBalance(newRoot); checkHeight(newRoot); return newRoot; } @@ -141,9 +134,7 @@ Node* rightSmallRotation(Node* lastRoot) } newRoot->parent = father; lastRoot->leftHeight = maxHeight(lastRoot->leftSon) + 1; - updateBalance(lastRoot); newRoot->rightHeight = maxHeight(lastRoot) + 1; - updateBalance(newRoot); checkHeight(newRoot); return newRoot; } @@ -172,12 +163,9 @@ Node* largeLeftRotation(Node* lastRoot) } newRoot->parent = father; rightSon->leftHeight = maxHeight(newLeftSonForRightSon) + 1; - updateBalance(rightSon); lastRoot->rightHeight = maxHeight(newRightSonForLastRoot) + 1; - updateBalance(lastRoot); newRoot->leftHeight = maxHeight(lastRoot) + 1; newRoot->rightHeight = maxHeight(rightSon) + 1; - updateBalance(newRoot); checkHeight(newRoot); return newRoot; } @@ -206,12 +194,9 @@ Node* largeRightRotation(Node* lastRoot) } newRoot->parent = father; leftSon->rightHeight = maxHeight(newRightSonForLeftSon) + 1; - updateBalance(leftSon); lastRoot->leftHeight = maxHeight(newLeftSonForLastRoot) + 1; - updateBalance(lastRoot); newRoot->leftHeight = maxHeight(leftSon) + 1; newRoot->rightHeight = maxHeight(lastRoot) + 1; - updateBalance(newRoot); checkHeight(newRoot); return newRoot; } @@ -220,7 +205,7 @@ Node* checkBalance(Node* root) { while (root->parent != NULL) { - if (root->parent->balance == -2) + if (root->parent->rightHeight - root->parent->leftHeight == -2) { if (root->leftHeight >= root->rightHeight) { @@ -231,7 +216,7 @@ Node* checkBalance(Node* root) root = largeRightRotation(root->parent); } } - else if (root->parent->balance == 2) + else if (root->parent->rightHeight - root->parent->leftHeight == 2) { if (root->rightHeight >= root->leftHeight) { @@ -251,7 +236,7 @@ Node* checkBalance(Node* root) return root; } -Node* createNewNode(Node* root, int key, char* value, int* error) +Node* createNewNode(Node* root, char* key, char* value, int* error) { char* copyValue = calloc(strlen(value) + 1, sizeof(char)); if (copyValue == NULL) @@ -266,16 +251,24 @@ Node* createNewNode(Node* root, int key, char* value, int* error) root->value = copyValue; return root; } - Node* newRoot = (Node*)calloc(1, sizeof(Node)); - if (newRoot == NULL) + Node* newNode = (Node*)calloc(1, sizeof(Node)); + if (newNode == NULL) { *error = 1; free(copyValue); return root; } - newRoot->key = key; - newRoot->value = copyValue; - return newRoot; + char* keyCopy = calloc(strlen(key) + 1, sizeof(char)); + if (keyCopy == NULL) + { + free(newNode); + free(copyValue); + return root; + } + strcpy(keyCopy, key); + newNode->key = keyCopy; + newNode->value = copyValue; + return newNode; } void startCheckingHeights(Node* root, Node* newRoot, Direction direction) @@ -284,7 +277,7 @@ void startCheckingHeights(Node* root, Node* newRoot, Direction direction) checkHeight(newRoot); } -Node* addNode(Node* root, int key, char* value, int* error) +Node* addNode(Node* root, char* key, char* value, int* error) { if (*error != 0) { @@ -294,9 +287,9 @@ Node* addNode(Node* root, int key, char* value, int* error) { return createNewNode(root, key, value, error); } - while(root != NULL) - { - if (key > root->key) + while (root != NULL) + { + if (strcmp(key, root->key) > 0) { if (root->rightSon == NULL) { @@ -311,7 +304,7 @@ Node* addNode(Node* root, int key, char* value, int* error) } root = root->rightSon; } - else if (key < root->key) + else if (strcmp(key, root->key) < 0) { if (root->leftSon == NULL) { @@ -338,28 +331,28 @@ Node* addNode(Node* root, int key, char* value, int* error) return root; } -Node* search(Node* root, int key) +Node* search(Node* root, char* key) { - Node* newRoot = root; - while (newRoot != NULL) + Node* node = root; + while (node != NULL) { - if (key > newRoot->key) + if (strcmp(key, node->key) > 0) { - newRoot = newRoot->rightSon; + node = node->rightSon; } - else if (key < newRoot->key) + else if (strcmp(key, node->key) < 0) { - newRoot = newRoot->leftSon; + node = node->leftSon; } else { - return newRoot; + return node; } } return NULL; } -void deleteNode(Node** root, int key, int* error) +void deleteNode(Node** root, char* key, int* error) { if (*error != 0) { @@ -368,7 +361,6 @@ void deleteNode(Node** root, int key, int* error) Node* searchResult = search(*root, key); if (searchResult == NULL) { - *error = 2; return; } *root = searchResult; @@ -377,7 +369,7 @@ void deleteNode(Node** root, int key, int* error) Node* parent = (*root)->parent; if (parent != NULL) { - if ((*root)->parent->rightSon == (*root)) + if ((*root)->parent->rightSon == *root) { (*root)->parent->rightSon = NULL; parent->rightHeight = 0; @@ -390,6 +382,7 @@ void deleteNode(Node** root, int key, int* error) } checkHeight(parent); Node* newRoot = checkBalance(*root); + free((*root)->key); free((*root)->value); free(*root); *root = parent == NULL ? NULL : newRoot; @@ -416,6 +409,7 @@ void deleteNode(Node** root, int key, int* error) parent->rightSon == (*root) ? attach((*root)->parent, currentRoot, right) : attach((*root)->parent, currentRoot, left); } currentRoot->rightHeight = (*root)->rightHeight; + free((*root)->key); free((*root)->value); free(*root); checkHeight(currentRoot); @@ -428,7 +422,15 @@ void deleteNode(Node** root, int key, int* error) { return; } - (*root)->key = currentRoot->key; + char* newKey = calloc(strlen((currentRoot)->key) + 1, sizeof(char)); + if (newKey == NULL) + { + free(newValue); + return; + } + strcpy(newKey, key); + free((*root)->key); + (*root)->key = newKey; free((*root)->value); strcpy(newValue, currentRoot->value); (*root)->value = newValue; @@ -437,6 +439,7 @@ void deleteNode(Node** root, int key, int* error) checkHeight(currentRootParent); Node* newRoot = checkBalance(currentRoot); free(currentRoot->value); + free(currentRoot->key); free(currentRoot); *root = newRoot; return; @@ -447,6 +450,7 @@ void deleteNode(Node** root, int key, int* error) if ((*root)->parent != NULL) { free((*root)->parent->value); + free((*root)->parent->key); } free((*root)->parent); (*root)->parent = NULL; @@ -469,18 +473,17 @@ void deleteNode(Node** root, int key, int* error) newRoot = checkBalance((*root)->rightSon); } free((*root)->value); + free((*root)->key); free(*root); *root = newRoot; - return; } -bool inTree(Node* root, int key) +bool inTree(Node* root, char* key) { - Node* searchResult = search(root, key); - return searchResult != NULL; + return search(root, key) != NULL; } -char* getValue(Node* root, int key) +char* getValue(Node* root, char* key) { Node* searchResult = search(root, key); if (searchResult == NULL) diff --git "a/Homework \342\204\2268/AVLTree/AVLTree/AVLTree.h" "b/Homework \342\204\2268/AVLTree/AVLTree/AVLTree.h" index eef3586..a48ef54 100644 --- "a/Homework \342\204\2268/AVLTree/AVLTree/AVLTree.h" +++ "b/Homework \342\204\2268/AVLTree/AVLTree/AVLTree.h" @@ -5,10 +5,10 @@ typedef struct Node Node; // function for adding a node to a tree -Node* addNode(Node* root, int key, char* value, int* error); +Node* addNode(Node* root, char* key, char* value, int* error); // function for removing a node from a tree -void deleteNode(Node** root, int key, int* error); +void deleteNode(Node** root, char* key, int* error); // function for deleting a tree void deleteTree(Node** root); @@ -17,10 +17,10 @@ void deleteTree(Node** root); Node* createTree(); // function for getting tree values -char* getValue(Node* root, int key); +char* getValue(Node* root, char* key); // function for checking the invariant of the tree bool invariant(Node* node); // function for checking the presence of a key -bool inTree(Node* root, int key); \ No newline at end of file +bool inTree(Node* root, char* key); \ No newline at end of file diff --git "a/Homework \342\204\2268/AVLTree/AVLTree/AVLTreeTest.c" "b/Homework \342\204\2268/AVLTree/AVLTree/AVLTreeTest.c" index d17c397..427de22 100644 --- "a/Homework \342\204\2268/AVLTree/AVLTree/AVLTreeTest.c" +++ "b/Homework \342\204\2268/AVLTree/AVLTree/AVLTreeTest.c" @@ -5,24 +5,24 @@ bool addNodeTest() { Node* tree = createTree(); int error = 0; - tree = addNode(tree, 12, "firstString", &error); - tree = addNode(tree, 4, "secondString", &error); + tree = addNode(tree, "firstString", "firstString", &error); + tree = addNode(tree, "secondString", "secondString", &error); bool firstResult = invariant(tree); - tree = addNode(tree, 45, "value", &error); + tree = addNode(tree, "value", "value", &error); bool secondResult = invariant(tree); - tree = addNode(tree, 34, "hello world", &error); + tree = addNode(tree, "hello world", "hello world", &error); bool thirdResult = invariant(tree); - tree = addNode(tree, 29, "4 points", &error); + tree = addNode(tree, "4 points", "4 points", &error); bool fourthResult = invariant(tree); - tree = addNode(tree, 223, "AVLTree", &error); + tree = addNode(tree, "AVLTree", "AVLTree", &error); bool fifthResult = invariant(tree); - tree = addNode(tree, 223, "addNode", &error); + tree = addNode(tree, "addNode", "addNode", &error); bool sixthResult = invariant(tree); - tree = addNode(tree, 52, "i want a session", &error); + tree = addNode(tree, "i want a session", "i want a session", &error); bool seventhResult = invariant(tree); - tree = addNode(tree, 24, "twenty", &error); + tree = addNode(tree, "twenty", "twenty", &error); bool eighthResult = invariant(tree); - tree = addNode(tree, 2, "lastValue", &error); + tree = addNode(tree, "lastValue", "lastValue", &error); bool ninthResult = invariant(tree); deleteTree(&tree); return firstResult && secondResult && thirdResult && fourthResult && fifthResult @@ -33,22 +33,22 @@ bool firstDeleteNodeTest() { Node* tree = createTree(); int error = 0; - tree = addNode(tree, 3, "firstValue", &error); - tree = addNode(tree, 28, "assdf", &error); - tree = addNode(tree, 12, "xzcv", &error); - tree = addNode(tree, 344, "htrhrth", &error); - tree = addNode(tree, 21, "wertyy", &error); - tree = addNode(tree, 223, "fghgf", &error); - tree = addNode(tree, 52, "asfasf", &error); - tree = addNode(tree, -123, "qwrrwq", &error); - deleteNode(&tree, 52, &error); - deleteNode(&tree, 344, &error); + tree = addNode(tree, "firstValue", "firstValue", &error); + tree = addNode(tree, "assdf", "assdf", &error); + tree = addNode(tree, "xzcv", "xzcv", &error); + tree = addNode(tree, "htrhrth", "htrhrth", &error); + tree = addNode(tree, "wertyy", "wertyy", &error); + tree = addNode(tree, "fghgf", "fghgf", &error); + tree = addNode(tree, "asfasf", "asfasf", &error); + tree = addNode(tree, "qwrrwq", "qwrrwq", &error); + deleteNode(&tree, "asfasf", &error); + deleteNode(&tree, "htrhrth", &error); bool firstResult = invariant(tree); - deleteNode(&tree, 28, &error); + deleteNode(&tree, "assdf", &error); bool secondResult = invariant(tree); - deleteNode(&tree, 21, &error); + deleteNode(&tree, "wertyy", &error); bool thirdResult = invariant(tree); - deleteNode(&tree, 223, &error); + deleteNode(&tree, "fghgf", &error); bool fourthResult = invariant(tree); deleteTree(&tree); return firstResult && secondResult && thirdResult && fourthResult && error == 0; @@ -58,25 +58,25 @@ bool secondDeleteNodeTest() { Node* tree = createTree(); int error = 0; - tree = addNode(tree, 46, "frt", &error); - tree = addNode(tree, 14, "luej", &error); - tree = addNode(tree, 23, " ", &error); - tree = addNode(tree, -3, "(adsdsd)", &error); - tree = addNode(tree, 1, "firstValue", &error); - tree = addNode(tree, -22, "fcv", &error); - tree = addNode(tree, 5, "fi", &error); - tree = addNode(tree, -13, "kek", &error); - deleteNode(&tree, 21, &error); + tree = addNode(tree, "frt", "frt", &error); + tree = addNode(tree, "luej", "luej", &error); + tree = addNode(tree, " ", " ", &error); + tree = addNode(tree, "(adsdsd)", "(adsdsd)", &error); + tree = addNode(tree, "firstValue", "firstValue", &error); + tree = addNode(tree, "fcv", "fcv", &error); + tree = addNode(tree, "fi", "fi", &error); + tree = addNode(tree, "kek", "kek", &error); + deleteNode(&tree, "kek", &error); bool firstResult = invariant(tree); - deleteNode(&tree, 12, &error); - deleteNode(&tree, 152, &error); + deleteNode(&tree, "luej", &error); + deleteNode(&tree, "fcv", &error); bool secondResult = invariant(tree); - deleteNode(&tree, 344, &error); + deleteNode(&tree, "xzczxc", &error); bool thirdResult = invariant(tree); deleteTree(&tree); return firstResult && secondResult && thirdResult && error == 0; } bool allTestResult() { - return addNodeTest() && secondDeleteNodeTest() && firstDeleteNodeTest(); + return addNodeTest() && firstDeleteNodeTest() && secondDeleteNodeTest(); } \ No newline at end of file diff --git "a/Homework \342\204\2268/AVLTree/AVLTree/AVLTreeTest.h" "b/Homework \342\204\2268/AVLTree/AVLTree/AVLTreeTest.h" index e7a55b8..911dcf1 100644 --- "a/Homework \342\204\2268/AVLTree/AVLTree/AVLTreeTest.h" +++ "b/Homework \342\204\2268/AVLTree/AVLTree/AVLTreeTest.h" @@ -1,4 +1,5 @@ #pragma once #include +// Function for returning test results bool allTestResult(); \ No newline at end of file From b009ac47c4797f919d1e1415e4c45add56455a1a Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sun, 5 Dec 2021 12:27:09 +0300 Subject: [PATCH 6/7] The key has been changed to string --- .../AVLTree/AVLTree/AVLTree.c" | 5 +++- .../InteractiveModeForAVLTree.c" | 26 +++++++++---------- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git "a/Homework \342\204\2268/AVLTree/AVLTree/AVLTree.c" "b/Homework \342\204\2268/AVLTree/AVLTree/AVLTree.c" index 96d79ca..9a38853 100644 --- "a/Homework \342\204\2268/AVLTree/AVLTree/AVLTree.c" +++ "b/Homework \342\204\2268/AVLTree/AVLTree/AVLTree.c" @@ -380,7 +380,10 @@ void deleteNode(Node** root, char* key, int* error) parent->leftHeight = 0; } } - checkHeight(parent); + if (parent != NULL) + { + checkHeight(parent); + } Node* newRoot = checkBalance(*root); free((*root)->key); free((*root)->value); diff --git "a/Homework \342\204\2268/InteractiveModeForAVLTree/InteractiveModeForAVLTree/InteractiveModeForAVLTree.c" "b/Homework \342\204\2268/InteractiveModeForAVLTree/InteractiveModeForAVLTree/InteractiveModeForAVLTree.c" index 9c5c307..68fd280 100644 --- "a/Homework \342\204\2268/InteractiveModeForAVLTree/InteractiveModeForAVLTree/InteractiveModeForAVLTree.c" +++ "b/Homework \342\204\2268/InteractiveModeForAVLTree/InteractiveModeForAVLTree/InteractiveModeForAVLTree.c" @@ -33,15 +33,15 @@ int main() } case 1: { - printf(" \n"); - int key = 0; - const int firstScanfResult = scanf_s("%d", &key); + printf(" ( 100 )\n"); + char key[100] = {'\0'}; + const int firstScanfResult = scanf_s("%s", key, (unsigned)sizeof(key)); if (firstScanfResult == 0) { printf(" \n"); break; } - printf(" ( 100 \n"); + printf(" ( 100 )\n"); char string[100] = { '\0' }; const int secondScanfResult = scanf_s("%s", string, (unsigned)sizeof(string)); if (secondScanfResult == 0) @@ -61,9 +61,9 @@ int main() } case 2: { - printf(" \n"); - int key = 0; - const int firstScanfResult = scanf_s("%d", &key); + printf(" ( 100 )\n"); + char key[100] = { '\0' }; + const int firstScanfResult = scanf_s("%s", key, (unsigned)sizeof(key)); if (firstScanfResult == 0) { printf(" \n"); @@ -74,9 +74,9 @@ int main() } case 3: { - printf(" \n"); - int key = 0; - const int firstScanfResult = scanf_s("%d", &key); + printf(" ( 100 )\n"); + char key[100] = { '\0' }; + const int firstScanfResult = scanf_s("%s", key, (unsigned)sizeof(key)); if (firstScanfResult == 0) { printf(" \n"); @@ -93,9 +93,9 @@ int main() } case 4: { - printf(" \n"); - int key = 0; - const int firstScanfResult = scanf_s("%d", &key); + printf(" ( 100 )\n"); + char key[100] = { '\0' }; + const int firstScanfResult = scanf_s("%s", key, (unsigned)sizeof(key)); if (firstScanfResult == 0) { printf(" \n"); From c6e8f6ce1ca1b09845c75fa5bf00e576509b7657 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sun, 5 Dec 2021 12:33:28 +0300 Subject: [PATCH 7/7] changed the function for deleting a node --- "Homework \342\204\2268/AVLTree/AVLTree/AVLTree.c" | 3 --- 1 file changed, 3 deletions(-) diff --git "a/Homework \342\204\2268/AVLTree/AVLTree/AVLTree.c" "b/Homework \342\204\2268/AVLTree/AVLTree/AVLTree.c" index 9a38853..1eeaec2 100644 --- "a/Homework \342\204\2268/AVLTree/AVLTree/AVLTree.c" +++ "b/Homework \342\204\2268/AVLTree/AVLTree/AVLTree.c" @@ -379,9 +379,6 @@ void deleteNode(Node** root, char* key, int* error) (*root)->parent->leftSon = NULL; parent->leftHeight = 0; } - } - if (parent != NULL) - { checkHeight(parent); } Node* newRoot = checkBalance(*root);