Exemplo n.º 1
0
        ///<summary>Insert new node</summary>
        ///<param name="prev">Node inserted before</param>
        ///<param name="value">Value of new node</param>
        ///<returns>Returns new inserted node</returns>
        private CTNode <T> InsertNewNode(CTNode <T> prev, T value)
        {
            CTNode <T> newNode       = new CTNode <T>(value);
            CTNode <T> currentParent = prev;

            while (IsBigger(currentParent.Value, value))
            {
                if (currentParent.Parent == null)
                {
                    break;
                }
                else
                {
                    currentParent = currentParent.Parent;
                }
            }

            if (IsBigger(currentParent.Value, value))
            {
                //currentParent is root node
                //newNode will be the new root and its left child must be currentParent
                newNode.SetLeft(currentParent);
                currentParent.SetParent(newNode);
            }
            else
            {
                InsertToRight(currentParent, newNode);
            }


            return(newNode);
        }
Exemplo n.º 2
0
        ///<summary>
        ///Insert newNode to parent node as right child and also
        ///replace parent right child as a left child of newNode
        ///</summary>
        private void InsertToRight(CTNode <T> parent, CTNode <T> newNode)
        {
            newNode.SetParent(parent);
            newNode.SetLeft(parent.Right);

            if (parent.Right != null)
            {
                parent.Right.SetParent(newNode);
            }

            parent.SetRight(newNode);
        }