Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Бинарная_куча
- {
- public class PriorityQueue<T> where T : IComparable<T>
- {
- private List<T> heap;
- public PriorityQueue()
- {
- heap = new List<T>();
- }
- public PriorityQueue(IEnumerable<T> collection)
- {
- heap = new List<T>(collection);
- BuildHeap();
- }
- public void Enqueue(T item)
- {
- heap.Add(item);
- HeapifyUp(heap.Count - 1);
- }
- public T Dequeue()
- {
- if (IsEmpty())
- {
- throw new InvalidOperationException("Priority queue is empty.");
- }
- T minItem = heap[0];
- int lastIndex = heap.Count - 1;
- heap[0] = heap[lastIndex];
- heap.RemoveAt(lastIndex);
- HeapifyDown(0);
- return minItem;
- }
- public T Peek()
- {
- if (IsEmpty())
- {
- throw new InvalidOperationException("Priority queue is empty.");
- }
- return heap[0];
- }
- public bool IsEmpty()
- {
- return heap.Count == 0;
- }
- private void HeapifyUp(int index)
- {
- int parentIndex = (index - 1) / 2;
- while (index > 0 && heap[index].CompareTo(heap[parentIndex]) < 0)
- {
- Swap(index, parentIndex);
- index = parentIndex;
- parentIndex = (index - 1) / 2;
- }
- }
- private void HeapifyDown(int index)
- {
- int leftChildIndex = 2 * index + 1;
- int rightChildIndex = 2 * index + 2;
- int smallestIndex = index;
- if (leftChildIndex < heap.Count && heap[leftChildIndex].CompareTo(heap[smallestIndex]) < 0)
- {
- smallestIndex = leftChildIndex;
- }
- if (rightChildIndex < heap.Count && heap[rightChildIndex].CompareTo(heap[smallestIndex]) < 0)
- {
- smallestIndex = rightChildIndex;
- }
- if (smallestIndex != index)
- {
- Swap(index, smallestIndex);
- HeapifyDown(smallestIndex);
- }
- }
- private void Swap(int i, int j)
- {
- T temp = heap[i];
- heap[i] = heap[j];
- heap[j] = temp;
- }
- private void BuildHeap()
- {
- int lastNonLeafIndex = (heap.Count - 2) / 2;
- for (int i = lastNonLeafIndex; i >= 0; i--)
- {
- HeapifyDown(i);
- }
- }
- public void PrintHeapTree()
- {
- PrintHeapTreeRecursive(0, 0);
- }
- private void PrintHeapTreeRecursive(int index, int level)
- {
- if (index < heap.Count)
- {
- PrintHeapTreeRecursive(2 * index + 2, level + 1);
- for (int i = 0; i < level; i++)
- {
- Console.Write(" ");
- }
- Console.WriteLine(heap[index]);
- PrintHeapTreeRecursive(2 * index + 1, level + 1);
- }
- }
- }
- public static class TestDataGenerator
- {
- private static readonly Random random = new Random();
- public static int[] GenerateRandomArray(int size, int minValue, int maxValue)
- {
- int[] array = new int[size];
- for (int i = 0; i < size; i++)
- {
- array[i] = random.Next(minValue, maxValue + 1);
- }
- return array;
- }
- private const string AllowedChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
- public static string[] GenerateRandomStringArray(int size, int stringLength)
- {
- string[] array = new string[size];
- for (int i = 0; i < size; i++)
- {
- array[i] = GenerateRandomString(stringLength);
- }
- return array;
- }
- public static string GenerateRandomString(int length)
- {
- char[] result = new char[length];
- for (int i = 0; i < length; i++)
- {
- result[i] = AllowedChars[random.Next(0, AllowedChars.Length)];
- }
- return new string(result);
- }
- }
- class Program
- {
- static void Main()
- {
- int[] array = { 3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5 };
- var priorityQueue = new PriorityQueue<int>(array);
- Console.WriteLine("Priority Queue:");
- priorityQueue.PrintHeapTree();
- Console.WriteLine();
- int top = priorityQueue.Peek();
- Console.WriteLine("Top: " + top);
- while (!priorityQueue.IsEmpty())
- {
- Random rnd = new Random();
- if(rnd.Next(0, 3)==1)
- {
- int item = rnd.Next(-100, 100);
- priorityQueue.Enqueue(item);
- Console.WriteLine("Insert: " + item + " ");
- priorityQueue.PrintHeapTree();
- Console.WriteLine();
- }
- int min = priorityQueue.Dequeue();
- Console.WriteLine("Delete: " + min + " ");
- priorityQueue.PrintHeapTree();
- Console.WriteLine();
- }
- Console.WriteLine();
- int[] testData = TestDataGenerator.GenerateRandomArray(30, -100, 100);
- var heap = new PriorityQueue<int>(testData);
- Console.WriteLine("Priority Queue:");
- heap.PrintHeapTree();
- Console.WriteLine();
- top = heap.Peek();
- Console.WriteLine("Top: " + top);
- while (!heap.IsEmpty())
- {
- Random rnd = new Random();
- if (rnd.Next(0, 3) == 1)
- {
- int item = rnd.Next(-100, 100);
- heap.Enqueue(item);
- Console.WriteLine("Insert: " + item + " ");
- heap.PrintHeapTree();
- Console.WriteLine();
- }
- int min = heap.Dequeue();
- Console.WriteLine("Delete: "+min + " ");
- heap.PrintHeapTree();
- Console.WriteLine();
- }
- Console.WriteLine();
- string[] stringTestData = TestDataGenerator.GenerateRandomStringArray(10, 10);
- var stringHeap = new PriorityQueue<string>(stringTestData);
- Console.WriteLine("Priority Queue:");
- stringHeap.PrintHeapTree();
- Console.WriteLine();
- string stop = stringHeap.Peek();
- Console.WriteLine("Top: " + stop);
- while (!stringHeap.IsEmpty())
- {
- Random rnd = new Random();
- if (rnd.Next(0, 3) == 1)
- {
- string item = TestDataGenerator.GenerateRandomString(10);
- stringHeap.Enqueue(item);
- Console.WriteLine("Insert: " + item + " ");
- stringHeap.PrintHeapTree();
- Console.WriteLine();
- }
- string min = stringHeap.Dequeue();
- Console.WriteLine("Delete: " + min + " ");
- stringHeap.PrintHeapTree();
- Console.WriteLine();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement