Пример #1
1
void Heap::heapRebuild(int root)
{
//	cout << "rebuilding at " << root << endl;
	// root's left child, if exists, means root has children
	if (root*2 + 1 < size)
	{
		// assume left child is largest, get right child's index
		int largestChildIndex = root * 2 + 1;
		int rightChildIndex = root * 2 + 2;
		
		// check if root has a right child
		if (rightChildIndex < size) // then compare to left
			if (patients[rightChildIndex] > patients[largestChildIndex])
				largestChildIndex = rightChildIndex;
		
		if (patients[root] < patients[largestChildIndex])
		{
			swap(patients[root], patients[largestChildIndex]);
			heapRebuild(largestChildIndex); 
		}
		
	}
}
Пример #2
0
void ArrayMaxHeap<ItemType>::heapCreate() 
{
   for (int index = itemCount / 2; index >= 0; index--)
   {
      heapRebuild(index);  
   }  // end for
}  // end heapCreate
Пример #3
0
void heapSort(std::vector<int> &input, int n){
  //std::cout<<"Entered heapsort\n";
  for(int index=n/2; index>=0; index--){
    heapRebuild(index, input, n);
  }
  int temp = input[0];
  input[0] = input[n-1];
  input[n-1] = temp;
  int heapSize = n-1;
  while(heapSize>1){
    heapRebuild(0, input, heapSize);
    temp = input[0];
    input[0] = input[heapSize-1];
    input[heapSize-1] = temp;
    heapSize--;
  }
}
Пример #4
0
Heap::Heap(Patient array[], int numItems)
{
	size = numItems;
	max = (size%10+1)*10;
	patients = new Patient[max];
	
	for (int i = 0; i < size; i++)
		patients[i] = array[i];	
	
	for (int i = size/2 -1; i >=0; i--)
		heapRebuild(i);
}
Пример #5
0
bool Heap::remove()
{
	bool removed = true;
	if (size == 0)
		removed = false;
	else
	{
		for (int i = 1; i<size; i++)
		{
			patients[i-1] = patients[i];
		}
		size--;
		heapRebuild(0);
	}
	return removed;
}
Пример #6
0
void heapRebuild(int rootIndex, std::vector<int> &HEAP, int HEAP_SIZE){
  //std::cout<<"heapRebuild_start\n";
  if(!isLeaf(rootIndex, HEAP_SIZE)){
    int largerChildIndex=(2*rootIndex)+1;
    if(hasRightChild(rootIndex, HEAP_SIZE)){
	int rightChildIndex=largerChildIndex+1;
	if(HEAP[rightChildIndex]>HEAP[largerChildIndex]){
	  counter++;
	  largerChildIndex=rightChildIndex;
	}
    }
    if(HEAP[rootIndex]<HEAP[largerChildIndex]){
      counter++;
      int temp = HEAP[rootIndex];
      HEAP[rootIndex]=HEAP[largerChildIndex];
      HEAP[largerChildIndex]=temp;
      heapRebuild(largerChildIndex, HEAP, HEAP_SIZE);
    }
  }
  //std::cout<<"heapRebuild_end\n";
}