public void Initialize(WordNode parentNode, string characters)
        {
            if (string.IsNullOrEmpty(characters))
            {
                return;
            }
            var firstChar = characters[0];

            var node = ChildNodes.Find(r => r.Character == firstChar);

            if (node == null)
            {
                var newChild = new WordNode(characters.Remove(0, 1))
                {
                    Character = firstChar
                };
                newChild.Parent = parentNode;
                if (characters.Length == 1)
                {
                    newChild.IsWord = true;
                }

                ChildNodes.Add(newChild);
            }
            else
            {
                node.Initialize(node, characters.Remove(0, 1));
            }
        }
        private WordNode GetLastNodeOfWord(WordNode targetNode, string word)
        {
            if (word.Length == 0)
            {
                return(targetNode);
            }

            targetNode = targetNode.ChildNodes.Find(r => r.Character == word[0]);
            return(targetNode.GetLastNodeOfWord(targetNode, word.Remove(0, 1)));
        }
 /// <summary>
 /// Reads and loads JSON File
 /// </summary>
 /// <param name="filePath"></param>
 /// <returns>True if success otherwise false</returns>
 public bool LoadLocal(string filePath)
 {
     try
     {
         string data = File.ReadAllText(filePath);
         Root = JsonConvert.DeserializeObject <WordNode>(data, new JsonSerializerSettings()
         {
             PreserveReferencesHandling = PreserveReferencesHandling.Objects
         });
         return(true);
     }
     catch (Exception)
     {
         return(false);
     }
 }
        public List <string> GetWordsByPrefix(string prefix)
        {
            //TODO Memory Profiling
            List <string>    words    = new List <string>();
            WordNode         lastNode = GetLastNodeOfWord(this, prefix);
            Stack <WordNode> nodes    = new Stack <WordNode>();

            nodes.Push(lastNode);

            while (nodes.Count > 0)
            {
                WordNode node = nodes.Pop();
                foreach (WordNode childNode in node.ChildNodes)
                {
                    nodes.Push(childNode);
                }

                if (!node.IsWord)
                {
                    continue;
                }
                string           word       = "";
                Stack <WordNode> wordsStack = new Stack <WordNode>();
                while (node.Parent != null)
                {
                    wordsStack.Push(node);
                    node = node.Parent;
                }

                while (wordsStack.Count > 0)
                {
                    word += wordsStack.Pop().Character;
                }
                words.Add(word);
            }

            return(words);
        }
 public WordDictionary()
 {
     Root = new WordNode("");
 }