コード例 #1
0
        // ! Passed Casual Testing
        /// <summary>
        /// Extenstion method, Merge.
        /// </summary>
        /// <typeparam name="R"></typeparam>
        /// <param name="tis"></param>
        /// <param name="that"></param>
        /// <exception cref="InvalidArgumentException">
        /// Thrown when merging 2 Btree with different rank.
        /// </exception>
        /// <returns></returns>
        public static HatinaryTree <R> Merge <R>
            (HatinaryTree <R> tis, HatinaryTree <R> that)
            where R : IComparable <R>
        {
            if (object.ReferenceEquals(tis, that))
            {
                throw new Exception("Dude stops right there.");
            }
            if (tis.Rank != that.Rank)
            {
                throw new InvalidArgumentException("Tree Merge different rank. ");
            }

            if (tis.Hat.Data.CompareTo(that.Hat.Data) > 0)
            {
                return(Merge(that, tis));
            }
            HatinaryTree <R> MergedTree = new HatinaryTree <R>();

            MergedTree.Hat = tis.Hat;
            BNode <R> NewRoot = that.Hat;

            NewRoot.RightChild = that.Root;
            NewRoot.LeftChild  = tis.Root;
            MergedTree.Root    = NewRoot;
            MergedTree.Rank    = tis.Rank + 1;
            return(MergedTree);
        }
コード例 #2
0
        // ! Haven't been tested yet!!!
        /// <summary>
        /// Method will remove the root nodes of the tree and return
        /// a new array of trees for merging in the heap, with the size of rank.
        /// <para>
        ///  It will not modifiy the current tree.
        ///  </para>
        ///
        /// <para>
        /// The size of the returned array equals to the rank of the tree.
        /// </para>
        /// </summary>
        /// <returns>
        /// Null is returned in the current tree is rank 0.
        /// </returns>
        public HatinaryTree <T>[] Expand()
        {
            if (Rank == 0)
            {
                return(null);
            }
            var res = new HatinaryTree <T> [Rank];

            {
                res[Rank - 1] = new HatinaryTree <T>(Hat.Data);
                BNode <T> node = Root; // ! Reference pointer, for traversing the tree.
                for (int i = Rank - 2; i >= 0; i--)
                {
                    res[i] = new HatinaryTree <T>
                             (
                        new BNode <T>(node.Data), node.RightChild
                             );
                    node = node.LeftChild;
                }
            }
            return(res);
        }
コード例 #3
0
 /// <summary>
 ///Merge 2 Btree with the same rank. It will not modify any of the tree, it
 ///will return a new reference to the merged new tree.
 /// <exception>
 /// Can not merge tree with 2 different rank.
 /// </exception>
 /// </summary>
 public HatinaryTree <T> Merge(HatinaryTree <T> t)
 {
     return(Merge(this, t));
 }