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
2 changes: 1 addition & 1 deletion CourseApp.Tests/CourseApp.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
<NoWarn>1573,1591,1701;1702;1705</NoWarn>
<IsPackable>false</IsPackable>
Expand Down
21 changes: 8 additions & 13 deletions CourseApp/CourseApp.csproj
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
<NoWarn>1573,1591,1701;1702;1705;</NoWarn>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="StyleCop.Analyzers" Version="1.0.2" PrivateAssets="all" />
</ItemGroup>

<PropertyGroup>
<CodeAnalysisRuleSet>../_stylecop/stylecop.ruleset</CodeAnalysisRuleSet>
<CodeAnalysisRuleSet>../_stylecop/stylecop.ruleset</CodeAnalysisRuleSet>
<GenerateFullPaths>true</GenerateFullPaths>
</PropertyGroup>

<ItemGroup>
<AdditionalFiles Include="../_stylecop/stylecop.json" />
<PackageReference Include="StyleCop.Analyzers" Version="1.0.2" PrivateAssets="all"/>
</ItemGroup>
<ItemGroup>
<AdditionalFiles Include="../_stylecop/stylecop.json"/>
</ItemGroup>

</Project>
</Project>
82 changes: 82 additions & 0 deletions CourseApp/Module5/Task_1/BinaryTree.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System.Collections.Generic;

namespace CourseApp.Module5.Task_1
{
public class BinaryTree
{
private Node root = null;
private int size = 0;

public bool Find (int n)
{
return InnerFind(n, root);
}

public List<int> GetElements()
{
List<int> elements = new List<int>();
InnerGetElements(root, elements);
return elements;
}

public void Insert (int n)
{
root = InnerInsert(n, root);
}

private bool InnerFind (int n, Node current)
{
if (current == null)
{
return false;
}
else if (current.Data == n)
{
return true;
}
else if (current.Data > n)
{
return InnerFind(n, current.Left);
}
else
{
return InnerFind(n, current.Right);
}
}

private Node InnerInsert(int n, Node current)
{
if (current == null)
{
size++;
return new Node(n);
}
else if (current.Data > n)
{
current.Left = InnerInsert(n, current.Left);
}
else if (current.Data < n)
{
current.Right = InnerInsert(n, current.Right);
}

return current;
}

private void InnerGetElements(Node current, List<int> elements)
{
if (current == null)
{
return;
}

InnerGetElements(current.Left, elements);
if ((current.Left == null && current.Right != null) || (current.Right == null && current.Left != null))
{
elements.Add(current.Data);
}

InnerGetElements(current.Right, elements);
}
}
}
18 changes: 18 additions & 0 deletions CourseApp/Module5/Task_1/Node.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace CourseApp.Module5.Task_1
{
public class Node
{
public Node (int input)
{
Left = null;
Right = null;
Data = input;
}

public Node Left { get; set; }

public Node Right { get; set; }

public int Data { get; set; }
}
}
34 changes: 34 additions & 0 deletions CourseApp/Module5/Task_1/OneChildNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;

namespace CourseApp.Module5.Task_1
{
public class OneChildNode
{
public static void FindNode()
{
StreamReader reader = new StreamReader("input.txt");
int[] numbers = reader.ReadLine().Trim().Split(' ').Select(n => Convert.ToInt32(n)).ToArray();
reader.Close();
BinaryTree tree = new BinaryTree();
foreach (var item in numbers)
{
if (item != 0)
{
tree.Insert(item);
}
}

List<int> ans = tree.GetElements();
StreamWriter output = new StreamWriter("output.txt");
foreach (var item in ans)
{
output.WriteLine(item);
}

output.Close();
}
}
}
91 changes: 91 additions & 0 deletions CourseApp/Module5/Task_2/BinaryTree.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using System;
using System.Collections.Generic;

namespace CourseApp.Module5.Task_2
{
public class BinaryTree
{
private Node root = null;

private int size = 0;

private bool trigger;

public void Insert (int n)
{
root = InnerInsert(n, root, null, 0);
}

public bool IsBalanced()
{
trigger = true;
InnerCheckBalanced(root);
return trigger;
}

private void InnerCheckBalanced(Node current)
{
if (current == null || !trigger)
{
return;
}

InnerCheckBalanced(current.Left);
if (Math.Abs(current.Height_left - current.Height_right) > 1)
{
trigger = false;
}

InnerCheckBalanced(current.Right);
}

private Node InnerInsert(int n, Node current, Node previous, int depth)
{
if (current == null)
{
size++;
Node temp = new Node(n);
temp.Previous = previous;
temp.Height_left = 0;
temp.Height_right = 0;
Node buffer1 = temp;
int i = depth - 1;
while (buffer1 != null)
{
if (buffer1.Previous != null)
{
if (buffer1.Data < buffer1.Previous.Data)
{
if (buffer1.Previous.Height_left < depth - i)
{
buffer1.Previous.Height_left = depth - i;
}
}
else
{
if (buffer1.Previous.Height_right < depth - i)
{
buffer1.Previous.Height_right = depth - i;
}
}
}

buffer1 = buffer1.Previous;
i--;
}

return temp;
}
else if (current.Data > n)
{
current.Left = InnerInsert(n, current.Left, current, depth + 1);
}
else if (current.Data < n)
{
current.Right = InnerInsert(n, current.Right, current, depth + 1);
}

return current;
}
}
}
29 changes: 29 additions & 0 deletions CourseApp/Module5/Task_2/CheckBalancedTree.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;

namespace CourseApp.Module5.Task_2
{
public class CheckBalancedTree
{
public static void CheckTree()
{
StreamReader reader = new StreamReader("input.txt");
int[] numbers = reader.ReadLine().Trim().Split(' ').Select(n => Convert.ToInt32(n)).ToArray();
reader.Close();
BinaryTree tree = new BinaryTree();
foreach (var item in numbers)
{
if (item != 0)
{
tree.Insert(item);
}
}

StreamWriter output = new StreamWriter("output.txt");
output.WriteLine(tree.IsBalanced() ? "YES" : "NO");
output.Close();
}
}
}
24 changes: 24 additions & 0 deletions CourseApp/Module5/Task_2/Node.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace CourseApp.Module5.Task_2
{
public class Node
{
public Node (int input)
{
Left = null;
Right = null;
Data = input;
}

public Node Previous { get; set; }

public Node Left { get; set; }

public Node Right { get; set; }

public int Data { get; set; }

public int Height_left { get; set; }

public int Height_right { get; set; }
}
}
31 changes: 31 additions & 0 deletions CourseApp/Module5/Task_3/SegmentGCD.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;

namespace CourseApp.Module5.Task_3
{
public class SegmentGCD
{
public static void FindGCD()
{
StreamReader reader = new StreamReader("input.txt");
int size = int.Parse(reader.ReadLine());
int[] numbers = reader.ReadLine().Trim().Split(' ').Select(n => Convert.ToInt32(n)).ToArray();
int query_count = int.Parse(reader.ReadLine());
SegmentTree tree = new SegmentTree(size);
tree.Build(numbers);
List<int> res = new List<int>();
for (int i = 0; i < query_count; i++)
{
int[] borders = reader.ReadLine().Trim().Split(' ').Select(n => Convert.ToInt32(n)).ToArray();
res.Add(tree.GetGCD(borders[0], borders[1]));
}

reader.Close();
StreamWriter output = new StreamWriter("output.txt");
output.WriteLine(string.Join(" ", res));
output.Close();
}
}
}
Loading