예제 #1
0
파일: MaxHeap.cs 프로젝트: bo29/heaps
 public static int[] sort(int[] input)
 {//an implementation of HeapSort; puts array into decreasing order
     MaxHeap tempHeap = new MaxHeap(input);
     int[] output = new int[tempHeap.Size()];
     for (int i = 0; i < output.Length; i++)
     {
         output[i] = tempHeap.pluck();
     }
     return output;
 }
예제 #2
0
        static void Main(string[] args)
        {
            Heap heap = new Heap();

            heap.insert(10);
            heap.insert(17);
            heap.insert(5);
            heap.insert(4);
            heap.insert(22);
            heap.remove();

            //Sort array items in ascending order

            //not working..
            heap.sortAscending(new int[5] {
                5, 20, 11, 7, 8
            });
            Console.WriteLine("----------------------");
            heap.sortDescending(new int[5] {
                5, 20, 11, 7, 8
            });

            MaxHeap.heapify(new int[6] {
                5, 3, 8, 4, 1, 2
            });

            MaxHeap maxHeap    = new MaxHeap();
            var     kthLargest = maxHeap.getKthLargestNumber(new int[6] {
                5, 3, 8, 4, 1, 2
            }, 3);

            Console.WriteLine("Kth Largest number is :" + kthLargest);

            Console.WriteLine("-----------HeapExercise----------");
            HeapExercise heapExercise = new HeapExercise();
            var          isMaxHeap    = heapExercise.isMaxHeap(new int[6] {
                5, 3, 8, 4, 1, 2
            });

            Console.WriteLine("Is this is a Max Heap -> { 5, 3, 8, 4, 1, 2 } :" + isMaxHeap);

            Console.ReadLine();
        }
예제 #3
0
        static void Main(string[] args)
        {
            var unsorted = new[] { 3, 2, 1, 7, 8, 4, 10, 16, 12 };
            var minHeap  = new MinHeap(unsorted);
            var maxHeap  = new MaxHeap(unsorted);

            var l = minHeap.Length();

            Console.Write("Array in ascending order : ");
            for (var i = 0; i < l; i++)
            {
                Console.Write(minHeap.ExtractMin() + " ");
            }
            Console.WriteLine();

            var l1 = maxHeap.Length();

            Console.Write("Array in descending order : ");
            for (var i = 0; i < l1; i++)
            {
                Console.Write(maxHeap.ExtractMax() + " ");
            }
            Console.WriteLine();
        }
예제 #4
0
        static void Main(string[] args)
        {
            Heap heap = new Heap();

            heap.insert(10);
            heap.insert(17);
            heap.insert(5);
            heap.insert(4);
            heap.insert(22);
            heap.remove();

            //Sort array items in ascending order

            //not working..
            heap.sortAscending(new int[5] {
                5, 20, 11, 7, 8
            });
            Console.WriteLine("----------------------");
            heap.sortDescending(new int[5] {
                5, 20, 11, 7, 8
            });

            MaxHeap.heapify(new int[6] {
                5, 3, 8, 4, 1, 2
            });

            MaxHeap maxHeap    = new MaxHeap();
            var     kthLargest = maxHeap.getKthLargestNumber(new int[6] {
                5, 3, 8, 4, 1, 2
            }, 3);

            Console.WriteLine("Kth Largest number is :" + kthLargest);

            Console.WriteLine("-----------HeapExercise----------");
            HeapExercise heapExercise = new HeapExercise();
            var          isMaxHeap    = heapExercise.isMaxHeap(new int[6] {
                8, 4, 5, 3, 10, 2
            });

            Console.WriteLine("Is this is a Max Heap -> { 5, 3, 8, 4, 1, 2 } :" + isMaxHeap);


            Console.WriteLine("-----------MinHeap----------");
            MinHeap minHeap = new MinHeap();

            minHeap.insert(30, "A");
            minHeap.insert(3, "B");
            minHeap.insert(60, "C");
            minHeap.insert(1, "D");
            minHeap.insert(10, "E");
            //minHeap.insert(2, "F");

            Console.WriteLine(minHeap.print());

            Console.WriteLine("Removed value is from MinHeap is : " + minHeap.remove());

            Console.WriteLine("-----------PriorityQueueWithMinHeap----------");
            PriorityQueueWithMinHeap priorityQueueWithMinHeap = new PriorityQueueWithMinHeap();

            priorityQueueWithMinHeap.add("Kavita", 5);
            priorityQueueWithMinHeap.add("Brother", 3);
            priorityQueueWithMinHeap.add("Mom", 1);
            priorityQueueWithMinHeap.add("Dad", 2);
            priorityQueueWithMinHeap.add("Brother", 3);

            Console.WriteLine(priorityQueueWithMinHeap.print());

            Console.WriteLine("Value removed from Priority Queue using MinHeap is : " + priorityQueueWithMinHeap.remove());



            Console.ReadLine();
        }