示例#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);
        }
示例#2
0
        private void BuildTreeFromArray(T[] array)
        {
            // choosing algorithm to build tree (today is only one algorithm named 'CTBSift')

            CTBSift <T>   siftBuildAlgorithm = new CTBSift <T>(compare);
            CTBuilder <T> ctBuilder          = new CTBuilder <T>(siftBuildAlgorithm);

            root = ctBuilder.CreateTree(array);
        }
示例#3
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);
        }
示例#4
0
        public override CTNode <T> BuildTree(T[] array)
        {
            CTNode <T> root     = new CTNode <T>(array[0]);
            CTNode <T> previous = root;

            for (int i = 1; i < array.Length; i++)
            {
                previous = InsertNewNode(previous, array[i]);

                if (previous.Parent == null)
                {
                    root = previous;
                }
            }

            return(root);
        }