Skip to content

Commit 36f1a91

Browse files
committed
Implement ternary search tries partial
1 parent 66a8b29 commit 36f1a91

File tree

3 files changed

+165
-1
lines changed

3 files changed

+165
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Algorithms.Utils;
4+
5+
namespace Algorithms.DataStructures.SearchTries
6+
{
7+
public class TernarySearchTries<T> : ISearchTries<T>
8+
{
9+
private class Node
10+
{
11+
internal char key;
12+
internal T value;
13+
internal Node left;
14+
internal Node mid;
15+
internal Node right;
16+
}
17+
18+
private Node root;
19+
private int N;
20+
21+
public T this[string key]
22+
{
23+
get
24+
{
25+
Node x = Get(root, key, 0);
26+
if (x != null)
27+
{
28+
return x.value;
29+
}
30+
return default(T);
31+
}
32+
set { root = Put(root, key, value, 0); }
33+
}
34+
35+
private Node Put(Node x, string key, T value, int d)
36+
{
37+
if (x == null)
38+
{
39+
x = new Node
40+
{
41+
key = key[d]
42+
};
43+
}
44+
45+
int cmp = key[d].CompareTo(x.key);
46+
if (cmp < 0)
47+
{
48+
x.left = Put(x.left, key, value, d);
49+
} else if (cmp > 0)
50+
{
51+
x.right = Put(x.right, key, value, d);
52+
}
53+
else
54+
{
55+
if (d >= key.Length-1)
56+
{
57+
if (ObjectUtil.IsNullOrDefault(x.value))
58+
{
59+
N++;
60+
}
61+
x.value = value;
62+
}
63+
else
64+
{
65+
x.mid = Put(x.mid, key, value, d + 1);
66+
}
67+
}
68+
return x;
69+
}
70+
71+
private Node Get(Node x, string key, int d)
72+
{
73+
if (x == null)
74+
{
75+
return null;
76+
}
77+
78+
int cmp = key[d].CompareTo(x.key);
79+
80+
if (cmp < 0) return Get(x.left, key, d);
81+
if (cmp > 0) return Get(x.right, key, d);
82+
if (key.Length - 1 == d)
83+
{
84+
return x;
85+
}
86+
return Get(x.mid, key, d + 1);
87+
}
88+
89+
public int Count => N;
90+
public bool IsEmpty => N == 0;
91+
public void Delete(string key)
92+
{
93+
throw new System.NotImplementedException();
94+
}
95+
96+
public bool ContainsKey(string key)
97+
{
98+
throw new System.NotImplementedException();
99+
}
100+
101+
public IEnumerable<string> Keys { get; }
102+
public IEnumerable<string> KeysWithPrefix(string prefix)
103+
{
104+
throw new System.NotImplementedException();
105+
}
106+
}
107+
}

AlgorithmsUnitTest/DataStrutures/SearchTries/RWayTriesUnitTest.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public RWayTriesUnitTest(ITestOutputHelper console)
1616
}
1717

1818
[Fact]
19-
public void TestHashMap()
19+
public void TestSearchTries()
2020
{
2121
var map = new RWayTries<int>();
2222
Assert.True(map.IsEmpty);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System.Reflection.PortableExecutable;
2+
using Algorithms.DataStructures.HashMap;
3+
using Algorithms.DataStructures.SearchTries;
4+
using Algorithms.DataStructures.TreeMap;
5+
using Xunit;
6+
using Xunit.Abstractions;
7+
8+
namespace AlgorithmsUnitTest.DataStrutures.TreeMap
9+
{
10+
public class TernarySearchTriesUnitTest
11+
{
12+
private ITestOutputHelper console;
13+
public TernarySearchTriesUnitTest(ITestOutputHelper console)
14+
{
15+
this.console = console;
16+
}
17+
18+
[Fact]
19+
public void TestSearchTries()
20+
{
21+
var map = new TernarySearchTries<int>();
22+
Assert.True(map.IsEmpty);
23+
map["one"] = 1;
24+
Assert.Equal(1, map["one"]);
25+
map["two"] = 2;
26+
Assert.Equal(2, map["two"]);
27+
Assert.Equal(2, map.Count);
28+
29+
30+
/*
31+
map.Delete("two");
32+
Assert.Equal(1, map.Count);
33+
Assert.True(map.ContainsKey("one"));
34+
Assert.False(map.ContainsKey("two"));
35+
Assert.False(map.IsEmpty);
36+
37+
38+
foreach(var key in map.Keys)
39+
{
40+
console.WriteLine("{0}", key);
41+
}
42+
43+
map["test"] = 3;
44+
map["te"] = 4;
45+
map["team"] = 4;
46+
map["dream"] = 2;
47+
48+
int count = 0;
49+
foreach (var key in map.KeysWithPrefix("te"))
50+
{
51+
count++;
52+
console.WriteLine("{0}", key);
53+
}
54+
Assert.Equal(3, count);*/
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)