diff --git a/sortTree/sortTree.sln b/sortTree/sortTree.sln new file mode 100644 index 0000000..a2e3993 --- /dev/null +++ b/sortTree/sortTree.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.31903.59 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "sortTree", "sortTree\sortTree.vcxproj", "{7DB42211-5B01-4741-85F7-C6F0B9661C6A}" +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 + {7DB42211-5B01-4741-85F7-C6F0B9661C6A}.Debug|x64.ActiveCfg = Debug|x64 + {7DB42211-5B01-4741-85F7-C6F0B9661C6A}.Debug|x64.Build.0 = Debug|x64 + {7DB42211-5B01-4741-85F7-C6F0B9661C6A}.Debug|x86.ActiveCfg = Debug|Win32 + {7DB42211-5B01-4741-85F7-C6F0B9661C6A}.Debug|x86.Build.0 = Debug|Win32 + {7DB42211-5B01-4741-85F7-C6F0B9661C6A}.Release|x64.ActiveCfg = Release|x64 + {7DB42211-5B01-4741-85F7-C6F0B9661C6A}.Release|x64.Build.0 = Release|x64 + {7DB42211-5B01-4741-85F7-C6F0B9661C6A}.Release|x86.ActiveCfg = Release|Win32 + {7DB42211-5B01-4741-85F7-C6F0B9661C6A}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {44F8A495-5658-482D-B148-398CC978B3FC} + EndGlobalSection +EndGlobal diff --git a/sortTree/sortTree/main.c b/sortTree/sortTree/main.c new file mode 100644 index 0000000..8e5efcf --- /dev/null +++ b/sortTree/sortTree/main.c @@ -0,0 +1,58 @@ +#include "tree.h" +#include +#include +#include + +int scanOneInt(); + +bool test() { + int arrayOut[3] = { 2, 1, 3 }; + int correctArray[3] = { 1, 2, 3 }; + addArrayToTree(arrayOut, 3); + for (int i = 0; i < 3; ++i) { + if (arrayOut[i] != correctArray[i]) { + return false; + } + } + return true; +} + +int main() { + if (test()) { + printf("Test correct\n"); + } else { + printf("Error\n"); + return -1; + } + printf("Input size and elements\n"); + int size = scanOneInt(); + int* arrayOut = calloc(size, sizeof(int)); + if (arrayOut == NULL) { + printf("%s", "Error \n"); + return -1; + } + for (int i = 0; i < size; ++i) { + arrayOut[i] = scanOneInt(); + } + + addArrayToTree(arrayOut, size); + + for (int i = 0; i < size; ++i) { + printf("%d ", arrayOut[i]); + } +} + +int scanOneInt() { + int number = 0; + int checkScanf = scanf("%d", &number); + + while (checkScanf != 1) { + while (getchar() != '\n') { + } + + printf("%s", "Error \n"); + checkScanf = scanf("%d", &number); + } + + return number; +} \ No newline at end of file diff --git a/sortTree/sortTree/sortTree.vcxproj b/sortTree/sortTree/sortTree.vcxproj new file mode 100644 index 0000000..f5e9a19 --- /dev/null +++ b/sortTree/sortTree/sortTree.vcxproj @@ -0,0 +1,151 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {7db42211-5b01-4741-85f7-c6f0b9661c6a} + sortTree + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + 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 + false + _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/sortTree/sortTree/sortTree.vcxproj.filters b/sortTree/sortTree/sortTree.vcxproj.filters new file mode 100644 index 0000000..e652ce0 --- /dev/null +++ b/sortTree/sortTree/sortTree.vcxproj.filters @@ -0,0 +1,30 @@ + + + + + {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/sortTree/sortTree/tree.c b/sortTree/sortTree/tree.c new file mode 100644 index 0000000..f9ede2e --- /dev/null +++ b/sortTree/sortTree/tree.c @@ -0,0 +1,94 @@ +#include "tree.h" +#include +#include +#include + +typedef struct Node { + int value; + Node* right; + Node* left; +} Node; + +typedef struct Tree { + Node* root; +} Tree; + +Tree* createTree(void) { + Tree* tree = calloc(1, sizeof(tree)); + return tree; +} + +Node* createNode(void) { + Node* temp = calloc(1, sizeof(Node)); + return temp; +} + +void addToTree(Tree* tree, int value) { + if (tree == NULL) { + return ; + } + if (tree->root == NULL) { + tree->root = createNode(); + tree->root->value = value; + return; + } + Node* walker = tree->root; + while (walker) { + if (walker->value > value) { + if (walker->left == NULL) { + Node* temp = createNode(); + temp->value = value; + walker->left = temp; + return; + } + walker = walker->left; + } else { + if (walker->right == NULL) { + Node* temp = createNode(); + temp->value = value; + walker->right = temp; + return; + } + walker = walker->right; + } + } +} + +int j = 0; + +void helpedInOrder(Node* root, int arrayOut[]) { + if (root != NULL) { + helpedInOrder(root->left, arrayOut); + arrayOut[j] = root->value; + ++j; + helpedInOrder(root->right, arrayOut); + } +} + +void inOrder(Tree* tree, int arrayOut[]) { + helpedInOrder(tree->root, arrayOut); + j = 0; +} + +void helpBackOrderClear(Node* root) { + if (root != NULL) { + helpBackOrderClear(root->left); + helpBackOrderClear(root->right); + free(root); + } +} + +void backOrderClear(Tree* tree) { + helpBackOrderClear(tree->root); +} + +void addArrayToTree(int arrayOut[], int size) { + Tree* tree = createTree(); + + for (int i = 0; i < size; ++i) { + addToTree(tree, arrayOut[i]); + } + + inOrder(tree, arrayOut); + backOrderClear(tree); +} \ No newline at end of file diff --git a/sortTree/sortTree/tree.h b/sortTree/sortTree/tree.h new file mode 100644 index 0000000..042379a --- /dev/null +++ b/sortTree/sortTree/tree.h @@ -0,0 +1,10 @@ +#ifndef BINARY_TREE_H +#define BINARY_TREE_H + +#include + +typedef struct Node Node; + +void addArrayToTree(int arrayOut[], int size); + +#endif \ No newline at end of file