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 Clean_AS
- {
- class MaxHeap
- {
- private Node[] maxHeapArray;
- private int maxHeapSize;
- private int currentSize;
- //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.
- //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.
- public MaxHeap(int maxHeapSize)
- {
- this.maxHeapSize = maxHeapSize;
- currentSize = 0;
- maxHeapArray = new Node[maxHeapSize];
- }
- public bool Insert(ServiceDescription desc)
- {
- if (currentSize == maxHeapSize)
- return false;
- Node node = new Node(desc);
- maxHeapArray[currentSize] = node;
- HeapifyUp(currentSize++);
- return true;
- }
- public void HeapifyUp(int index)
- {
- int parent = (index - 1) / 2;
- Node floor = maxHeapArray[index];
- while(index > 0 && maxHeapArray[parent].data < floor.data)
- {
- maxHeapArray[index] = maxHeapArray[parent];
- index = parent;
- parent = (parent - 1) / 2;
- }
- maxHeapArray[index] = floor;
- }
- public Node RemoveRoot()
- {
- Node root = maxHeapArray[0];
- maxHeapArray[0] = maxHeapArray[--currentSize];
- HeapifyDown(0);
- return root;
- }
- public void HeapifyDown(int index)
- {
- int largerLeaf;
- Node roof = maxHeapArray[index];
- while(index < currentSize / 2)
- {
- int leftLeaf = 2 * index + 1;
- int rightLeaf = leftLeaf + 1;
- if (rightLeaf < currentSize && maxHeapArray[leftLeaf].data < maxHeapArray[rightLeaf].data && maxHeapArray[index].data < maxHeapArray[rightLeaf].data)
- largerLeaf = rightLeaf;
- else
- largerLeaf = leftLeaf;
- if (roof.data >= maxHeapArray[largerLeaf].data)
- break;
- maxHeapArray[index] = maxHeapArray[largerLeaf];
- index = largerLeaf;
- }
- maxHeapArray[index] = roof;
- }
- public void DisplayHeapInConsole()
- {
- for (int i = 0; i < currentSize; i++)
- {
- if (maxHeapArray[i] != null)
- Console.Write(maxHeapArray[i].data + " ");
- else
- Console.Write("--- ");
- }
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Clean_AS
- {
- class Node
- {
- private ServiceDescription desc;
- public double data;
- public Node(ServiceDescription desc)
- {
- this.desc = desc;
- this.data = desc.GetTimeConsumption();
- }
- public ServiceDescription GetNodeServiceDescription()
- {
- return desc;
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace Clean_AS
- {
- static class Program
- {
- /// <summary>
- /// The main entry point for the application.
- /// </summary>
- [STAThread]
- static void Main()
- {
- //Application.EnableVisualStyles();
- //Application.SetCompatibleTextRenderingDefault(false);
- //Application.Run(new Form1());
- MaxHeap maxheap = new MaxHeap(10);
- Controller controller = Controller.GetInstance();
- var list = controller.GetServiceList();
- foreach(ServiceDescription desc in list)
- {
- maxheap.Insert(desc);
- }
- maxheap.DisplayHeapInConsole();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement