-
Notifications
You must be signed in to change notification settings - Fork 1
/
TrieNode.cs
43 lines (35 loc) · 979 Bytes
/
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
using System;
using System.Collections;
using System.Collections.Generic;
namespace PrefixTree {
public class TrieNode : Hashtable {
private TrieNode parent;
private char? value;
public TrieNode(char? value = null, TrieNode parent = null)
: base() {
this.parent = parent;
this.value = value;
}
public bool EndsWord {
get;
set;
}
public TrieNode this[char key] {
get { return base[key] as TrieNode; }
set { base[key] = value; }
}
public TrieNode FindOrCreate(char ch) {
return this[ch] = this[ch] ?? new TrieNode( ch, this );
}
public void ToList(ref List<string> wordList) {
foreach( TrieNode node in Values ) {
if( node.EndsWord )
wordList.Add( node.ToString() );
node.ToList( ref wordList );
}
}
public override string ToString() {
return String.Concat( parent == null ? "" : parent.ToString(), this.value );
}
}
}