Skip to content

Commit 0b165f0

Browse files
committed
The Remove and HowManyStartsWithPrefix functions have been changed
1 parent ed05f48 commit 0b165f0

2 files changed

Lines changed: 32 additions & 18 deletions

File tree

Homework2/Bor/Bor/Bor.cs

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public bool Contains(string element)
7272
{
7373
continue;
7474
}
75+
7576
return false;
7677
}
7778

@@ -107,18 +108,9 @@ public int HowManyStartWithPrefix(string prefix)
107108
}
108109

109110
// Function for clearing dictionaries
110-
static private void ClearDictionaryAndNode(string element, int index, Node? node)
111+
static private void ClearDictionaryAndNode(Node node)
111112
{
112-
if (index + 1 < element.Length - 1 && node != null)
113-
{
114-
node = node.Nodes[element[index + 1]];
115-
ClearDictionaryAndNode(element, index + 1, node);
116-
}
117-
118-
if (node != null)
119-
{
120-
node.Nodes.Clear();
121-
}
113+
node.Nodes.Clear();
122114
}
123115

124116
/// <summary>
@@ -134,22 +126,44 @@ public bool Remove(string element)
134126
}
135127

136128
int index = 0;
137-
Node? node = root;
129+
Node node = root;
130+
Node? anotherNode = null;
138131
for (int i = 0; i < element.Length; i++)
139132
{
140133
if (node != null)
141134
{
142135
node = node.Nodes[element[i]];
136+
137+
if (node.Nodes.Count == 0 && i == element.Length - 1)
138+
{
139+
if (anotherNode != null)
140+
{
141+
ClearDictionaryAndNode(anotherNode);
142+
Size -= i - index;
143+
return true;
144+
}
145+
else
146+
{
147+
ClearDictionaryAndNode(root);
148+
Size = 0;
149+
return true;
150+
}
151+
}
152+
153+
if (node.Nodes.Count != 0 && i == element.Length - 1)
154+
{
155+
node.IsTerminal = false;
156+
return true;
157+
}
143158
}
144159

145-
string subString = element[0..(i + 1)];
146-
if (HowManyStartWithPrefix(subString) < 2)
160+
if (node != null && node.IsTerminal)
147161
{
148162
index = i;
149-
break;
163+
anotherNode = node;
150164
}
165+
151166
}
152-
ClearDictionaryAndNode(element, index, node);
153167
return true;
154168
}
155169
}

Homework2/Bor/BorTest/BorTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public void ShouldExpectedFalseWhenContainsForRemovedString()
5959
public void ShouldExpected5WhenSizeForBorContains5Node()
6060
{
6161
Assert.IsTrue(bor.Add("hello"));
62-
Assert.AreEqual(5, bor?.Size);
62+
Assert.AreEqual(5, bor.Size);
6363
}
6464

6565
[Test]
@@ -80,7 +80,7 @@ public void ShouldBorSizeEqual8WhenAddStringLength3WithNonExistingFirstSymbolFor
8080
}
8181

8282
[Test]
83-
public void ShouldExpected2WhenHowManyStartWithPrefixForBorContains2StringWhichStartWithThisPrefix()
83+
public void ShouldExpected2WhenHowManyStartWithPrefixForBorContains2StringWhichStartWithSamePrefix()
8484
{
8585
Assert.IsTrue(bor.Add("hello"));
8686
Assert.IsTrue(bor.Add("hel"));

0 commit comments

Comments
 (0)