Skip to content

Commit 223e74f

Browse files
committed
Implement ternary search delete and containsKey
1 parent 36f1a91 commit 223e74f

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

Algorithms/DataStructures/SearchTries/TernarySearchTries.cs

+16-2
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,28 @@ private Node Get(Node x, string key, int d)
8888

8989
public int Count => N;
9090
public bool IsEmpty => N == 0;
91+
9192
public void Delete(string key)
9293
{
93-
throw new System.NotImplementedException();
94+
Node x = Get(root, key, 0);
95+
if (x != null)
96+
{
97+
if (!ObjectUtil.IsNullOrDefault(x.value))
98+
{
99+
N--;
100+
}
101+
x.value = default(T);
102+
}
94103
}
95104

96105
public bool ContainsKey(string key)
97106
{
98-
throw new System.NotImplementedException();
107+
Node x = Get(root, key, 0);
108+
if(x !=null)
109+
{
110+
return !ObjectUtil.IsNullOrDefault(x.value);
111+
}
112+
return false;
99113
}
100114

101115
public IEnumerable<string> Keys { get; }

AlgorithmsUnitTest/DataStrutures/SearchTries/TernarySearchTriesUnitTest.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@ public void TestSearchTries()
2727
Assert.Equal(2, map.Count);
2828

2929

30-
/*
30+
3131
map.Delete("two");
3232
Assert.Equal(1, map.Count);
3333
Assert.True(map.ContainsKey("one"));
3434
Assert.False(map.ContainsKey("two"));
3535
Assert.False(map.IsEmpty);
3636

37+
/*
3738
3839
foreach(var key in map.Keys)
3940
{

0 commit comments

Comments
 (0)