示例#1
0
文件: Trie.cs 项目: shanch/Experiment
        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);
            }
        }
示例#2
0
文件: Trie.cs 项目: shanch/Experiment
        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);
        }
示例#3
0
文件: Trie.cs 项目: shanch/Experiment
        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++;
            }
        }
示例#4
0
文件: Trie.cs 项目: shanch/Experiment
 public Trie(int n)
 {
     N        = n;
     rootNode = new CharNode();
 }