Example #1
0
File: heap.cs Project: Squarion/ds
 public static Player heapExtractMax(Player[] a)
 {
     Player max = a[1];
     if (maxHeapSize < 1) {
         Console.WriteLine ("Geen spelers over!");
     } else {
         a [1] = a [maxHeapSize];
         a [maxHeapSize] = null;
         maxHeapSize--;
         maxHeapify (a, 1);
     }
     return max;
 }
Example #2
0
File: heap.cs Project: Squarion/ds
        public static void heapDecreaseKey(Player[] a, int i, int key)
        {
            if (key > a [i].Score) {
                Console.WriteLine ("Nieuwe key groter dan huidige");
            } else {
                a [i].Score = key;
                while (i > 1 && (a [parent (i)].Score > a [i].Score || (a [parent (i)].Score == a [i].Score && a [parent (i)].Number < a [i].Number))) {
                    Player h = a [parent (i)];
                    a [parent (i)] = a [i];
                    a [i] = h;
                    i = parent (i);
                }

                if (i > 1 && a
                    [parent (i)].Score == a [i].Score) {
                    if (a [parent (i)].Number < a [i].Number) {
                        Player h = a [parent (i)];
                        a [parent (i)] = a [i];
                        a [i] = h;
                    }
                }
            }
        }
Example #3
0
File: heap.cs Project: Squarion/ds
 public static void buildMinHeap(Player[] a)
 {
     for (int i = (int)Math.Floor((double) minHeapSize/2); i >= 1; i--) {
         minHeapify (a, i);
     }
 }
Example #4
0
File: heap.cs Project: Squarion/ds
 public static void newPlayer(int number, int score)
 {
     Player p = new Player(number, score);
     directAccess [number] = p;
     maxHeapInsert (maxHeap, p);
     minHeapInsert (minHeap, p);
     print ();
 }
Example #5
0
File: heap.cs Project: Squarion/ds
 public static void minHeapInsert(Player[] a, Player p)
 {
     int key = p.Score;
     p.Score = 1000001;
     minHeapSize++;
     a [minHeapSize] = p;
     heapDecreaseKey (a, minHeapSize, key);
 }
Example #6
0
File: heap.cs Project: Squarion/ds
        public static void minHeapify(Player[] a, int i)
        {
            int l = left(i);
            int r = right(i);
            int largest;
            if (l <= minHeapSize && a [l].Score < a [i].Score) {
                largest = l;
            } else {
                largest = i;
            }
            if (r <= minHeapSize && a[r].Score < a[largest].Score) {
                largest = r;
            }
            if (largest != r && l <= minHeapSize && a [l].Score == a [i].Score) {
                if (a [l].Number > a [i].Number) {
                    largest = l;
                } else {
                    largest = i;
                }

            }
            if (largest != l &&r <= minHeapSize && a [r].Score == a [i].Score) {
                if (a [r].Number > a [i].Number) {
                    largest = r;
                } else {
                    largest = i;
                }
            }
            if (largest != i) {
                Player h = a [largest];
                a [largest] = a [i];
                a [i] = h;
                minHeapify (a, largest);
            }
        }
Example #7
0
File: heap.cs Project: Squarion/ds
 public static void maxHeapInsert(Player[] a, Player p)
 {
     int key = p.Score;
     p.Score = -1000001;
     maxHeapSize++;
     a [maxHeapSize] = p;
     heapIncreaseKey (a, maxHeapSize, key);
 }
Example #8
0
File: heap.cs Project: Squarion/ds
        public static void maxHeapify(Player[] a, int i)
        {
            //Console.WriteLine("max-heapify i: " + i);
            int l = left(i);
            int r = right(i);
            //Console.WriteLine(l + " " + r);
            int largest;
            //Console.WriteLine("l" + a [l].Score + " i" + a [i].Score + " r" + a [r].Score);
            if (l <= maxHeapSize && a [l].Score > a [i].Score) {
                largest = l;
            } else {
                largest = i;
            }
            if (r <= maxHeapSize && a [r].Score > a [largest].Score) {
                largest = r;
            }

            if (largest != r && l <= maxHeapSize && a [l].Score == a [i].Score) {
                if (a [l].Number < a [i].Number) {
                    largest = l;
                } else {
                    largest = i;
                }
            }
            if (largest != l && r <= maxHeapSize && a [r].Score == a [i].Score) {
                if (a [r].Number < a [i].Number) {
                    largest = r;
                } else {
                    largest = i;
                }
            }
            //Console.WriteLine("largest" + largest + " i" + i);
            if (largest != i) {
                Player h = a [largest];
                a [largest] = a [i];
                a [i] = h;
                maxHeapify (a, largest);
            }
        }