Example #1
0
        public bool Contains(string word)
        {
            Node current = _root;

            foreach (var letter in word)
            {
                if (!current.HasChild(letter))
                {
                    return(false);
                }

                current = current.GetChild(letter);
            }
            return(current._isEndOfWord);
        }
Example #2
0
        private bool ContaisRecursive(Node current, string word, int index)
        {
            if (index == word.Length)
            {
                return(current._isEndOfWord);
            }

            var letter = word[index];

            if (!current.HasChild(letter))
            {
                return(false);
            }

            return(ContaisRecursive(
                       current.GetChild(letter),
                       word,
                       index + 1));
        }
Example #3
0
        public void Insert(string word)
        {
            if (string.IsNullOrEmpty(word))
            {
                throw new Exception();
            }

            Node current = _root;

            foreach (var ch in word)
            {
                if (!current.HasChild(ch))
                {
                    current.AddChild(ch);
                }

                current = current.GetChild(ch);
            }
            current._isEndOfWord = true;
        }
Example #4
0
        private void Delete(Node root, string word, int index)
        {
            if (index == word.Length)
            {
                root._isEndOfWord = false;
                return;
            }

            var ch    = word[index];
            var child = root.GetChild(ch);

            if (child == null)
            {
                return;
            }

            Delete(child, word, index + 1);

            if (!child.HasChildren() && !child._isEndOfWord)
            {
                root.RemoveChild(ch);
            }
            //Console.WriteLine(ch);
        }