-
Notifications
You must be signed in to change notification settings - Fork 1
/
TernaryTrie.cs
141 lines (117 loc) · 4.03 KB
/
TernaryTrie.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using System;
namespace AlgorithmsAndDataStructures.DataStructures.Trie
{
public class TernaryTrie
{
private TernaryTrieNode root;
public void Insert(string key)
{
if (string.IsNullOrEmpty(key))
{
throw new ArgumentNullException(nameof(key));
}
root = InsertInternal(key, 0, root);
}
private static TernaryTrieNode InsertInternal(string key, int index, TernaryTrieNode currentNode)
{
var node = currentNode ?? new TernaryTrieNode
{
Value = key[index]
};
if (key.Length == index + 1 && node.Value == key[index])
{
node.IsWord = true;
}
else
{
if (node.Value == key[index])
{
node.EqualToCurrentCharacter = InsertInternal(key, index + 1, node.EqualToCurrentCharacter);
}
else if (node.Value < key[index])
{
node.GreaterThanCurrentCharacter = InsertInternal(key, index, node.GreaterThanCurrentCharacter);
}
else
{
node.LessThanCurrentCharacter = InsertInternal(key, index, node.LessThanCurrentCharacter);
}
}
return node;
}
public bool Search(string key)
{
if (string.IsNullOrEmpty(key))
{
return default;
}
if (root == null)
{
return false;
}
return SearchInternal(key, 0, root);
}
private static bool SearchInternal(string key, int index, TernaryTrieNode currentNode)
{
if (currentNode == null)
{
return false;
}
var isEqual = currentNode.Value == key[index];
var isEndSymbol = index == key.Length - 1;
if (isEqual)
{
if (isEndSymbol)
{
return currentNode.IsWord;
}
return SearchInternal(key, index + 1, currentNode.EqualToCurrentCharacter);
}
var isBigger = currentNode.Value < key[index];
if (isBigger)
{
return SearchInternal(key, index , currentNode.GreaterThanCurrentCharacter);
}
return SearchInternal(key, index, currentNode.LessThanCurrentCharacter);
}
public void Delete(string key)
{
if (string.IsNullOrEmpty(key))
{
return;
}
root = DeleteInternal(key, 0, root);
}
private TernaryTrieNode DeleteInternal(string key, int index, TernaryTrieNode currentNode)
{
if (currentNode == null)
{
return null;
}
if (currentNode.Value == key[index])
{
if (index == key.Length - 1)
{
currentNode.IsWord = false;
return IsEmptyNode(currentNode) ? null : currentNode;
}
currentNode.EqualToCurrentCharacter = DeleteInternal(key, index + 1, currentNode.EqualToCurrentCharacter);
}
else if (currentNode.Value < key[index])
{
currentNode.GreaterThanCurrentCharacter = DeleteInternal(key, index, currentNode.GreaterThanCurrentCharacter);
}
else
{
currentNode.LessThanCurrentCharacter = DeleteInternal(key, index, currentNode.LessThanCurrentCharacter);
}
return currentNode;
}
private static bool IsEmptyNode(TernaryTrieNode currentNode)
{
return currentNode.EqualToCurrentCharacter != null
|| currentNode.GreaterThanCurrentCharacter != null
|| currentNode.LessThanCurrentCharacter != null;
}
}
}