Пример #1
0
        public override int Pop()
        {
            var result = HeapBase[0];

            HeapBase[0] = HeapBase.Last();
            HeapBase.RemoveAt(Count - 1);
            FloydRestoreHeapProperty();
            return(result);
        }
Пример #2
0
        /// <summary>
        /// Performs a breadth-first search on the tree underlying the specied integer heap, returning all the nodes of the tree in a Dictionary.
        /// </summary>
        /// <param name="inputHeap">The heap to search.</param>
        /// <returns>A Dictionary keyed by the integer item of each node, and containing all the nodes in the tree.</returns>
        protected Dictionary <Int32, DoublyLinkedTreeNode <Int32> > GetAllNodes(HeapBase <Int32> inputHeap)
        {
            Dictionary <Int32, DoublyLinkedTreeNode <Int32> > returnDictionary = new Dictionary <Int32, DoublyLinkedTreeNode <Int32> >();
            Action <DoublyLinkedTreeNode <Int32> >            nodeAction       = (node) =>
            {
                returnDictionary.Add(node.Item, node);
            };

            inputHeap.BreadthFirstSearch(nodeAction);
            return(returnDictionary);
        }
Пример #3
0
        /// <summary>
        /// Performs a breadth-first search on the tree underlying the specied integer heap, returning all the items in the tree in breadth-first order.
        /// </summary>
        /// <param name="inputHeap">The heap to search.</param>
        /// <returns>A list containing the items in the tree in breadth-first order.</returns>
        protected List <Int32> GetListOfItems(HeapBase <Int32> inputHeap)
        {
            List <Int32> returnList = new List <Int32>();
            Action <DoublyLinkedTreeNode <Int32> > nodeAction = (node) =>
            {
                returnList.Add(node.Item);
            };

            inputHeap.BreadthFirstSearch(nodeAction);
            return(returnList);
        }
Пример #4
0
        /// <summary>
        /// Fills the heap with random elements
        /// </summary>
        /// <param name="heap">The heap.</param>
        /// <param name="numberOfElements">The number of elements.</param>
        private static void FillHeap(HeapBase <HeapElement> heap, int numberOfElements)
        {
            Random rand = new Random(unchecked ((int)DateTime.Now.Ticks));

            // fill heap with elements
            for (int i = 0; i < numberOfElements; i++)
            {
                // add a new element to the heap, with a random priority from the list and a value.
                heap.Insert(new HeapElement(rand.Next(numberOfElements), "Value: " + i));
            }
        }
Пример #5
0
 public void SetUp()
 {
     _array = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
     _heap  = new MinHeap <int>(_array);
 }
Пример #6
0
		/// <summary>
		/// Fills the heap with random elements
		/// </summary>
		/// <param name="heap">The heap.</param>
		/// <param name="numberOfElements">The number of elements.</param>
		private static void FillHeap(HeapBase<HeapElement> heap, int numberOfElements)
		{
			Random rand = new Random(unchecked((int)DateTime.Now.Ticks));

			// fill heap with elements
			for(int i = 0; i < numberOfElements; i++)
			{
				// add a new element to the heap, with a random priority from the list and a value.
				heap.Insert(new HeapElement(rand.Next(numberOfElements), "Value: " + i));
			}
		}