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 NewBorForCI/NewBorForCI.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.4.33403.182
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NewBorForCI", "NewBorForCI\NewBorForCI.csproj", "{9A77CDBE-D0EE-4CED-B5B7-92B3567E6BAE}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestsTrie", "TestsBor\TestsTrie.csproj", "{8ECB64A5-1B15-4C3F-85A6-34DDE66CE741}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{9A77CDBE-D0EE-4CED-B5B7-92B3567E6BAE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9A77CDBE-D0EE-4CED-B5B7-92B3567E6BAE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9A77CDBE-D0EE-4CED-B5B7-92B3567E6BAE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9A77CDBE-D0EE-4CED-B5B7-92B3567E6BAE}.Release|Any CPU.Build.0 = Release|Any CPU
{8ECB64A5-1B15-4C3F-85A6-34DDE66CE741}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8ECB64A5-1B15-4C3F-85A6-34DDE66CE741}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8ECB64A5-1B15-4C3F-85A6-34DDE66CE741}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8ECB64A5-1B15-4C3F-85A6-34DDE66CE741}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {22B84F78-A6D8-4810-95B6-A79AEF403E7F}
EndGlobalSection
EndGlobal
10 changes: 10 additions & 0 deletions NewBorForCI/NewBorForCI/NewBorForCI.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>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
149 changes: 149 additions & 0 deletions NewBorForCI/NewBorForCI/Trie.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
using System.Xml.Linq;

namespace Trie;

// A container for storing strings, in the form of a suspended tree
public class Trie
{
private TrieElement root = new();

// Adding an element
public bool Add(string element)
{
ArgumentNullException.ThrowIfNull(element);
root.NumberOfLinesInTheDictionary++;
var walker = root;
int i = 0;
while (i < element.Length)
{
char number = element[i];
if (!walker.Next.ContainsKey(number))
{
walker.Next.Add(number, new TrieElement());
}
++walker.Next[number].NumberOfLinesInTheDictionary;
walker = walker.Next[number];
i++;
}
if (!walker.IsTerminal)
{
walker.IsTerminal = true;
return true;
}
return false;
}

private bool RemoveHelp(TrieElement walker, string element, int position, ref bool isDeleted)
{
if (position == element.Length)
{
if (walker.IsTerminal)
{
walker.IsTerminal = false;
return true;
}
return false;
}

if (walker.Next.ContainsKey(element[position]))
{
bool isCorrect = RemoveHelp(walker.Next[element[position]], element, position + 1, ref isDeleted);
if (!isCorrect)
{
return false;
}
if (walker.Next[element[position]].NumberOfLinesInTheDictionary == 1)
{
walker.Next.Remove(element[position]);
isDeleted = true;
return true;
}
if (isDeleted)
{
isDeleted = false;
}
--walker.Next[element[position]].NumberOfLinesInTheDictionary;
return true;
}
return false;
}

// Deleting an element in the tree
public bool Remove(string element)
{
if (root == null)
{
throw new InvalidOperationException();
}
if (element == null)
{
throw new ArgumentNullException();
}

if (element == "")
{
root.IsTerminal = false;
return true;
}
var walker = root;
bool isDeleted = false;
if (RemoveHelp(walker, element, 0, ref isDeleted))
{
--root.NumberOfLinesInTheDictionary;
return true;
}
return false;
}

// Counts the number of rows with the same prefix
public int HowManyStartsWithPrefix(string prefix)
{
ArgumentNullException.ThrowIfNull(root);

var walker = root;
int i = 0;
while (i < prefix.Length)
{
if (!walker.Next.ContainsKey(prefix[i]))
{
return 0;
}
walker = walker.Next[prefix[i]];
++i;
}
return walker.NumberOfLinesInTheDictionary;
}

// Checks for the presence of a string
public bool Contains(string element)
{
ArgumentNullException.ThrowIfNull(root);

var walker = root;
int i = 0;
while (i < element.Length)
{
if (!walker.Next.ContainsKey(element[i]))
{
return false;
}
walker = walker.Next[element[i]];
++i;
}
return true;
}

private class TrieElement
{
public Dictionary<char, TrieElement> Next { get; set; }
public bool IsTerminal { get; set; }


public int NumberOfLinesInTheDictionary { get; set; }

public TrieElement()
{
Next = new Dictionary<char, TrieElement>();
}
}
}
74 changes: 74 additions & 0 deletions NewBorForCI/TestsBor/TestsTrie.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
namespace Trie;

public class Tests
{
private Trie? trie;

[SetUp]
public void TestInitialize()
{
trie = new();
Assert.IsNotNull(trie);
}

[Test]
public void TheAddedElementShouldbeFoundInBor()
{
TestInitialize();
trie.Add("end");
Assert.True(trie.Contains("end"));
}

[Test]
public void ByAddingAndRemovingAnElementItShouldNotStayInBor()
{
TestInitialize();
trie.Add("end");
trie.Remove("end");
Assert.False(trie.Contains("end"));
}

[Test]
public void AddTwoStringsWithTheSamePrefixBorTheNumberOfStringsWithThisPrefixIsTwo()
{
TestInitialize();
trie.Add("endProgram");
trie.Add("endFunction");
Assert.That(trie.HowManyStartsWithPrefix("end") == 2);
}

[Test]
public void WhenEnteringDifferentStringsTheNumberOfStringsWithTheSamePrefixNotZeroMustBeOne()
{
TestInitialize();
trie.Add("aaaaa");
trie.Add("bbbbb");
trie.Add("ccccc");
Assert.That(trie.HowManyStartsWithPrefix("a") == 1);
}

[Test]
public void TheAnswerToANoExistentPrefixInTheBorShouldBeOne()
{
TestInitialize();
trie.Add("adadasd");
Assert.That(trie.HowManyStartsWithPrefix("dsdsd") == 0);
}

[Test]
public void AnEmptyPrefixShouldGiveOuAllTheLinesInTheBor()
{
TestInitialize();
trie.Add("adads");
trie.Add("ddsds");
trie.Add("End");
Assert.That(trie.HowManyStartsWithPrefix("") == 3);
}

[Test]
public void EmptyBorShouldGiveZeroStringsInBor()
{
TestInitialize();
Assert.That(trie.HowManyStartsWithPrefix("") == 0);
}
}
23 changes: 23 additions & 0 deletions NewBorForCI/TestsBor/TestsTrie.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

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

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="NUnit.Analyzers" Version="3.3.0" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
</ItemGroup>

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

</Project>
1 change: 1 addition & 0 deletions NewBorForCI/TestsBor/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using NUnit.Framework;