Binary Heap

最小堆实现与操作
BinaryHeap.h:

#include <iostream>
#include <string>

#define MAX_HEAP_SIZE 101

class BinaryMinHeap {
public:
	BinaryMinHeap(int *heapArray);
	BinaryMinHeap(int size);
	~BinaryMinHeap() {
		delete []heapArray;
	}
	void insert(int value);
	void removeMin();
	int getMin();
	void displayHeapArray();

private:
	int *heapArray;
	int heapSize;
	int heapArraySize;

	void siftUp(int nodeIndex);
	void siftDown(int nodeIndex);

	int getLeftChildIndex(int nodeIndex) {
		return 2*nodeIndex+1;
	}

	int getRightChildIndex(int nodeIndex) {
		return 2*nodeIndex+2;
	}

	int getParentIndex(int nodeIndex) {
		return (nodeIndex-1)/2;
	}
};


BinaryHeap.cpp:

#include "BinaryHeap.h"
#include <stdio.h>

BinaryMinHeap::BinaryMinHeap(int size) {
	heapArray = new int[size];
	heapSize = 0;
	heapArraySize = size;
}

void BinaryMinHeap::insert(int value) {
	if(heapSize == heapArraySize) {
		throw std::string("Insertion Error: The heap is full!");
	} else {
		heapSize++;
		heapArray[heapSize-1] = value;
		siftUp(heapSize-1);
	}
}

/*
 * NON-Recursive Version
 */
void BinaryMinHeap::siftUp(int nodeIndex) {
	int i = nodeIndex;
	int parent = getParentIndex(nodeIndex);
	while(parent >= 0) {
		if(heapArray[parent] > heapArray[i]) {
			int tmp = heapArray[parent];
			heapArray[parent] =  heapArray[i];
			heapArray[i] =  tmp;

			i = parent;
			parent = getParentIndex(parent);
		} else {
			break;
		}
	}
//	fprintf(stderr, "parent = %d\n", parent);
}

void BinaryMinHeap::removeMin() {
	if(heapSize == 0) {
		throw std::string("Remove Error: The heap is empty!");	
	} else {
		heapArray[0] =  heapArray[heapSize - 1];
		heapSize--;
		siftDown(0);
	}
}

void BinaryMinHeap::siftDown(int nodeIndex) {
	int leftIndex = getLeftChildIndex(nodeIndex);
	int rightIndex = getRightChildIndex(nodeIndex);
	int minIndex = (heapArray[leftIndex]>heapArray[rightIndex])?rightIndex:leftIndex;

	while(minIndex <= heapSize) {
		if(heapArray[minIndex] < heapArray[nodeIndex]) {
			int tmp = heapArray[minIndex];
			heapArray[minIndex] =  heapArray[nodeIndex];
			heapArray[nodeIndex] = tmp;
			
			nodeIndex = minIndex;
			leftIndex = getLeftChildIndex(nodeIndex);
			rightIndex = getRightChildIndex(nodeIndex);
			minIndex = (heapArray[leftIndex]>heapArray[rightIndex])?rightIndex:leftIndex;
		} else {
			break;
		}
	}
}

void BinaryMinHeap::displayHeapArray() {
	if(heapSize == 0) {
		throw std::string("Empty Heap!");
	} else {
		std::cout << "\n--------------------\n";
		std::cout << "heap size is:\t" << heapSize << std::endl; 
		for(int i = 0; i < heapSize; i++) {
			std::cout << heapArray[i] << "\t";
		}
		std::cout << "\n--------------------\n";
	}
}

int BinaryMinHeap::getMin() {
	return heapArray[0];
}


Main.cpp:

#include "BinaryHeap.h"

using namespace std;

int main() {
	BinaryMinHeap minHeap(10);

	minHeap.insert(10);
	minHeap.displayHeapArray();
	minHeap.insert(23);
	minHeap.displayHeapArray();
	minHeap.insert(11);
	minHeap.displayHeapArray();
	minHeap.insert(4);
	minHeap.displayHeapArray();
	minHeap.insert(3);
	minHeap.displayHeapArray();

	minHeap.removeMin();
	minHeap.displayHeapArray();
	minHeap.removeMin();
	minHeap.displayHeapArray();
	minHeap.removeMin();
	minHeap.displayHeapArray();
	minHeap.removeMin();
	minHeap.displayHeapArray();
	minHeap.removeMin();
	try {
		minHeap.displayHeapArray();
	} catch(std::string e) {
		cout << e << endl;
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值