Esempio n. 1
0
        public bool AddChild(string str)
        {
            var ch = str[0];

            if (!childs.TryGetValue(ch, out var child))
            {
                child = new NodeTrie();
                childs.Add(ch, child);
                if (str.Length == 1)
                {
                    ++child.Count;
                    child.Word = true;
                    return(true);
                }
            }
            else if (str.Length == 1)
            {
                if (!child.Word)
                {
                    ++child.Count;
                    child.Word = true;
                    return(true);
                }
                return(false);
            }

            if (child.AddChild(str.Substring(1, str.Length - 1)))
            {
                ++child.Count;
                return(true);
            }

            return(false);
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            var notebook = new NodeTrie();

            Console.WriteLine($"Enter 's' for start search");
            while (true)
            {
                var input = Console.ReadLine();
                if (input.Equals("s", StringComparison.OrdinalIgnoreCase))
                {
                    break;
                }

                if (notebook.AddChild(input))
                {
                    Console.WriteLine($"Add {input}");
                }
                else
                {
                    Console.WriteLine($"Alread exists {input}");
                }
            }

            Console.WriteLine($"Enter 'e' for start search");
            while (true)
            {
                var input = Console.ReadLine();
                if (input.Equals("e", StringComparison.OrdinalIgnoreCase))
                {
                    break;
                }

                var count = notebook.Search(input);
                Console.WriteLine($"{input} -> {count}");
            }
        }