Exemplo n.º 1
0
        /// <summary>
        /// Removes the element with min priority and returns it.
        /// </summary>
        /// <returns></returns>
        public TValue RemoveMinimumPriority()
        {
            if (_elements == null)
            {
                throw new InvalidOperationException();
            }

            TValue temp = _elements.Data.Value;

            _elements = Merge(_elements.LeftChild, _elements.RightChild);
            Count--;
            return(temp);
        }
        /// <summary>
        /// Tries to remove the element from the tree with minimum priority
        /// </summary>
        /// <returns></returns>
        public TValue RemoveMinimumPriority()
        {
            if (Count == 0)
            {
                throw new InvalidOperationException();
            }

            TValue minPriority = _elements.Data.Value;

            _elements = Merge(_elements.LeftChild, _elements.RightChild);
            Count--;
            return(minPriority);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Method removes the element with minimum priority and return that element
        /// </summary>
        /// <returns>Value element of field</returns>
        public TValue RemoveMinimumPriority()
        {
            if (Count == 0)
            {
                throw new InvalidOperationException();
            }
            TValue tempValue = _elements.Data.Value;

            //LeftistTree<KeyValuePair<TPriority,TValue>> newRight = Merge(_elements.LeftChild.RightChild, _elements.RightChild);
            _elements = Merge(_elements.LeftChild, _elements.RightChild);
            Count--;
            return(tempValue);
        }
 public TValue RemoveMinimumPriority()
 {
     if (_elements == null)
     {
         throw new InvalidOperationException();
     }
     else
     {
         LeftistTree <KeyValuePair <TPriority, TValue> > min = _elements;
         Count--;
         _elements = Merge(_elements.LeftChild, _elements.RightChild);
         return(min.Data.Value);
     }
 }
 /// <summary>
 /// removes the min priority and returns the value that is min
 /// </summary>
 /// <returns>min val</returns>
 public TValue RemoveMinimumPriority()
 {
     if (_elements != null)
     {
         Count--;
         TValue val = _elements.Data.Value;
         _elements = Merge(_elements.RightChild, _elements.LeftChild);
         return(val);
     }
     else
     {
         throw new InvalidOperationException();
     }
 }
 /// <summary>
 /// Constructs a BinaryTreeNode with the given data, left child, and right child.
 /// </summary>
 /// <param name="data">The data stored in the node.</param>
 /// <param name="left">The left child.</param>
 /// <param name="right">The right child.</param>
 public LeftistTree(T data, LeftistTree <T> left, LeftistTree <T> right)
 {
     Data = data;
     if (left._pathLength >= right._pathLength)
     {
         LeftChild  = left;
         RightChild = right;
     }
     else
     {
         LeftChild  = left;
         RightChild = right;
     }
 }
Exemplo n.º 7
0
 /// <summary>
 /// Removes the root if the tree is not empty
 /// </summary>
 /// <returns>the value of the root</returns>
 public TValue RemoveMinimumPriority()
 {
     if (Count > 0)
     {
         TValue x = _elements.Data.Value;
         Count--;
         _elements = Merge(_elements.LeftChild, _elements.RightChild);
         return(x);
     }
     else
     {
         throw new InvalidOperationException();
     }
 }
Exemplo n.º 8
0
 /// <summary>
 /// Constructs a BinaryTreeNode with the given data, left child, and right child.
 /// </summary>
 /// <param name="data">The data stored in the node.</param>
 /// <param name="child1">a child.</param>
 /// <param name="child2">another child.</param>
 public LeftistTree(T data, LeftistTree <T> child1, LeftistTree <T> child2)
 {
     if (NullPathLength(child1) > NullPathLength(child2))
     {
         RightChild = child2;
         LeftChild  = child1;
     }
     else
     {
         RightChild = child1;
         LeftChild  = child2;
     }
     Data            = data;
     _nullPathLength = NullPathLength(RightChild) + 1;
 }
 /// <summary>
 /// Constructs a BinaryTreeNode with the given data, left child, and right child.
 /// </summary>
 /// <param name="data">The data stored in the node.</param>
 /// <param name="left">The left child.</param>
 /// <param name="right">The right child.</param>
 public LeftistTree(T data, LeftistTree <T> left, LeftistTree <T> right)
 {
     Data = data;
     if (NullPathLength(left) > NullPathLength(right))
     {
         RightChild = right;
         LeftChild  = left;
     }
     else
     {
         RightChild = left;
         LeftChild  = right;
     }
     _nullLength = 1 + NullPathLength(RightChild);
 }
 /// <summary>
 ///  remove the element with minimum priority and return that element
 /// </summary>
 /// <returns>returns minimum priority</returns>
 public TValue RemoveMinimumPriority()
 {
     if (Count == 0)
     {
         throw new InvalidOperationException();
     }
     else
     {
         TValue t = _elements.Data.Value;
         LeftistTree <KeyValuePair <TPriority, TValue> > temp = Merge(_elements.LeftChild, _elements.RightChild);
         _elements = temp;
         Count--;
         return(t);
     }
 }
Exemplo n.º 11
0
 /// <summary>
 /// Constructs a BinaryTreeNode with the given data, left child, and right child.
 /// </summary>
 /// <param name="data">The data stored in the node.</param>
 /// <param name="left">The left child.</param>
 /// <param name="right">The right child.</param>
 public LeftistTree(T data, LeftistTree <T> left, LeftistTree <T> right)
 {
     Data = data;
     if (NullPathLength(right) < NullPathLength(left))
     {
         LeftChild  = left;
         RightChild = right;
     }
     else
     {
         LeftChild  = right;
         RightChild = left;
     }
     _nullPathLength = NullPathLength(RightChild) + 1;
 }
Exemplo n.º 12
0
 /// <summary>
 /// Constructs a LeftistTree with the given data, left child, and right child.
 /// </summary>
 /// <param name="data">The data stored in the node.</param>
 /// <param name="left">The left child.</param>
 /// <param name="right">The right child.</param>
 public LeftistTree(T data, LeftistTree <T> child1, LeftistTree <T> child2)
 {
     Data = data;
     if (NullPathLength(child1) >= NullPathLength(child2))
     {
         LeftChild  = child1;
         RightChild = child2;
     }
     else
     {
         LeftChild  = child2;
         RightChild = child1;
     }
     _nullPathLength = NullPathLength(RightChild) + 1;
 }
Exemplo n.º 13
0
 /// <summary>
 /// Constructs a BinaryTreeNode with the given data, left child, and right child.
 /// </summary>
 /// <param name="data">The data stored in the node.</param>
 /// <param name="left">The left child.</param>
 /// <param name="right">The right child.</param>
 public LeftistTree(T data, LeftistTree <T> left, LeftistTree <T> right)
 {
     Data = data;
     if (NullPathLength(left) > NullPathLength(right))
     {
         LeftChild  = left;
         RightChild = right;
         _store     = NullPathLength(right) + 1;
     }
     else
     {
         LeftChild  = right;
         RightChild = left;
         _store     = NullPathLength(left) + 1;
     }
 }
Exemplo n.º 14
0
        /// <summary>
        /// Removes element with minimum priority.
        /// </summary>
        /// <returns>Return element of minimum priority.</returns>
        public TValue RemoveMinimumPriority()
        {
            TValue original;

            if (Count == 0)
            {
                throw new InvalidOperationException();
            }
            else
            {
                original  = _elements.Data.Value;
                _elements = Merge(_elements.LeftChild, _elements.RightChild);
                Count--;
            }
            return(original);
        }
Exemplo n.º 15
0
 /// <summary>
 /// Constructs a BinaryTreeNode with the given data, left child, and right child.
 /// </summary>
 /// <param name="data">The data stored in the node.</param>
 /// <param name="first">The first child.</param>
 /// <param name="second">The second child.</param>
 public LeftistTree(T data, LeftistTree <T> first, LeftistTree <T> second)
 {
     Data = data;
     if (NullPathLength(first) < NullPathLength(second))
     {
         RightChild = first;
         LeftChild  = second;
     }
     else
     {
         RightChild = second;
         LeftChild  = first;
     }
     // LeftChild = left;
     // RightChild = right;
     _nullLength = NullPathLength(RightChild) + 1;
 }
Exemplo n.º 16
0
 /// <summary>
 /// Constructs a BinaryTreeNode with the given data, left child, and right child.
 /// </summary>
 /// <param name="data">The data stored in the node.</param>
 /// <param name="left">The left child.</param>
 /// <param name="right">The right child.</param>
 /// Modify the constructor so that it initializes:
 ///RightChild to the given child with smaller null path length(use the above method);
 ///LeftChild to the other given child; and
 ///the null path length field to 1 more than the null path length of the right child.
 public LeftistTree(T data, LeftistTree <T> left, LeftistTree <T> right)
 {
     Data = data;
     if (NullPathLength(right) < NullPathLength(left))
     {
         RightChild = right;
         LeftChild  = left;
     }
     else
     {
         // the left child is smaller
         RightChild = left;
         LeftChild  = right;
     }
     // make sure students use the correct right child
     _nullPathLength = 1 + NullPathLength(RightChild);
 }
        /// <summary>
        /// Constructs a BinaryTreeNode with the given data, left child, and right child.
        /// </summary>
        /// <param name="data">The data stored in the node.</param>
        /// <param name="left">The left child.</param>
        /// <param name="right">The right child.</param>
        public LeftistTree(T data, LeftistTree <T> left, LeftistTree <T> right)
        {
            Data = data;

            if (NullPathLength(left) > NullPathLength(right))
            {
                LeftChild       = left;
                RightChild      = right;
                _nullPathLength = 1 + NullPathLength(RightChild);
            }
            else
            {
                LeftChild  = right;
                RightChild = left;
                // _nullPathLength = 1 + RightChild._nullPathLength;
                _nullPathLength = 1 + NullPathLength(RightChild);
            }
        }
Exemplo n.º 18
0
        /// <summary>
        /// Constructs a BinaryTreeNode with the given data, left child, and right child.
        /// </summary>
        /// <param name="data">The data stored in the node.</param>
        /// <param name="left">The left child.</param>
        /// <param name="right">The right child.</param>
        public LeftistTree(T data, LeftistTree <T> left, LeftistTree <T> right)
        {
            Data = data;
            int smallerLeft  = NullPathLength(left);
            int smallerRight = NullPathLength(right);

            if (smallerRight < smallerLeft)
            {
                RightChild = right;
                LeftChild  = left;
            }
            else
            {
                RightChild = left;
                LeftChild  = right;
            }
            _nullPathLength = 1 + NullPathLength(RightChild);
        }
Exemplo n.º 19
0
        /// <summary>
        /// Constructs a BinaryTreeNode with the given data, left child, and right child.
        /// </summary>
        /// <param name="data">The data stored in the node.</param>
        /// <param name="left">The left child.</param>
        /// <param name="right">The right child.</param>
        public LeftistTree(T data, LeftistTree <T> left, LeftistTree <T> right)
        {
            Data = data;
            int l = NullPathLength(left);
            int r = NullPathLength(right);

            if (l < r)
            {
                RightChild      = left;
                LeftChild       = right;
                _nullPathLength = l + 1;
            }
            else
            {
                LeftChild       = left;
                RightChild      = right;
                _nullPathLength = r + 1;
            }
        }
Exemplo n.º 20
0
        /// <summary>
        /// Constructs a LeftTreeNode with the given data, left child, and right child.
        /// </summary>
        /// <param name="data">The data stored in the node.</param>
        /// <param name="left">The left child.</param>
        /// <param name="right">The right child.</param>
        public LeftistTree(T data, LeftistTree <T> left, LeftistTree <T> right)
        {
            Data = data;
            int rightLength = NullPathLength(right);
            int leftLength  = NullPathLength(left);

            if (rightLength > leftLength)
            {
                RightChild      = left;
                LeftChild       = right;
                _nullPathLength = NullPathLength(RightChild) + 1;
            }
            else
            {
                RightChild      = right;
                LeftChild       = left;
                _nullPathLength = NullPathLength(RightChild) + 1;
            }
        }
Exemplo n.º 21
0
        /// <summary>
        /// Constructs a LeftistTree with the given data, left child, and right child.
        /// </summary>
        /// <param name="data">The data stored in the node.</param>
        /// <param name="left">The left child.</param>
        /// <param name="right">The right child.</param>
        public LeftistTree(T data, LeftistTree <T> left, LeftistTree <T> right)
        {
            Data = data;
            int leftPath  = NullPathLength(left);
            int rightPath = NullPathLength(right);

            if (leftPath < rightPath)
            {
                RightChild = left;
                LeftChild  = right;
            }
            else
            {
                LeftChild  = left;
                RightChild = right;
            }

            _pathLength = Math.Min(leftPath, rightPath) + 1;
        }
Exemplo n.º 22
0
 /// <summary>
 /// Merges the given leftist heaps into one leftist heap.
 /// </summary>
 /// <param name="h1">One of the leftist heaps to merge.</param>
 /// <param name="h2">The other leftist heap to merge.</param>
 /// <returns>The resulting leftist heap.</returns>
 public static LeftistTree <KeyValuePair <TPriority, TValue> > Merge(LeftistTree <KeyValuePair <TPriority, TValue> > h1,
                                                                     LeftistTree <KeyValuePair <TPriority, TValue> > h2)
 {
     if (h1 == null)
     {
         return(h2);
     }
     else if (h2 == null)
     {
         return(h1);
     }
     if (h1.Data.Key.CompareTo(h2.Data.Key) > 0)
     {
         return(new LeftistTree <KeyValuePair <TPriority, TValue> >(h2.Data, h2.LeftChild, Merge(h1, h2.RightChild)));
     }
     else
     {
         return(new LeftistTree <KeyValuePair <TPriority, TValue> >(h1.Data, h1.LeftChild, Merge(h2, h1.RightChild)));
     }
 }
        /// <summary>
        /// Constructs a BinaryTreeNode with the given data, left child, and right child.
        /// </summary>
        /// <param name="data">The data stored in the node.</param>
        /// <param name="left">The left child.</param>
        /// <param name="right">The right child.</param>
        public LeftistTree(T data, LeftistTree <T> left, LeftistTree <T> right)
        {
            Data = data;
            int llength = NullPathLength(left);
            int rlength = NullPathLength(right);

            if (llength < rlength)
            {
                RightChild = left;
                LeftChild  = right;
            }

            else
            {
                LeftChild  = left;
                RightChild = right;
            }

            _nullPathLength = 1 + NullPathLength(RightChild);
        }
Exemplo n.º 24
0
 /// <summary>
 /// Merges the given leftist heaps into one leftist heap.
 /// </summary>
 /// <param name="h1">One of the leftist heaps to merge.</param>
 /// <param name="h2">The other leftist heap to merge.</param>
 /// <returns>The resulting leftist heap.</returns>
 public static LeftistTree <KeyValuePair <TPriority, TValue> > Merge(LeftistTree <KeyValuePair <TPriority, TValue> > h1,
                                                                     LeftistTree <KeyValuePair <TPriority, TValue> > h2)
 {
     if (LeftistTree <KeyValuePair <TPriority, TValue> > .NullPathLength(h1) == 0)
     {
         return(h2);
     }
     else if (LeftistTree <KeyValuePair <TPriority, TValue> > .NullPathLength(h2) == 0)
     {
         return(h1);
     }
     else if (h1.Data.Key.CompareTo(h2.Data.Key) < 0)
     {
         return(new LeftistTree <KeyValuePair <TPriority, TValue> >(h1.Data, h1.LeftChild, Merge(h1.RightChild, h2)));
     }
     else
     {
         return(new LeftistTree <KeyValuePair <TPriority, TValue> >(h2.Data, h2.LeftChild, Merge(h2.RightChild, h1)));
     }
 }
        /// <summary>
        /// Merges the given leftist heaps into one leftist heap.
        /// </summary>
        /// <param name="h1">One of the leftist heaps to merge.</param>
        /// <param name="h2">The other leftist heap to merge.</param>
        /// <returns>The resulting leftist heap.</returns>
        public static LeftistTree <KeyValuePair <TPriority, TValue> > Merge(LeftistTree <KeyValuePair <TPriority, TValue> > h1,
                                                                            LeftistTree <KeyValuePair <TPriority, TValue> > h2)
        {
            if (h1 == null)
            {
                return(h2);
            }

            if (h2 == null)
            {
                return(h1);
            }

            TPriority h1Priority = h1.Data.Key;
            TPriority h2Priority = h2.Data.Key;
            TPriority priority;
            TValue    value;
            LeftistTree <KeyValuePair <TPriority, TValue> > large;
            LeftistTree <KeyValuePair <TPriority, TValue> > small;

            if (h1Priority.CompareTo(h2Priority) <= 0)
            {
                priority = h1Priority;
                value    = h1.Data.Value;
                large    = h2;
                small    = h1;
            }

            else
            {
                priority = h2Priority;
                value    = h2.Data.Value;
                large    = h1;
                small    = h2;
            }

            KeyValuePair <TPriority, TValue> newRoot = new KeyValuePair <TPriority, TValue>(priority, value);
            LeftistTree <KeyValuePair <TPriority, TValue> > newTree = new LeftistTree <KeyValuePair <TPriority, TValue> >(newRoot, small.LeftChild, Merge(small.RightChild, large));

            return(newTree);
        }
Exemplo n.º 26
0
        /// <summary>
        /// Constructs a BinaryTreeNode with the given data, left child, and right child.
        /// </summary>
        /// <param name="data">The data stored in the node.</param>
        /// <param name="left">The left child.</param>
        /// <param name="right">The right child.</param>
        public LeftistTree(T data, LeftistTree <T> left, LeftistTree <T> right)
        {
            /*Data = data;
             * LeftChild = left;
             * RightChild = right;*/
            Data = data;


            if (NullPathLength(left) < NullPathLength(right))
            {
                RightChild      = left;
                LeftChild       = right;
                _nullPathLength = NullPathLength(RightChild) + 1;
            }
            else
            {
                RightChild      = right;
                LeftChild       = left;
                _nullPathLength = NullPathLength(RightChild) + 1;
            }
        }
        /// <summary>
        /// Constructs a BinaryTreeNode with the given data, left child, and right child.
        /// </summary>
        /// <param name="data">The data stored in the node.</param>
        /// <param name="left">The left child.</param>
        /// <param name="right">The right child.</param>
        ///
        public LeftistTree(T data, LeftistTree <T> left, LeftistTree <T> right)
        {
            Data       = data;
            LeftChild  = left;
            RightChild = right;

            int lengthRight = NullPathLength(right);
            int lengthLeft  = NullPathLength(left);

            if (lengthLeft < lengthRight)
            {
                RightChild = left;
                LeftChild  = right;
            }
            else
            {
                RightChild = right;
                LeftChild  = left;
            }
            _nullPathLength = NullPathLength(RightChild) + 1;
        }
        /// <summary>
        /// Constructs a BinaryTreeNode with the given data, left child, and right child.
        /// </summary>
        /// <param name="data">The data stored in the node.</param>
        /// <param name="left">The left child.</param>
        /// <param name="right">The right child.</param>
        public LeftistTree(T data, LeftistTree <T> left, LeftistTree <T> right)
        {
            LeftistTree <T> shortChild;
            LeftistTree <T> tallChild;

            if (NullPathLength(left) < NullPathLength(right))
            {
                shortChild = left;
                tallChild  = right;
            }
            else
            {
                shortChild = right;
                tallChild  = left;
            }

            Data            = data;
            LeftChild       = tallChild;
            RightChild      = shortChild;
            _nullPathLength = NullPathLength(RightChild) + 1;
        }
        /// <summary>
        /// Merges the given leftist heaps into one leftist heap.
        /// </summary>
        /// <param name="h1">One of the leftist heaps to merge.</param>
        /// <param name="h2">The other leftist heap to merge.</param>
        /// <returns>The resulting leftist heap.</returns>
        public static LeftistTree <KeyValuePair <TPriority, TValue> > Merge(LeftistTree <KeyValuePair <TPriority, TValue> > h1,
                                                                            LeftistTree <KeyValuePair <TPriority, TValue> > h2)
        {
            if (h1 == null)
            {
                return(h2);
            }
            if (h2 == null)
            {
                return(h1);
            }
            int comparison = h1.Data.Key.CompareTo(h2.Data.Key);

            if (comparison < 0)
            {
                LeftistTree <KeyValuePair <TPriority, TValue> > temp = new LeftistTree <KeyValuePair <TPriority, TValue> >(h1.Data, h1.LeftChild, Merge(h1.RightChild, h2));
                return(temp);
            }
            LeftistTree <KeyValuePair <TPriority, TValue> > temp2 = new LeftistTree <KeyValuePair <TPriority, TValue> >(h2.Data, h2.LeftChild, Merge(h2.RightChild, h1));

            return(temp2);
        }
Exemplo n.º 30
0
 /// <summary>
 /// Merges the given leftist heaps into one leftist heap.
 /// </summary>
 /// <param name="h1">One of the leftist heaps to merge.</param>
 /// <param name="h2">The other leftist heap to merge.</param>
 /// <returns>The resulting leftist heap.</returns>
 public static LeftistTree <KeyValuePair <TPriority, TValue> > Merge(LeftistTree <KeyValuePair <TPriority, TValue> > h1,
                                                                     LeftistTree <KeyValuePair <TPriority, TValue> > h2)
 {
     if (h1 == null)
     {
         return(h2);
     }
     else if (h2 == null)
     {
         return(h1);
     }
     else
     {
         LeftistTree <KeyValuePair <TPriority, TValue> > smaller = h1;
         LeftistTree <KeyValuePair <TPriority, TValue> > larger  = h2;
         if (h1.Data.Key.CompareTo(h2.Data.Key) > 0)
         {
             larger  = h1;
             smaller = h2;
         }
         return(new LeftistTree <KeyValuePair <TPriority, TValue> >(smaller.Data, smaller.LeftChild, Merge(smaller.RightChild, larger)));
     }
 }