From 666961b9452cc92017f44b5dea3baf39e1a0b3d4 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sat, 5 Mar 2022 19:16:49 +0300 Subject: [PATCH 01/10] writing basic functions and tests --- Homework2/Bor/Bor.sln | 31 +++++ Homework2/Bor/Bor/Bor.cs | 179 +++++++++++++++++++++++++++ Homework2/Bor/Bor/Bor.csproj | 10 ++ Homework2/Bor/BorTest/BorTest.cs | 60 +++++++++ Homework2/Bor/BorTest/BorTest.csproj | 21 ++++ 5 files changed, 301 insertions(+) create mode 100644 Homework2/Bor/Bor.sln create mode 100644 Homework2/Bor/Bor/Bor.cs create mode 100644 Homework2/Bor/Bor/Bor.csproj create mode 100644 Homework2/Bor/BorTest/BorTest.cs create mode 100644 Homework2/Bor/BorTest/BorTest.csproj diff --git a/Homework2/Bor/Bor.sln b/Homework2/Bor/Bor.sln new file mode 100644 index 0000000..66a4364 --- /dev/null +++ b/Homework2/Bor/Bor.sln @@ -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 diff --git a/Homework2/Bor/Bor/Bor.cs b/Homework2/Bor/Bor/Bor.cs new file mode 100644 index 0000000..a609b51 --- /dev/null +++ b/Homework2/Bor/Bor/Bor.cs @@ -0,0 +1,179 @@ +using System; + +namespace String; + +/// +/// A class representing the bor data structure +/// The bor can store Latin alphabet characters, numbers +/// +public class Bor +{ + /// + /// // A class representing the bor data structure + /// + private class Node + { + /// + /// Array of vertices to move from one vertex to another + /// The bor can store Latin alphabet characters, numbers + /// + public Node?[] next = new Node['z']; + + // A field for storing information about whether a character is the end of a string + public bool isTerminal { get; set; } + + // A field for storing information about the number of strings containing a certain prefix + public int numberOfLinesContainingThePrefix { get; set; } + } + + private Node root = new Node(); + + // Bor size + private int size; + + /// + /// Function for adding a string + /// + /// Element to add + /// Was there an element string before calling Add + public bool Add(string element) + { + Node? node = root; + int counter = 0; + for(int i = 0; i < element.Length; i++) + { + if(element[i] > 'z') + { + throw new Exception("The bor can store Latin alphabet characters, numbers"); ; + } + if (node != null && node.next[element[i]] == null) + { + node.next[element[i]] = new Node(); + size++; + } + else + { + counter++; + } + if (node != null) + { + node = node.next[element[i]]; + if(node != null) + { + node.numberOfLinesContainingThePrefix++; + } + } + } + if (counter == element.Length && node != null && node.isTerminal) + { + Node? newNode = root; + for (int i = 0; i < element.Length; i++) + { + newNode = newNode?.next[element[i]]; + if(newNode != null) + { + newNode.numberOfLinesContainingThePrefix--; + } + } + return false; + } + if (node != null) + { + node.isTerminal = true; + } + return true; + } + + /// + /// Is the string contained in the Bor + /// + /// Element to search + /// True if there is such a string. False if there is no such string + public bool Contains(string element) + { + Node? node = root; + for (int i = 0; i < element.Length; i++) + { + node = node.next[element[i]]; + if (node == null) + { + return false; + } + } + return node.isTerminal; + } + + /// + /// Function for finding the number of strings starting with a prefix + /// Количество строк содеражащих префикс + /// The number of strings starting with the prefix + public int HowManyStartWithPrefix(string prefix) + { + Node? node = root; + for (int i = 0; i < prefix.Length;i++) + { + if (node?.next[prefix[i]] != null) + { + node = node.next[prefix[i]]; + } + else + { + return 0; + } + } + return node == null ? 0 : node.numberOfLinesContainingThePrefix; + } + + /// + /// Function for deleting string from a Bor + /// + /// Element to delete + /// as there an element string before calling Remove + public bool Remove(string element) + { + if(!this.Contains(element)) + { + return false; + } + Node? node = root; + Node? nodeWithMaxPrefix = root; + for (int i = 0; i < element.Length; i++) + { + node = node?.next[element[i]]; + if(node != null) + { + node.numberOfLinesContainingThePrefix--; + } + if (i == element.Length - 1) + { + nodeWithMaxPrefix.isTerminal = false; + } + } + node = root; + for (int i = 0; i < element.Length; i++) + { + nodeWithMaxPrefix = node; + node = node?.next[element[i]]; + if (nodeWithMaxPrefix?.next[element[i]]?.numberOfLinesContainingThePrefix == 0) + { + nodeWithMaxPrefix.next[element[i]] = null; + } + + } + return true; + } + + /// + /// Function for returning the number of elements + /// + /// Number of elements contained in the bor + public int Size() + { + return this.size; + } + + static void Main(string[] args) + { + return; + } +} diff --git a/Homework2/Bor/Bor/Bor.csproj b/Homework2/Bor/Bor/Bor.csproj new file mode 100644 index 0000000..74abf5c --- /dev/null +++ b/Homework2/Bor/Bor/Bor.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/Homework2/Bor/BorTest/BorTest.cs b/Homework2/Bor/BorTest/BorTest.cs new file mode 100644 index 0000000..5c28419 --- /dev/null +++ b/Homework2/Bor/BorTest/BorTest.cs @@ -0,0 +1,60 @@ +using NUnit.Framework; + +using String; + +using System; + +namespace BorTest +{ + public class Tests + { + Bor? bor; + [SetUp] + public void Setup() + { + bor = new Bor(); + } + + [Test] + public void DeleteLineFromEmptyBor() + { + Assert.IsFalse(bor?.Remove("hello")); + } + + [Test] + public void AddExistingString() + { + Assert.IsTrue(bor?.Add("hello")); + Assert.IsFalse(bor?.Add("hello")); + } + + [Test] + public void FindNonExistentString() + { + Assert.IsFalse(bor?.Contains("hello")); + } + + [Test] + public void FindStringAfterAdd() + { + Assert.IsTrue(bor?.Add("hello")); + Assert.IsTrue(bor?.Contains("hello")); + } + + [Test] + public void RemoveStringAfterRemove() + { + Assert.IsTrue(bor?.Add("hello")); + Assert.IsTrue(bor?.Remove("hello")); + Assert.IsFalse(bor?.Remove("hello")); + } + + [Test] + public void FindStringAfterRemove() + { + Assert.IsTrue(bor?.Add("hello")); + Assert.IsTrue(bor?.Remove("hello")); + Assert.IsFalse(bor?.Contains("hello")); + } + } +} \ No newline at end of file diff --git a/Homework2/Bor/BorTest/BorTest.csproj b/Homework2/Bor/BorTest/BorTest.csproj new file mode 100644 index 0000000..534cb0f --- /dev/null +++ b/Homework2/Bor/BorTest/BorTest.csproj @@ -0,0 +1,21 @@ + + + + net6.0 + enable + + false + + + + + + + + + + + + + + From 9af2edf5c013072fdcc6c3df72ab9d6ed0766fc0 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sat, 30 Apr 2022 02:46:04 +0300 Subject: [PATCH 02/10] The Remove and HowManyStartsWithPrefix functions have been changed --- Homework2/Bor/Bor/Bor.cs | 180 +++++++++++++++---------------- Homework2/Bor/Bor/Bor.csproj | 4 +- Homework2/Bor/BorTest/BorTest.cs | 145 +++++++++++++++---------- 3 files changed, 178 insertions(+), 151 deletions(-) diff --git a/Homework2/Bor/Bor/Bor.cs b/Homework2/Bor/Bor/Bor.cs index a609b51..dbf990d 100644 --- a/Homework2/Bor/Bor/Bor.cs +++ b/Homework2/Bor/Bor/Bor.cs @@ -1,10 +1,7 @@ -using System; - -namespace String; +namespace Bor; /// /// A class representing the bor data structure -/// The bor can store Latin alphabet characters, numbers /// public class Bor { @@ -13,23 +10,18 @@ public class Bor /// private class Node { - /// - /// Array of vertices to move from one vertex to another - /// The bor can store Latin alphabet characters, numbers - /// - public Node?[] next = new Node['z']; - - // A field for storing information about whether a character is the end of a string - public bool isTerminal { get; set; } + // Dictionary for storing characters for each node + public Dictionary Nodes = new(); - // A field for storing information about the number of strings containing a certain prefix - public int numberOfLinesContainingThePrefix { get; set; } + // Node property - whether it is the end of a string + public bool IsTerminal { get; set; } } - private Node root = new Node(); + // Bor root + private readonly Node root = new(); // Bor size - private int size; + public int Size { get; private set; } /// /// Function for adding a string @@ -38,49 +30,31 @@ private class Node /// Was there an element string before calling Add public bool Add(string element) { - Node? node = root; - int counter = 0; - for(int i = 0; i < element.Length; i++) + if (Contains(element)) { - if(element[i] > 'z') - { - throw new Exception("The bor can store Latin alphabet characters, numbers"); ; - } - if (node != null && node.next[element[i]] == null) - { - node.next[element[i]] = new Node(); - size++; - } - else - { - counter++; - } - if (node != null) - { - node = node.next[element[i]]; - if(node != null) - { - node.numberOfLinesContainingThePrefix++; - } - } + return false; } - if (counter == element.Length && node != null && node.isTerminal) + + Node node = root; + for (int i = 0; i < element.Length; i++) { - Node? newNode = root; - for (int i = 0; i < element.Length; i++) + if (node != null && !node.Nodes.ContainsKey(element[i])) { - newNode = newNode?.next[element[i]]; - if(newNode != null) - { - newNode.numberOfLinesContainingThePrefix--; - } + node.Nodes.Add(element[i], new Node()); + Size++; + } + + if (node != null && node.Nodes.ContainsKey(element[i])) + { + node = node.Nodes[element[i]]; } - return false; } + if (node != null) { - node.isTerminal = true; + node.IsTerminal = true; } + return true; } @@ -94,34 +68,49 @@ public bool Contains(string element) Node? node = root; for (int i = 0; i < element.Length; i++) { - node = node.next[element[i]]; - if (node == null) + if (node.Nodes.TryGetValue(element[i], out node)) { - return false; + continue; } + + return false; } - return node.isTerminal; + + return node.IsTerminal; } /// /// Function for finding the number of strings starting with a prefix - /// Количество строк содеражащих префикс + /// /// The number of strings starting with the prefix public int HowManyStartWithPrefix(string prefix) { Node? node = root; - for (int i = 0; i < prefix.Length;i++) + for (int i = 0; i < prefix.Length; i++) { - if (node?.next[prefix[i]] != null) - { - node = node.next[prefix[i]]; - } - else + if (node != null) { - return 0; + if (node.Nodes.TryGetValue(prefix[i], out node)) + { + continue; + } } + + return 0; + } + + if (node == null) + { + return 0; } - return node == null ? 0 : node.numberOfLinesContainingThePrefix; + + return node.IsTerminal ? 1 + node.Nodes.Count : node.Nodes.Count; + } + + // Function for clearing dictionaries + static private void ClearDictionaryAndNode(Node node) + { + node.Nodes.Clear(); } /// @@ -131,49 +120,50 @@ public int HowManyStartWithPrefix(string prefix) /// as there an element string before calling Remove public bool Remove(string element) { - if(!this.Contains(element)) + if (!Contains(element)) { return false; } - Node? node = root; - Node? nodeWithMaxPrefix = root; + + int index = 0; + Node node = root; + Node? anotherNode = null; for (int i = 0; i < element.Length; i++) { - node = node?.next[element[i]]; - if(node != null) - { - node.numberOfLinesContainingThePrefix--; - } - if (i == element.Length - 1) + if (node != null) { - nodeWithMaxPrefix.isTerminal = false; + 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; + } } - } - node = root; - for (int i = 0; i < element.Length; i++) - { - nodeWithMaxPrefix = node; - node = node?.next[element[i]]; - if (nodeWithMaxPrefix?.next[element[i]]?.numberOfLinesContainingThePrefix == 0) + + if (node != null && node.IsTerminal) { - nodeWithMaxPrefix.next[element[i]] = null; + index = i; + anotherNode = node; } } return true; } - - /// - /// Function for returning the number of elements - /// - /// Number of elements contained in the bor - public int Size() - { - return this.size; - } - - static void Main(string[] args) - { - return; - } } diff --git a/Homework2/Bor/Bor/Bor.csproj b/Homework2/Bor/Bor/Bor.csproj index 74abf5c..644c780 100644 --- a/Homework2/Bor/Bor/Bor.csproj +++ b/Homework2/Bor/Bor/Bor.csproj @@ -1,7 +1,7 @@ - + - Exe + Library net6.0 enable enable diff --git a/Homework2/Bor/BorTest/BorTest.cs b/Homework2/Bor/BorTest/BorTest.cs index 5c28419..a8e379f 100644 --- a/Homework2/Bor/BorTest/BorTest.cs +++ b/Homework2/Bor/BorTest/BorTest.cs @@ -1,60 +1,97 @@ +namespace BorTest; + using NUnit.Framework; +using Bor; -using String; +public class BorTest +{ + Bor bor = new(); -using System; + [SetUp] + public void Setup() + { + bor = new(); + } -namespace BorTest -{ - public class Tests - { - Bor? bor; - [SetUp] - public void Setup() - { - bor = new Bor(); - } - - [Test] - public void DeleteLineFromEmptyBor() - { - Assert.IsFalse(bor?.Remove("hello")); - } - - [Test] - public void AddExistingString() - { - Assert.IsTrue(bor?.Add("hello")); - Assert.IsFalse(bor?.Add("hello")); - } - - [Test] - public void FindNonExistentString() - { - Assert.IsFalse(bor?.Contains("hello")); - } - - [Test] - public void FindStringAfterAdd() - { - Assert.IsTrue(bor?.Add("hello")); - Assert.IsTrue(bor?.Contains("hello")); - } - - [Test] - public void RemoveStringAfterRemove() - { - Assert.IsTrue(bor?.Add("hello")); - Assert.IsTrue(bor?.Remove("hello")); - Assert.IsFalse(bor?.Remove("hello")); - } - - [Test] - public void FindStringAfterRemove() - { - Assert.IsTrue(bor?.Add("hello")); - Assert.IsTrue(bor?.Remove("hello")); - Assert.IsFalse(bor?.Contains("hello")); - } + [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")); } } \ No newline at end of file From 480d6ba85ecd93867ea0ae9bf27433ab03658432 Mon Sep 17 00:00:00 2001 From: Roman Date: Thu, 2 Jun 2022 00:31:51 +0300 Subject: [PATCH 03/10] add yml --- .github/workflows/Bor.yml | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 .github/workflows/Bor.yml diff --git a/.github/workflows/Bor.yml b/.github/workflows/Bor.yml new file mode 100644 index 0000000..94ac0b1 --- /dev/null +++ b/.github/workflows/Bor.yml @@ -0,0 +1,38 @@ +name: Build + +on: [push] + +jobs: + build-Windows: + + runs-on: windows-latest + + steps: + - uses: actions/checkout@v2 + - name: Build + uses: actions/setup-dotnet@v1 + with: + dotnet-version: '6.x' + - name: Restore + run: $slnList = Get-ChildItem $foo.FullName -Recurse -Filter '*.sln'; foreach ($file in $slnList) {nuget restore $file.FullName} + - name: Build + run: $slnList = Get-ChildItem $foo.FullName -Recurse -Filter '*.sln'; foreach ($file in $slnList) {dotnet build $file.FullName} + - name: Test + run: $slnList = Get-ChildItem $foo.FullName -Recurse -Filter '*.sln'; foreach ($file in $slnList) {dotnet test $file.FullName} + + build-Ubuntu: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - name: Build + uses: actions/setup-dotnet@v1 + with: + dotnet-version: '6.x' + - name: Restore + run: for f in $(find . -name "*.sln"); do dotnet restore $f; done + - name: Build + run: for f in $(find . -name "*.sln"); do dotnet build $f; done + - name: Test + run: for f in $(find . -name "*.sln"); do dotnet test $f; done \ No newline at end of file From df383e72eab9dbe205faa62ae84767ad1250ea5a Mon Sep 17 00:00:00 2001 From: Roman Date: Thu, 2 Jun 2022 01:08:27 +0300 Subject: [PATCH 04/10] Revert "add yml" This reverts commit 480d6ba85ecd93867ea0ae9bf27433ab03658432. --- .github/workflows/{Bor.yml => dotnet.txt} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename .github/workflows/{Bor.yml => dotnet.txt} (100%) diff --git a/.github/workflows/Bor.yml b/.github/workflows/dotnet.txt similarity index 100% rename from .github/workflows/Bor.yml rename to .github/workflows/dotnet.txt From c159a093df2f3ed65bfa50e7f5e15bb878f57ef0 Mon Sep 17 00:00:00 2001 From: Roman Date: Thu, 2 Jun 2022 01:11:34 +0300 Subject: [PATCH 05/10] Revert "add yml" This reverts commit 480d6ba85ecd93867ea0ae9bf27433ab03658432. # Conflicts: # .github/workflows/dotnet.txt --- .github/workflows/{dotnet.txt => dotnet.yml} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename .github/workflows/{dotnet.txt => dotnet.yml} (100%) diff --git a/.github/workflows/dotnet.txt b/.github/workflows/dotnet.yml similarity index 100% rename from .github/workflows/dotnet.txt rename to .github/workflows/dotnet.yml From b7fc6d293288d43edb69d7ebd9fbdebfaf0faa28 Mon Sep 17 00:00:00 2001 From: Roman Date: Thu, 2 Jun 2022 02:13:24 +0300 Subject: [PATCH 06/10] formatted --- Homework2/Bor/Bor/{Bor.cs => Trie.cs} | 41 ++++++----- Homework2/Bor/Bor/Trie.csproj | 10 +++ Homework2/Bor/BorTest/BorTest.cs | 97 --------------------------- Homework2/Bor/BorTest/TrieTest.cs | 97 +++++++++++++++++++++++++++ Homework2/Bor/BorTest/TrieTest.csproj | 21 ++++++ 5 files changed, 154 insertions(+), 112 deletions(-) rename Homework2/Bor/Bor/{Bor.cs => Trie.cs} (82%) create mode 100644 Homework2/Bor/Bor/Trie.csproj delete mode 100644 Homework2/Bor/BorTest/BorTest.cs create mode 100644 Homework2/Bor/BorTest/TrieTest.cs create mode 100644 Homework2/Bor/BorTest/TrieTest.csproj diff --git a/Homework2/Bor/Bor/Bor.cs b/Homework2/Bor/Bor/Trie.cs similarity index 82% rename from Homework2/Bor/Bor/Bor.cs rename to Homework2/Bor/Bor/Trie.cs index dbf990d..7d37218 100644 --- a/Homework2/Bor/Bor/Bor.cs +++ b/Homework2/Bor/Bor/Trie.cs @@ -1,26 +1,34 @@ -namespace Bor; +namespace Trie; /// -/// A class representing the bor data structure +/// A class representing the Trie /// -public class Bor +public class Trie { /// - /// // A class representing the bor data structure + /// A class representing Trie node /// private class Node { - // Dictionary for storing characters for each node + /// + /// Dictionary for storing characters for each node + /// public Dictionary Nodes = new(); - // Node property - whether it is the end of a string + /// + /// Node property - whether it is the end of a string + /// public bool IsTerminal { get; set; } } - // Bor root + /// + /// Trie root + /// private readonly Node root = new(); - // Bor size + /// + /// Trie size + /// public int Size { get; private set; } /// @@ -59,7 +67,7 @@ public bool Add(string element) } /// - /// Is the string contained in the Bor + /// Is the string contained in the Trie /// /// Element to search /// True if there is such a string. False if there is no such string @@ -107,14 +115,16 @@ public int HowManyStartWithPrefix(string prefix) return node.IsTerminal ? 1 + node.Nodes.Count : node.Nodes.Count; } - // Function for clearing dictionaries + /// + /// Function for clearing dictionaries + /// static private void ClearDictionaryAndNode(Node node) { node.Nodes.Clear(); } /// - /// Function for deleting string from a Bor + /// Function for deleting string from a Trie /// /// Element to delete /// as there an element string before calling Remove @@ -127,7 +137,7 @@ public bool Remove(string element) int index = 0; Node node = root; - Node? anotherNode = null; + Node? parent = null; for (int i = 0; i < element.Length; i++) { if (node != null) @@ -136,9 +146,9 @@ public bool Remove(string element) if (node.Nodes.Count == 0 && i == element.Length - 1) { - if (anotherNode != null) + if (parent != null) { - ClearDictionaryAndNode(anotherNode); + ClearDictionaryAndNode(parent); Size -= i - index; return true; } @@ -160,10 +170,11 @@ public bool Remove(string element) if (node != null && node.IsTerminal) { index = i; - anotherNode = node; + parent = node; } } + return true; } } diff --git a/Homework2/Bor/Bor/Trie.csproj b/Homework2/Bor/Bor/Trie.csproj new file mode 100644 index 0000000..644c780 --- /dev/null +++ b/Homework2/Bor/Bor/Trie.csproj @@ -0,0 +1,10 @@ + + + + Library + net6.0 + enable + enable + + + diff --git a/Homework2/Bor/BorTest/BorTest.cs b/Homework2/Bor/BorTest/BorTest.cs deleted file mode 100644 index a8e379f..0000000 --- a/Homework2/Bor/BorTest/BorTest.cs +++ /dev/null @@ -1,97 +0,0 @@ -namespace BorTest; - -using NUnit.Framework; -using Bor; - -public class BorTest -{ - 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")); - } -} \ No newline at end of file diff --git a/Homework2/Bor/BorTest/TrieTest.cs b/Homework2/Bor/BorTest/TrieTest.cs new file mode 100644 index 0000000..c27b471 --- /dev/null +++ b/Homework2/Bor/BorTest/TrieTest.cs @@ -0,0 +1,97 @@ +namespace TrieTest; + +using NUnit.Framework; +using Trie; + +public class TrieTest +{ + private Trie trie = new(); + + [SetUp] + public void Setup() + { + trie = new(); + } + + [Test] + public void ShouldExpectedFalseWhenRemoveFromEmptyBor() + { + Assert.IsFalse(trie.Remove("hello")); + } + + [Test] + public void ShouldExpectedFalseWhenAddExistingString() + { + Assert.IsTrue(trie.Add("hello")); + Assert.IsFalse(trie.Add("hello")); + } + + [Test] + public void ShouldExpectedFalseWhenContainsForNonExistingString() + { + Assert.IsFalse(trie.Contains("hello")); + } + + [Test] + public void ShouldExpectedTrueWhenContainsForExistingString() + { + Assert.IsTrue(trie.Add("hello")); + Assert.IsTrue(trie.Contains("hello")); + } + + [Test] + public void ShouldExpectedFalseWhenRemoveForRemovedString() + { + Assert.IsTrue(trie.Add("hello")); + Assert.IsTrue(trie.Remove("hello")); + Assert.IsFalse(trie.Remove("hello")); + } + + [Test] + public void ShouldExpectedFalseWhenContainsForRemovedString() + { + Assert.IsTrue(trie.Add("hello")); + Assert.IsTrue(trie.Remove("hello")); + Assert.IsFalse(trie.Contains("hello")); + } + + [Test] + public void ShouldExpected5WhenSizeForBorContains5Node() + { + Assert.IsTrue(trie.Add("hello")); + Assert.AreEqual(5, trie.Size); + } + + [Test] + public void ShouldBorSizeNotChangeWhenAddExistingSubstring() + { + Assert.IsTrue(trie.Add("hello")); + int size = trie.Size; + Assert.IsTrue(trie.Add("hell")); + Assert.AreEqual(size, trie.Size); + } + + [Test] + public void ShouldBorSizeEqual8WhenAddStringLength3WithNonExistingFirstSymbolForBorContains5Node() + { + Assert.IsTrue(trie.Add("hello")); + Assert.IsTrue(trie.Add("bye")); + Assert.AreEqual(8, trie.Size); + } + + [Test] + public void ShouldExpected2WhenHowManyStartWithPrefixForBorContains2StringWhichStartWithSamePrefix() + { + Assert.IsTrue(trie.Add("hello")); + Assert.IsTrue(trie.Add("hel")); + Assert.AreEqual(2, trie.HowManyStartWithPrefix("hel")); + } + + [Test] + public void ShouldExpected0WhenHowManyStartWithPrefixForNonExistingPrefix() + { + Assert.IsTrue(trie.Add("hello")); + Assert.IsTrue(trie.Add("bye")); + Assert.AreEqual(0, trie.HowManyStartWithPrefix("leee")); + } +} \ No newline at end of file diff --git a/Homework2/Bor/BorTest/TrieTest.csproj b/Homework2/Bor/BorTest/TrieTest.csproj new file mode 100644 index 0000000..e18d8ea --- /dev/null +++ b/Homework2/Bor/BorTest/TrieTest.csproj @@ -0,0 +1,21 @@ + + + + net6.0 + enable + + false + + + + + + + + + + + + + + From fe7ace3c0307b732feeaf1334288a3a0a44b23e8 Mon Sep 17 00:00:00 2001 From: Roman Date: Thu, 2 Jun 2022 02:17:26 +0300 Subject: [PATCH 07/10] Revert "formatted" This reverts commit b7fc6d293288d43edb69d7ebd9fbdebfaf0faa28. --- Homework2/Bor/Bor/{Trie.cs => Bor.cs} | 41 +++++------ Homework2/Bor/Bor/Trie.csproj | 10 --- Homework2/Bor/BorTest/BorTest.cs | 97 +++++++++++++++++++++++++++ Homework2/Bor/BorTest/TrieTest.cs | 97 --------------------------- Homework2/Bor/BorTest/TrieTest.csproj | 21 ------ 5 files changed, 112 insertions(+), 154 deletions(-) rename Homework2/Bor/Bor/{Trie.cs => Bor.cs} (82%) delete mode 100644 Homework2/Bor/Bor/Trie.csproj create mode 100644 Homework2/Bor/BorTest/BorTest.cs delete mode 100644 Homework2/Bor/BorTest/TrieTest.cs delete mode 100644 Homework2/Bor/BorTest/TrieTest.csproj diff --git a/Homework2/Bor/Bor/Trie.cs b/Homework2/Bor/Bor/Bor.cs similarity index 82% rename from Homework2/Bor/Bor/Trie.cs rename to Homework2/Bor/Bor/Bor.cs index 7d37218..dbf990d 100644 --- a/Homework2/Bor/Bor/Trie.cs +++ b/Homework2/Bor/Bor/Bor.cs @@ -1,34 +1,26 @@ -namespace Trie; +namespace Bor; /// -/// A class representing the Trie +/// A class representing the bor data structure /// -public class Trie +public class Bor { /// - /// A class representing Trie node + /// // A class representing the bor data structure /// private class Node { - /// - /// Dictionary for storing characters for each node - /// + // Dictionary for storing characters for each node public Dictionary Nodes = new(); - /// - /// Node property - whether it is the end of a string - /// + // Node property - whether it is the end of a string public bool IsTerminal { get; set; } } - /// - /// Trie root - /// + // Bor root private readonly Node root = new(); - /// - /// Trie size - /// + // Bor size public int Size { get; private set; } /// @@ -67,7 +59,7 @@ public bool Add(string element) } /// - /// Is the string contained in the Trie + /// Is the string contained in the Bor /// /// Element to search /// True if there is such a string. False if there is no such string @@ -115,16 +107,14 @@ public int HowManyStartWithPrefix(string prefix) return node.IsTerminal ? 1 + node.Nodes.Count : node.Nodes.Count; } - /// - /// Function for clearing dictionaries - /// + // Function for clearing dictionaries static private void ClearDictionaryAndNode(Node node) { node.Nodes.Clear(); } /// - /// Function for deleting string from a Trie + /// Function for deleting string from a Bor /// /// Element to delete /// as there an element string before calling Remove @@ -137,7 +127,7 @@ public bool Remove(string element) int index = 0; Node node = root; - Node? parent = null; + Node? anotherNode = null; for (int i = 0; i < element.Length; i++) { if (node != null) @@ -146,9 +136,9 @@ public bool Remove(string element) if (node.Nodes.Count == 0 && i == element.Length - 1) { - if (parent != null) + if (anotherNode != null) { - ClearDictionaryAndNode(parent); + ClearDictionaryAndNode(anotherNode); Size -= i - index; return true; } @@ -170,11 +160,10 @@ public bool Remove(string element) if (node != null && node.IsTerminal) { index = i; - parent = node; + anotherNode = node; } } - return true; } } diff --git a/Homework2/Bor/Bor/Trie.csproj b/Homework2/Bor/Bor/Trie.csproj deleted file mode 100644 index 644c780..0000000 --- a/Homework2/Bor/Bor/Trie.csproj +++ /dev/null @@ -1,10 +0,0 @@ - - - - Library - net6.0 - enable - enable - - - diff --git a/Homework2/Bor/BorTest/BorTest.cs b/Homework2/Bor/BorTest/BorTest.cs new file mode 100644 index 0000000..a8e379f --- /dev/null +++ b/Homework2/Bor/BorTest/BorTest.cs @@ -0,0 +1,97 @@ +namespace BorTest; + +using NUnit.Framework; +using Bor; + +public class BorTest +{ + 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")); + } +} \ No newline at end of file diff --git a/Homework2/Bor/BorTest/TrieTest.cs b/Homework2/Bor/BorTest/TrieTest.cs deleted file mode 100644 index c27b471..0000000 --- a/Homework2/Bor/BorTest/TrieTest.cs +++ /dev/null @@ -1,97 +0,0 @@ -namespace TrieTest; - -using NUnit.Framework; -using Trie; - -public class TrieTest -{ - private Trie trie = new(); - - [SetUp] - public void Setup() - { - trie = new(); - } - - [Test] - public void ShouldExpectedFalseWhenRemoveFromEmptyBor() - { - Assert.IsFalse(trie.Remove("hello")); - } - - [Test] - public void ShouldExpectedFalseWhenAddExistingString() - { - Assert.IsTrue(trie.Add("hello")); - Assert.IsFalse(trie.Add("hello")); - } - - [Test] - public void ShouldExpectedFalseWhenContainsForNonExistingString() - { - Assert.IsFalse(trie.Contains("hello")); - } - - [Test] - public void ShouldExpectedTrueWhenContainsForExistingString() - { - Assert.IsTrue(trie.Add("hello")); - Assert.IsTrue(trie.Contains("hello")); - } - - [Test] - public void ShouldExpectedFalseWhenRemoveForRemovedString() - { - Assert.IsTrue(trie.Add("hello")); - Assert.IsTrue(trie.Remove("hello")); - Assert.IsFalse(trie.Remove("hello")); - } - - [Test] - public void ShouldExpectedFalseWhenContainsForRemovedString() - { - Assert.IsTrue(trie.Add("hello")); - Assert.IsTrue(trie.Remove("hello")); - Assert.IsFalse(trie.Contains("hello")); - } - - [Test] - public void ShouldExpected5WhenSizeForBorContains5Node() - { - Assert.IsTrue(trie.Add("hello")); - Assert.AreEqual(5, trie.Size); - } - - [Test] - public void ShouldBorSizeNotChangeWhenAddExistingSubstring() - { - Assert.IsTrue(trie.Add("hello")); - int size = trie.Size; - Assert.IsTrue(trie.Add("hell")); - Assert.AreEqual(size, trie.Size); - } - - [Test] - public void ShouldBorSizeEqual8WhenAddStringLength3WithNonExistingFirstSymbolForBorContains5Node() - { - Assert.IsTrue(trie.Add("hello")); - Assert.IsTrue(trie.Add("bye")); - Assert.AreEqual(8, trie.Size); - } - - [Test] - public void ShouldExpected2WhenHowManyStartWithPrefixForBorContains2StringWhichStartWithSamePrefix() - { - Assert.IsTrue(trie.Add("hello")); - Assert.IsTrue(trie.Add("hel")); - Assert.AreEqual(2, trie.HowManyStartWithPrefix("hel")); - } - - [Test] - public void ShouldExpected0WhenHowManyStartWithPrefixForNonExistingPrefix() - { - Assert.IsTrue(trie.Add("hello")); - Assert.IsTrue(trie.Add("bye")); - Assert.AreEqual(0, trie.HowManyStartWithPrefix("leee")); - } -} \ No newline at end of file diff --git a/Homework2/Bor/BorTest/TrieTest.csproj b/Homework2/Bor/BorTest/TrieTest.csproj deleted file mode 100644 index e18d8ea..0000000 --- a/Homework2/Bor/BorTest/TrieTest.csproj +++ /dev/null @@ -1,21 +0,0 @@ - - - - net6.0 - enable - - false - - - - - - - - - - - - - - From f294d05e77b9d35cef8dd9ed29fdc201092e43e4 Mon Sep 17 00:00:00 2001 From: Roman Date: Thu, 2 Jun 2022 02:20:46 +0300 Subject: [PATCH 08/10] formatted --- Homework2/Bor/Bor/{Bor.cs => Trie.cs} | 35 ++++++---- Homework2/Bor/BorTest/BorTest.cs | 97 --------------------------- Homework2/Bor/BorTest/TrieTest.cs | 97 +++++++++++++++++++++++++++ 3 files changed, 120 insertions(+), 109 deletions(-) rename Homework2/Bor/Bor/{Bor.cs => Trie.cs} (85%) delete mode 100644 Homework2/Bor/BorTest/BorTest.cs create mode 100644 Homework2/Bor/BorTest/TrieTest.cs diff --git a/Homework2/Bor/Bor/Bor.cs b/Homework2/Bor/Bor/Trie.cs similarity index 85% rename from Homework2/Bor/Bor/Bor.cs rename to Homework2/Bor/Bor/Trie.cs index dbf990d..3fa4731 100644 --- a/Homework2/Bor/Bor/Bor.cs +++ b/Homework2/Bor/Bor/Trie.cs @@ -1,26 +1,34 @@ -namespace Bor; +namespace Trie; /// -/// A class representing the bor data structure +/// A class representing the Trie /// -public class Bor +public class Trie { /// - /// // A class representing the bor data structure + /// A class representing the Trie node /// private class Node { - // Dictionary for storing characters for each node + /// + /// Dictionary for storing characters for each node + /// public Dictionary Nodes = new(); - // Node property - whether it is the end of a string + /// + /// Node property - whether it is the end of a string + /// public bool IsTerminal { get; set; } } - // Bor root + /// + /// Bor root + /// private readonly Node root = new(); - // Bor size + /// + /// Bor size + /// public int Size { get; private set; } /// @@ -59,7 +67,7 @@ public bool Add(string element) } /// - /// Is the string contained in the Bor + /// Is the string contained in the Trie /// /// Element to search /// True if there is such a string. False if there is no such string @@ -107,14 +115,17 @@ public int HowManyStartWithPrefix(string prefix) return node.IsTerminal ? 1 + node.Nodes.Count : node.Nodes.Count; } - // Function for clearing dictionaries + /// + /// Function for clearing dictionaries + /// + /// static private void ClearDictionaryAndNode(Node node) { node.Nodes.Clear(); } /// - /// Function for deleting string from a Bor + /// Function for deleting string from Trie /// /// Element to delete /// as there an element string before calling Remove @@ -162,8 +173,8 @@ public bool Remove(string element) index = i; anotherNode = node; } - } + return true; } } diff --git a/Homework2/Bor/BorTest/BorTest.cs b/Homework2/Bor/BorTest/BorTest.cs deleted file mode 100644 index a8e379f..0000000 --- a/Homework2/Bor/BorTest/BorTest.cs +++ /dev/null @@ -1,97 +0,0 @@ -namespace BorTest; - -using NUnit.Framework; -using Bor; - -public class BorTest -{ - 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")); - } -} \ No newline at end of file diff --git a/Homework2/Bor/BorTest/TrieTest.cs b/Homework2/Bor/BorTest/TrieTest.cs new file mode 100644 index 0000000..c27b471 --- /dev/null +++ b/Homework2/Bor/BorTest/TrieTest.cs @@ -0,0 +1,97 @@ +namespace TrieTest; + +using NUnit.Framework; +using Trie; + +public class TrieTest +{ + private Trie trie = new(); + + [SetUp] + public void Setup() + { + trie = new(); + } + + [Test] + public void ShouldExpectedFalseWhenRemoveFromEmptyBor() + { + Assert.IsFalse(trie.Remove("hello")); + } + + [Test] + public void ShouldExpectedFalseWhenAddExistingString() + { + Assert.IsTrue(trie.Add("hello")); + Assert.IsFalse(trie.Add("hello")); + } + + [Test] + public void ShouldExpectedFalseWhenContainsForNonExistingString() + { + Assert.IsFalse(trie.Contains("hello")); + } + + [Test] + public void ShouldExpectedTrueWhenContainsForExistingString() + { + Assert.IsTrue(trie.Add("hello")); + Assert.IsTrue(trie.Contains("hello")); + } + + [Test] + public void ShouldExpectedFalseWhenRemoveForRemovedString() + { + Assert.IsTrue(trie.Add("hello")); + Assert.IsTrue(trie.Remove("hello")); + Assert.IsFalse(trie.Remove("hello")); + } + + [Test] + public void ShouldExpectedFalseWhenContainsForRemovedString() + { + Assert.IsTrue(trie.Add("hello")); + Assert.IsTrue(trie.Remove("hello")); + Assert.IsFalse(trie.Contains("hello")); + } + + [Test] + public void ShouldExpected5WhenSizeForBorContains5Node() + { + Assert.IsTrue(trie.Add("hello")); + Assert.AreEqual(5, trie.Size); + } + + [Test] + public void ShouldBorSizeNotChangeWhenAddExistingSubstring() + { + Assert.IsTrue(trie.Add("hello")); + int size = trie.Size; + Assert.IsTrue(trie.Add("hell")); + Assert.AreEqual(size, trie.Size); + } + + [Test] + public void ShouldBorSizeEqual8WhenAddStringLength3WithNonExistingFirstSymbolForBorContains5Node() + { + Assert.IsTrue(trie.Add("hello")); + Assert.IsTrue(trie.Add("bye")); + Assert.AreEqual(8, trie.Size); + } + + [Test] + public void ShouldExpected2WhenHowManyStartWithPrefixForBorContains2StringWhichStartWithSamePrefix() + { + Assert.IsTrue(trie.Add("hello")); + Assert.IsTrue(trie.Add("hel")); + Assert.AreEqual(2, trie.HowManyStartWithPrefix("hel")); + } + + [Test] + public void ShouldExpected0WhenHowManyStartWithPrefixForNonExistingPrefix() + { + Assert.IsTrue(trie.Add("hello")); + Assert.IsTrue(trie.Add("bye")); + Assert.AreEqual(0, trie.HowManyStartWithPrefix("leee")); + } +} \ No newline at end of file From 2e1aa0e4ded50d2d4f323de9856fc49315722561 Mon Sep 17 00:00:00 2001 From: Roman Date: Thu, 2 Jun 2022 02:23:02 +0300 Subject: [PATCH 09/10] rename project --- Homework2/Bor/Bor/Trie.cs | 8 ++++---- Homework2/Bor/Bor/{Bor.csproj => Trie.csproj} | 0 Homework2/Bor/BorTest/{BorTest.csproj => TrieTest.csproj} | 0 Homework2/Bor/{Bor.sln => TrieProject.sln} | 0 4 files changed, 4 insertions(+), 4 deletions(-) rename Homework2/Bor/Bor/{Bor.csproj => Trie.csproj} (100%) rename Homework2/Bor/BorTest/{BorTest.csproj => TrieTest.csproj} (100%) rename Homework2/Bor/{Bor.sln => TrieProject.sln} (100%) diff --git a/Homework2/Bor/Bor/Trie.cs b/Homework2/Bor/Bor/Trie.cs index 3fa4731..5bf2180 100644 --- a/Homework2/Bor/Bor/Trie.cs +++ b/Homework2/Bor/Bor/Trie.cs @@ -138,7 +138,7 @@ public bool Remove(string element) int index = 0; Node node = root; - Node? anotherNode = null; + Node? parent = null; for (int i = 0; i < element.Length; i++) { if (node != null) @@ -147,9 +147,9 @@ public bool Remove(string element) if (node.Nodes.Count == 0 && i == element.Length - 1) { - if (anotherNode != null) + if (parent != null) { - ClearDictionaryAndNode(anotherNode); + ClearDictionaryAndNode(parent); Size -= i - index; return true; } @@ -171,7 +171,7 @@ public bool Remove(string element) if (node != null && node.IsTerminal) { index = i; - anotherNode = node; + parent = node; } } diff --git a/Homework2/Bor/Bor/Bor.csproj b/Homework2/Bor/Bor/Trie.csproj similarity index 100% rename from Homework2/Bor/Bor/Bor.csproj rename to Homework2/Bor/Bor/Trie.csproj diff --git a/Homework2/Bor/BorTest/BorTest.csproj b/Homework2/Bor/BorTest/TrieTest.csproj similarity index 100% rename from Homework2/Bor/BorTest/BorTest.csproj rename to Homework2/Bor/BorTest/TrieTest.csproj diff --git a/Homework2/Bor/Bor.sln b/Homework2/Bor/TrieProject.sln similarity index 100% rename from Homework2/Bor/Bor.sln rename to Homework2/Bor/TrieProject.sln From 2d95dc9b8a97e98fc7c4f5ea569a7d22d686b4c6 Mon Sep 17 00:00:00 2001 From: Roman Date: Thu, 2 Jun 2022 02:54:56 +0300 Subject: [PATCH 10/10] rename file --- Homework2/Bor/{TrieProject.sln => Bor.sln} | 0 Homework2/Bor/Bor/Bor.csproj | 10 ++++++++++ .../Bor/BorTest/{TrieTest.csproj => BorTest.csproj} | 0 3 files changed, 10 insertions(+) rename Homework2/Bor/{TrieProject.sln => Bor.sln} (100%) create mode 100644 Homework2/Bor/Bor/Bor.csproj rename Homework2/Bor/BorTest/{TrieTest.csproj => BorTest.csproj} (100%) diff --git a/Homework2/Bor/TrieProject.sln b/Homework2/Bor/Bor.sln similarity index 100% rename from Homework2/Bor/TrieProject.sln rename to Homework2/Bor/Bor.sln diff --git a/Homework2/Bor/Bor/Bor.csproj b/Homework2/Bor/Bor/Bor.csproj new file mode 100644 index 0000000..644c780 --- /dev/null +++ b/Homework2/Bor/Bor/Bor.csproj @@ -0,0 +1,10 @@ + + + + Library + net6.0 + enable + enable + + + diff --git a/Homework2/Bor/BorTest/TrieTest.csproj b/Homework2/Bor/BorTest/BorTest.csproj similarity index 100% rename from Homework2/Bor/BorTest/TrieTest.csproj rename to Homework2/Bor/BorTest/BorTest.csproj