Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions sortTree/sortTree.sln
Original file line number Diff line number Diff line change
@@ -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
58 changes: 58 additions & 0 deletions sortTree/sortTree/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include "tree.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

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]);
}
}

Choose a reason for hiding this comment

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

Память не удаляется


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;
}
151 changes: 151 additions & 0 deletions sortTree/sortTree/sortTree.vcxproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{7db42211-5b01-4741-85f7-c6f0b9661c6a}</ProjectGuid>
<RootNamespace>sortTree</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>false</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="tree.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.c" />
<ClCompile Include="tree.c" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
30 changes: 30 additions & 0 deletions sortTree/sortTree/sortTree.vcxproj.filters
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Исходные файлы">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Файлы заголовков">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="Файлы ресурсов">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="tree.h">
<Filter>Исходные файлы</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="tree.c">
<Filter>Исходные файлы</Filter>
</ClCompile>
<ClCompile Include="main.c">
<Filter>Исходные файлы</Filter>
</ClCompile>
</ItemGroup>
</Project>
94 changes: 94 additions & 0 deletions sortTree/sortTree/tree.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#include "tree.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>

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;
Comment on lines +17 to +18

Choose a reason for hiding this comment

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

Suggested change
Tree* tree = calloc(1, sizeof(tree));
return tree;
return calloc(1, sizeof(tree));

}

Node* createNode(void) {
Node* temp = calloc(1, sizeof(Node));
return temp;
}

void addToTree(Tree* tree, int value) {
if (tree == NULL) {
return ;

Choose a reason for hiding this comment

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

Suggested change
return ;
return;

Choose a reason for hiding this comment

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

Но ещё надо бы как-то просигнализировать вызывающему, что всё плохо

}
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;

Choose a reason for hiding this comment

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

О, глобальная переменная, минус два балла :) Надо было передавать как параметр по указателю везде, где она нужна.


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);
}

Choose a reason for hiding this comment

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

Само tree ещё не удаляется


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);
}
10 changes: 10 additions & 0 deletions sortTree/sortTree/tree.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef BINARY_TREE_H
#define BINARY_TREE_H

#include <stdbool.h>

typedef struct Node Node;

Choose a reason for hiding this comment

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

Это тут, к сожалению, не нужно, потому что этот модуль буквально ничего про tree внешнему миру не предоставляет


void addArrayToTree(int arrayOut[], int size);

Choose a reason for hiding this comment

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

Неудачное название функции. Она не добавляет массив в дерево, она сортирует массив


#endif