示例#1
0
        public void MaxHeapify(Heap <int> A, int i)
        {
            int largest = i;

            int left = A.Left(i);

            if (left <= A.HeapSize && A[largest] < A[left])
            {
                largest = left;
            }

            int right = A.Right(i);

            if (right <= A.HeapSize && A[largest] < A[right])
            {
                largest = right;
            }

            if (largest != i)
            {
                int temp = A[i];
                A[i]       = A[largest];
                A[largest] = temp;
                MaxHeapify(A, largest);
            }
        }
示例#2
0
        public void MaxHeapifyLoop(Heap <int> A, int i)
        {
            int largest = i;

            while (!A.IsLeaf(largest))
            {
                int left = A.Left(i);
                if (left <= A.HeapSize && A[largest] < A[left])
                {
                    largest = left;
                }

                int right = A.Right(i);
                if (right <= A.HeapSize && A[largest] < A[right])
                {
                    largest = right;
                }

                if (largest != i)
                {
                    int temp = A[i];
                    A[i]       = A[largest];
                    A[largest] = temp;
                }
                else
                {
                    break;
                }
            }
        }