-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrieNode.cs
94 lines (81 loc) · 2.1 KB
/
TrieNode.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RadixTrie
{
/// <summary>
/// TrieNode Class
/// </summary>
public class TrieNode
{
public List<string> GetPrefixMatchValues()
{
if (IsLeaf())
{
if (IsTerminal())
{
return new List<string>() { Value };
}
else
{
return null;
}
}
else
{
var values = new List<string>();
foreach (TrieNode node in Children.Values)
{
values.AddRange(node.GetPrefixMatchValues());
}
if (IsTerminal())
{
values.Add(Value);
}
return values;
}
}
public bool IsSubList(List<int> input, int index, ref bool isMatch)
{
int i = 0;
for (i = 0; i < input.Count; i++)
{
if (input[i] != Key[i + index])
{
return false;
}
}
isMatch = ((i + index) == Key.Count);
return true;
}
public bool IsLeaf()
{
return Children.Count == 0;
}
public bool IsTerminal()
{
return Value != null;
}
public TrieNode()
{
Parent = null;
Children = new Dictionary<List<int>, TrieNode>();
}
public TrieNode(List<int> key)
: this()
{
this.Key = key;
}
public TrieNode(List<int> key, string value)
: this()
{
this.Key = key;
this.Value = value;
}
public List<int> Key { get; private set; }
public string Value { get; private set; }
public TrieNode Parent { get; set; }
public IDictionary<List<int>, TrieNode> Children { get; private set; }
}
}