public void PrinInfo(CharNode cn, int depth) { Console.Write(string.Join("", Enumerable.Repeat(" ", depth))); Console.WriteLine($"{cn.Value}=>{cn.Count}"); if (!cn.HasChild()) { return; } foreach (CharNode childNode in cn) { PrinInfo(childNode, depth + 1); } }
public long Count(string s) { char[] ca = s.ToCharArray(); CharNode cn = rootNode; for (int i = 0; i < ca.Length; i++) { if (cn.HasChild(ca[i])) { if (i == ca.Length - 1) { return(cn[ca[i]].Count); } cn = cn[ca[i]]; } else { return(0); } } return(0); }
public void Put(string s) { char[] cs = s.ToCharArray(); if (!rootNode.HasChild(cs[0])) { rootNode.AddChild(cs[0]); } CharNode cn = rootNode[cs[0]]; cn.Count++; for (int i = 1; i < cs.Length; i++) { if (!cn.HasChild(cs[i])) { cn.AddChild(cs[i]); } cn = cn[cs[i]]; cn.Count++; } }
public Trie(int n) { N = n; rootNode = new CharNode(); }