Advertisement
Margoshinka

archivator

Oct 11th, 2023
709
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Бинарная_куча
  8. {
  9.     public class PriorityQueue<T> where T : IComparable<T>
  10.     {
  11.         private List<T> heap;
  12.  
  13.         public PriorityQueue()
  14.         {
  15.             heap = new List<T>();
  16.         }
  17.  
  18.         public PriorityQueue(IEnumerable<T> collection)
  19.         {
  20.             heap = new List<T>(collection);
  21.             BuildHeap();
  22.         }
  23.  
  24.         public void Enqueue(T item)
  25.         {
  26.             heap.Add(item);
  27.             HeapifyUp(heap.Count - 1);
  28.         }
  29.  
  30.         public T Dequeue()
  31.         {
  32.             if (IsEmpty())
  33.             {
  34.                 throw new InvalidOperationException("Priority queue is empty.");
  35.             }
  36.  
  37.             T minItem = heap[0];
  38.             int lastIndex = heap.Count - 1;
  39.             heap[0] = heap[lastIndex];
  40.             heap.RemoveAt(lastIndex);
  41.             HeapifyDown(0);
  42.  
  43.             return minItem;
  44.         }
  45.  
  46.         public T Peek()
  47.         {
  48.             if (IsEmpty())
  49.             {
  50.                 throw new InvalidOperationException("Priority queue is empty.");
  51.             }
  52.  
  53.             return heap[0];
  54.         }
  55.  
  56.         public bool IsEmpty()
  57.         {
  58.             return heap.Count == 0;
  59.         }
  60.  
  61.         private void HeapifyUp(int index)
  62.         {
  63.             int parentIndex = (index - 1) / 2;
  64.             while (index > 0 && heap[index].CompareTo(heap[parentIndex]) < 0)
  65.             {
  66.                 Swap(index, parentIndex);
  67.                 index = parentIndex;
  68.                 parentIndex = (index - 1) / 2;
  69.             }
  70.         }
  71.  
  72.         private void HeapifyDown(int index)
  73.         {
  74.             int leftChildIndex = 2 * index + 1;
  75.             int rightChildIndex = 2 * index + 2;
  76.             int smallestIndex = index;
  77.  
  78.             if (leftChildIndex < heap.Count && heap[leftChildIndex].CompareTo(heap[smallestIndex]) < 0)
  79.             {
  80.                 smallestIndex = leftChildIndex;
  81.             }
  82.  
  83.             if (rightChildIndex < heap.Count && heap[rightChildIndex].CompareTo(heap[smallestIndex]) < 0)
  84.             {
  85.                 smallestIndex = rightChildIndex;
  86.             }
  87.  
  88.             if (smallestIndex != index)
  89.             {
  90.                 Swap(index, smallestIndex);
  91.                 HeapifyDown(smallestIndex);
  92.             }
  93.         }
  94.  
  95.         private void Swap(int i, int j)
  96.         {
  97.             T temp = heap[i];
  98.             heap[i] = heap[j];
  99.             heap[j] = temp;
  100.         }
  101.  
  102.         private void BuildHeap()
  103.         {
  104.             int lastNonLeafIndex = (heap.Count - 2) / 2;
  105.             for (int i = lastNonLeafIndex; i >= 0; i--)
  106.             {
  107.                 HeapifyDown(i);
  108.             }
  109.         }
  110.         public void PrintHeapTree()
  111.         {
  112.             PrintHeapTreeRecursive(0, 0);
  113.         }
  114.         private void PrintHeapTreeRecursive(int index, int level)
  115.         {
  116.             if (index < heap.Count)
  117.             {
  118.                 PrintHeapTreeRecursive(2 * index + 2, level + 1);
  119.  
  120.  
  121.                 for (int i = 0; i < level; i++)
  122.                 {
  123.                     Console.Write("    ");
  124.                 }
  125.  
  126.  
  127.                 Console.WriteLine(heap[index]);
  128.  
  129.                 PrintHeapTreeRecursive(2 * index + 1, level + 1);
  130.             }
  131.         }
  132.  
  133.     }
  134.     public static class TestDataGenerator
  135.     {
  136.         private static readonly Random random = new Random();
  137.  
  138.         public static int[] GenerateRandomArray(int size, int minValue, int maxValue)
  139.         {
  140.             int[] array = new int[size];
  141.             for (int i = 0; i < size; i++)
  142.             {
  143.                 array[i] = random.Next(minValue, maxValue + 1);
  144.             }
  145.             return array;
  146.         }
  147.         private const string AllowedChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  148.  
  149.         public static string[] GenerateRandomStringArray(int size, int stringLength)
  150.         {
  151.             string[] array = new string[size];
  152.             for (int i = 0; i < size; i++)
  153.             {
  154.                 array[i] = GenerateRandomString(stringLength);
  155.             }
  156.             return array;
  157.         }
  158.  
  159.         public static string GenerateRandomString(int length)
  160.         {
  161.             char[] result = new char[length];
  162.             for (int i = 0; i < length; i++)
  163.             {
  164.                 result[i] = AllowedChars[random.Next(0, AllowedChars.Length)];
  165.             }
  166.             return new string(result);
  167.         }
  168.     }
  169.     class Program
  170.     {
  171.         static void Main()
  172.         {
  173.            
  174.  
  175.             int[] array = { 3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5 };
  176.             var priorityQueue = new PriorityQueue<int>(array);
  177.  
  178.             Console.WriteLine("Priority Queue:");
  179.             priorityQueue.PrintHeapTree();
  180.             Console.WriteLine();
  181.             int top = priorityQueue.Peek();
  182.             Console.WriteLine("Top: " + top);
  183.             while (!priorityQueue.IsEmpty())
  184.             {
  185.                 Random rnd = new Random();
  186.                 if(rnd.Next(0, 3)==1)
  187.                 {
  188.                     int item = rnd.Next(-100, 100);
  189.                     priorityQueue.Enqueue(item);
  190.                     Console.WriteLine("Insert: " + item + " ");
  191.                     priorityQueue.PrintHeapTree();
  192.                     Console.WriteLine();
  193.  
  194.                 }
  195.  
  196.                 int min = priorityQueue.Dequeue();
  197.                 Console.WriteLine("Delete: " + min + " ");
  198.                 priorityQueue.PrintHeapTree();
  199.                 Console.WriteLine();
  200.             }
  201.  
  202.             Console.WriteLine();
  203.  
  204.  
  205.             int[] testData = TestDataGenerator.GenerateRandomArray(30, -100, 100);
  206.             var heap = new PriorityQueue<int>(testData);
  207.             Console.WriteLine("Priority Queue:");
  208.             heap.PrintHeapTree();
  209.             Console.WriteLine();
  210.             top = heap.Peek();
  211.             Console.WriteLine("Top: " + top);
  212.             while (!heap.IsEmpty())
  213.             {
  214.                 Random rnd = new Random();
  215.                 if (rnd.Next(0, 3) == 1)
  216.                 {
  217.                     int item = rnd.Next(-100, 100);
  218.                     heap.Enqueue(item);
  219.                     Console.WriteLine("Insert: " + item + " ");
  220.                     heap.PrintHeapTree();
  221.                     Console.WriteLine();
  222.  
  223.                 }
  224.                 int min = heap.Dequeue();
  225.                 Console.WriteLine("Delete: "+min + " ");
  226.                 heap.PrintHeapTree();
  227.                 Console.WriteLine();
  228.             }
  229.  
  230.             Console.WriteLine();
  231.  
  232.             string[] stringTestData = TestDataGenerator.GenerateRandomStringArray(10, 10);
  233.             var stringHeap = new PriorityQueue<string>(stringTestData);
  234.             Console.WriteLine("Priority Queue:");
  235.             stringHeap.PrintHeapTree();
  236.             Console.WriteLine();
  237.             string stop = stringHeap.Peek();
  238.             Console.WriteLine("Top: " + stop);
  239.             while (!stringHeap.IsEmpty())
  240.             {
  241.                 Random rnd = new Random();
  242.                 if (rnd.Next(0, 3) == 1)
  243.                 {
  244.                     string item = TestDataGenerator.GenerateRandomString(10);
  245.                     stringHeap.Enqueue(item);
  246.                     Console.WriteLine("Insert: " + item + " ");
  247.                     stringHeap.PrintHeapTree();
  248.                     Console.WriteLine();
  249.  
  250.                 }
  251.                 string min = stringHeap.Dequeue();
  252.                 Console.WriteLine("Delete: " + min + " ");
  253.                 stringHeap.PrintHeapTree();
  254.                 Console.WriteLine();
  255.             }
  256.  
  257.         }
  258.     }
  259.  
  260.  
  261. }
  262.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement