RadixTree.Tree.DeleteRec C# (CSharp) Method

DeleteRec() public method

delete a word from the tree means delete the last leaf that makes up the stored word
public DeleteRec ( string wordPart, Node curNode ) : void
wordPart string
curNode Node
return void
        public void DeleteRec(string wordPart, Node curNode)
        {
            var matches = MatchingConsecutiveCharacters(wordPart, curNode);

            if ((matches == 0) || (curNode == _root) ||
                ((matches > 0) && (matches < wordPart.Length) && (matches >= curNode.Label.Length)))
            {

                var newLabel = wordPart.Substring(matches, wordPart.Length - matches);
                foreach (var child in curNode.SubNodes)
                    if (child.Label.StartsWith(newLabel[0].ToString()))
                    {
                        if (newLabel == child.Label)
                        {
                            if (child.SubNodes.Count == 0)
                            {
                                curNode.SubNodes.Remove(child);
                                return;
                            }
                        }

                        DeleteRec(newLabel, child);
                    }
            }
        }