RadixTree.Tree.InsertRec C# (CSharp) Method

InsertRec() private method

recursively traverse the tree carry the word you want inserted until a proper place for it is found and it can be inserted there if a node already stores a substring of the word(substrnig with the same first letter as the word itself) then that substring must be removed from the word and it's children checked out next hence the name wordPart - part of a word
private InsertRec ( string wordPart, Node curNode ) : void
wordPart string the part of the word that is to be inserted that is not already included in any of the tree's nodes
curNode Node the node currently traversed
return void
        private void InsertRec(string wordPart, Node curNode)
        {
            //get the number of characters that the word part that is to be inserted and the current node's label have
            //in common starting from the first position of both strings
            //matching characters in the two strings = have the same value at the same position in both strings
            var matches = MatchingConsecutiveCharacters(wordPart, curNode);

            //if we are at the root node
            //OR
            //the number of characters from the two strings that match is
            //bigger than 0
            //smaller than the the part of the word that is to be inserted
            //bigger than the the label of the current node
            if  ((matches == 0) || (curNode == _root) ||
                ((matches > 0) && (matches < wordPart.Length) && (matches >= curNode.Label.Length)))
                {
                    //remove the current node's label from the word part
                    bool inserted = false;
                    var newWordPart = wordPart.Substring(matches, wordPart.Length - matches);
                    //search the node's subnodes and if the subnode label's first character matches
                    //the word part's first character then insert the word part after this node(call the
                    //current method recursively)
                    foreach(var child in curNode.SubNodes)
                        if (child.Label.StartsWith(newWordPart[0].ToString()))
                        {
                            inserted = true;
                            InsertRec(newWordPart, child);
                        }
                    if (inserted == false)
                    {
                        curNode.SubNodes.Add(new Node(newWordPart));
                    }
                }
                else if(matches < wordPart.Length)
                {
                    //in this case we have to nodes that we must add to the tree
                    //one is the node that has a label extracted from the current node's label without the string of
                    //matching characters(common characters)
                    //the other is the node that has it's label extracted from the current word part minus the string
                    //of matching characters
                    string commonRoot = wordPart.Substring(0, matches);
                    string branchPreviousLabel = curNode.Label.Substring(matches, curNode.Label.Length - matches);
                    string branchNewLabel = wordPart.Substring(matches, wordPart.Length - matches);

                    curNode.Label = commonRoot;

                    var newNodePreviousLabel = new Node(branchPreviousLabel);
                    newNodePreviousLabel.SubNodes.AddRange(curNode.SubNodes);

                    curNode.SubNodes.Clear();
                    curNode.SubNodes.Add(newNodePreviousLabel);

                    var newNodeNewLabel = new Node(branchNewLabel);
                    curNode.SubNodes.Add(newNodeNewLabel);
                }
                else if (matches == curNode.Label.Length)
                {
                    //in this case we don't do anything because the word is already added
                }
                else if (matches > curNode.Label.Length)
                {
                    //add the current word part minus the common characters after the current node
                    string newNodeLabel = curNode.Label.Substring(curNode.Label.Length, wordPart.Length);
                    var newNode = new Node(newNodeLabel);
                    curNode.SubNodes.Add(newNode);
                }
        }