Advertisement
Guest User

eksdee

a guest
May 29th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.29 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 Clean_AS
  8. {
  9.     class MaxHeap
  10.     {
  11.         private Node[] maxHeapArray;
  12.         private int maxHeapSize;
  13.         private int currentSize;
  14.  
  15.         //Indekset for root er 1, f.eks. N=1. Venstre leaf af root vil så blive gemt på placeringen 2*N = 2. Højre leaf vil blive gemt på 2*N + 1 = 3. Fortsat på næste linje.
  16.         //Indekset for venstre leaf af root fra eksemplet før vil være 2, så når vi kigger på leafs fra denne er N = 2. Indekset for venstre leaf fra denne vil så være 2*N igen. denne gang bliver det så 4 osv. osv.
  17.         public MaxHeap(int maxHeapSize)
  18.         {
  19.             this.maxHeapSize = maxHeapSize;
  20.             currentSize = 0;
  21.             maxHeapArray = new Node[maxHeapSize];
  22.         }
  23.  
  24.         public bool Insert(ServiceDescription desc)
  25.         {
  26.             if (currentSize == maxHeapSize)
  27.                 return false;
  28.             Node node = new Node(desc);
  29.             maxHeapArray[currentSize] = node;
  30.             HeapifyUp(currentSize++);
  31.             return true;
  32.         }
  33.         public void HeapifyUp(int index)
  34.         {
  35.             int parent = (index - 1) / 2;
  36.             Node floor = maxHeapArray[index];
  37.             while(index > 0 && maxHeapArray[parent].data < floor.data)
  38.             {
  39.                 maxHeapArray[index] = maxHeapArray[parent];
  40.                 index = parent;
  41.                 parent = (parent - 1) / 2;
  42.             }
  43.             maxHeapArray[index] = floor;
  44.         }
  45.         public Node RemoveRoot()
  46.         {
  47.             Node root = maxHeapArray[0];
  48.             maxHeapArray[0] = maxHeapArray[--currentSize];
  49.             HeapifyDown(0);
  50.             return root;
  51.         }
  52.         public void HeapifyDown(int index)
  53.         {
  54.             int largerLeaf;
  55.             Node roof = maxHeapArray[index];
  56.             while(index < currentSize / 2)
  57.             {
  58.                 int leftLeaf = 2 * index + 1;
  59.                 int rightLeaf = leftLeaf + 1;
  60.                 if (rightLeaf < currentSize && maxHeapArray[leftLeaf].data < maxHeapArray[rightLeaf].data && maxHeapArray[index].data < maxHeapArray[rightLeaf].data)
  61.                     largerLeaf = rightLeaf;
  62.                 else
  63.                     largerLeaf = leftLeaf;
  64.                 if (roof.data >= maxHeapArray[largerLeaf].data)
  65.                     break;
  66.                 maxHeapArray[index] = maxHeapArray[largerLeaf];
  67.                 index = largerLeaf;
  68.             }
  69.             maxHeapArray[index] = roof;
  70.         }
  71.  
  72.         public void DisplayHeapInConsole()
  73.         {
  74.             for (int i = 0; i < currentSize; i++)
  75.             {
  76.                 if (maxHeapArray[i] != null)
  77.                     Console.Write(maxHeapArray[i].data + " ");
  78.                 else
  79.                     Console.Write("--- ");
  80.             }
  81.         }
  82.            
  83.        
  84.     }
  85. }
  86. using System;
  87. using System.Collections.Generic;
  88. using System.Linq;
  89. using System.Text;
  90. using System.Threading.Tasks;
  91.  
  92. namespace Clean_AS
  93. {
  94.     class Node
  95.     {
  96.         private ServiceDescription desc;
  97.         public double data;
  98.         public Node(ServiceDescription desc)
  99.         {
  100.             this.desc = desc;
  101.             this.data = desc.GetTimeConsumption();
  102.         }
  103.  
  104.         public ServiceDescription GetNodeServiceDescription()
  105.         {
  106.             return desc;
  107.         }
  108.     }
  109. }
  110. using System;
  111. using System.Collections.Generic;
  112. using System.Linq;
  113. using System.Threading.Tasks;
  114. using System.Windows.Forms;
  115.  
  116. namespace Clean_AS
  117. {
  118.     static class Program
  119.     {
  120.         /// <summary>
  121.         /// The main entry point for the application.
  122.         /// </summary>
  123.         [STAThread]
  124.         static void Main()
  125.         {
  126.             //Application.EnableVisualStyles();
  127.             //Application.SetCompatibleTextRenderingDefault(false);
  128.             //Application.Run(new Form1());
  129.             MaxHeap maxheap = new MaxHeap(10);
  130.  
  131.             Controller controller = Controller.GetInstance();
  132.             var list = controller.GetServiceList();
  133.             foreach(ServiceDescription desc in list)
  134.             {
  135.                 maxheap.Insert(desc);
  136.             }
  137.             maxheap.DisplayHeapInConsole();
  138.         }
  139.     }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement