Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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 Homework2/Bor/Bor.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.1.32210.238
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bor", "Bor\Bor.csproj", "{A5948E4C-54A6-49B2-AD93-A2C768CF3B7B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BorTest", "BorTest\BorTest.csproj", "{82DAD3CF-7041-4300-B987-4CF0FD1C3E52}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{A5948E4C-54A6-49B2-AD93-A2C768CF3B7B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A5948E4C-54A6-49B2-AD93-A2C768CF3B7B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A5948E4C-54A6-49B2-AD93-A2C768CF3B7B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A5948E4C-54A6-49B2-AD93-A2C768CF3B7B}.Release|Any CPU.Build.0 = Release|Any CPU
{82DAD3CF-7041-4300-B987-4CF0FD1C3E52}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{82DAD3CF-7041-4300-B987-4CF0FD1C3E52}.Debug|Any CPU.Build.0 = Debug|Any CPU
{82DAD3CF-7041-4300-B987-4CF0FD1C3E52}.Release|Any CPU.ActiveCfg = Release|Any CPU
{82DAD3CF-7041-4300-B987-4CF0FD1C3E52}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {EEBE94E6-045F-4E20-8C45-A352B7E2EEA7}
EndGlobalSection
EndGlobal
169 changes: 169 additions & 0 deletions Homework2/Bor/Bor/Bor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
namespace Bor;

/// <summary>
/// A class representing the bor data structure
/// </summary>
public class Bor
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Он не Bor, он Trie

{
/// <summary>
/// // A class representing the bor data structure
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Не совсем правда, и комментарий в неправильном формате, достаточно ///

/// </summary>
private class Node
{
// Dictionary for storing characters for each node
public Dictionary<char, Node> Nodes = new();

// Node property - whether it is the end of a string
public bool IsTerminal { get; set; }
}

// Bor root
private readonly Node root = new();

// Bor size
public int Size { get; private set; }

/// <summary>
/// Function for adding a string
/// </summary>
/// <param name="element"> Element to add </param>
/// <returns> Was there an element string before calling Add </returns>
public bool Add(string element)
{
if (Contains(element))
{
return false;
}

Node node = root;
for (int i = 0; i < element.Length; i++)
{
if (node != null && !node.Nodes.ContainsKey(element[i]))
{
node.Nodes.Add(element[i], new Node());
Size++;
}

if (node != null && node.Nodes.ContainsKey(element[i]))
{
node = node.Nodes[element[i]];
}
}

if (node != null)
{
node.IsTerminal = true;
}

return true;
}

/// <summary>
/// Is the string contained in the Bor
/// </summary>
/// <param name="element"> Element to search </param>
/// <returns> True if there is such a string. False if there is no such string </returns>
public bool Contains(string element)
{
Node? node = root;
for (int i = 0; i < element.Length; i++)
{
if (node.Nodes.TryGetValue(element[i], out node))
{
continue;
}

return false;
}

return node.IsTerminal;
}

/// <summary>
/// Function for finding the number of strings starting with a prefix
/// </summary>
/// <returns> The number of strings starting with the prefix </returns>
public int HowManyStartWithPrefix(string prefix)
{
Node? node = root;
for (int i = 0; i < prefix.Length; i++)
{
if (node != null)
{
if (node.Nodes.TryGetValue(prefix[i], out node))
{
continue;
}
}

return 0;
}

if (node == null)
{
return 0;
}

return node.IsTerminal ? 1 + node.Nodes.Count : node.Nodes.Count;
}

// Function for clearing dictionaries
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Внезапно комментарий не в стандартном формате

static private void ClearDictionaryAndNode(Node node)
{
node.Nodes.Clear();
}

/// <summary>
/// Function for deleting string from a Bor
/// </summary>
/// <param name="element"> Element to delete </param>
/// <returns> as there an element string before calling Remove </returns>
public bool Remove(string element)
{
if (!Contains(element))
{
return false;
}

int index = 0;
Node node = root;
Node? anotherNode = null;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Я бы назвал его parent или как-то так, потому что вообще непонятна его роль сейчас

for (int i = 0; i < element.Length; i++)
{
if (node != null)
{
node = node.Nodes[element[i]];

if (node.Nodes.Count == 0 && i == element.Length - 1)
{
if (anotherNode != null)
{
ClearDictionaryAndNode(anotherNode);
Size -= i - index;
return true;
}
else
{
ClearDictionaryAndNode(root);
Size = 0;
return true;
}
}

if (node.Nodes.Count != 0 && i == element.Length - 1)
{
node.IsTerminal = false;
return true;
}
}

if (node != null && node.IsTerminal)
{
index = i;
anotherNode = node;
}

}
return true;
}
}
10 changes: 10 additions & 0 deletions Homework2/Bor/Bor/Bor.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
97 changes: 97 additions & 0 deletions Homework2/Bor/BorTest/BorTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
namespace BorTest;

using NUnit.Framework;
using Bor;

public class BorTest
{
Bor bor = new();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggested change
Bor bor = new();
private Bor bor = new();


[SetUp]
public void Setup()
{
bor = new();
}

[Test]
public void ShouldExpectedFalseWhenRemoveFromEmptyBor()
{
Assert.IsFalse(bor.Remove("hello"));
}

[Test]
public void ShouldExpectedFalseWhenAddExistingString()
{
Assert.IsTrue(bor.Add("hello"));
Assert.IsFalse(bor.Add("hello"));
}

[Test]
public void ShouldExpectedFalseWhenContainsForNonExistingString()
{
Assert.IsFalse(bor.Contains("hello"));
}

[Test]
public void ShouldExpectedTrueWhenContainsForExistingString()
{
Assert.IsTrue(bor.Add("hello"));
Assert.IsTrue(bor.Contains("hello"));
}

[Test]
public void ShouldExpectedFalseWhenRemoveForRemovedString()
{
Assert.IsTrue(bor.Add("hello"));
Assert.IsTrue(bor.Remove("hello"));
Assert.IsFalse(bor.Remove("hello"));
}

[Test]
public void ShouldExpectedFalseWhenContainsForRemovedString()
{
Assert.IsTrue(bor.Add("hello"));
Assert.IsTrue(bor.Remove("hello"));
Assert.IsFalse(bor.Contains("hello"));
}

[Test]
public void ShouldExpected5WhenSizeForBorContains5Node()
{
Assert.IsTrue(bor.Add("hello"));
Assert.AreEqual(5, bor.Size);
}

[Test]
public void ShouldBorSizeNotChangeWhenAddExistingSubstring()
{
Assert.IsTrue(bor.Add("hello"));
int size = bor.Size;
Assert.IsTrue(bor.Add("hell"));
Assert.AreEqual(size, bor.Size);
}

[Test]
public void ShouldBorSizeEqual8WhenAddStringLength3WithNonExistingFirstSymbolForBorContains5Node()
{
Assert.IsTrue(bor.Add("hello"));
Assert.IsTrue(bor.Add("bye"));
Assert.AreEqual(8, bor.Size);
}

[Test]
public void ShouldExpected2WhenHowManyStartWithPrefixForBorContains2StringWhichStartWithSamePrefix()
{
Assert.IsTrue(bor.Add("hello"));
Assert.IsTrue(bor.Add("hel"));
Assert.AreEqual(2, bor.HowManyStartWithPrefix("hel"));
}

[Test]
public void ShouldExpected0WhenHowManyStartWithPrefixForNonExistingPrefix()
{
Assert.IsTrue(bor.Add("hello"));
Assert.IsTrue(bor.Add("bye"));
Assert.AreEqual(0, bor.HowManyStartWithPrefix("leee"));
}
}
21 changes: 21 additions & 0 deletions Homework2/Bor/BorTest/BorTest.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="NUnit" Version="3.13.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.0.0" />
<PackageReference Include="coverlet.collector" Version="3.1.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Bor\Bor.csproj" />
</ItemGroup>

</Project>