C++二叉堆的实现

头文件 BinaryHeap.h

#ifndef __BinaryHeap__
#define __BinaryHeap__

#include <iostream>

class BinaryHeap
{
public:
	typedef struct node
	{
		int startIndex;
		int endIndex;
		int weight;
	} Node;
private:
	Node * m_heap;
	int m_size;
	int m_currentSize;
	void m_percolateUp (int index);
	void m_percolateDown (int index);
public:
	BinaryHeap (int size = 0);
	~BinaryHeap (void);
	bool isEmpty (void);
	bool isFull (void);
	bool insert (int startIndex, int endIndex, int weight);
	bool deleteMin (Node * const pNode);
};

#endif /* defined(__BinaryHeap__) */


实现文件 BinaryHeap.cpp

#include "BinaryHeap.h"

//	Private methods:
//	---------------------
void BinaryHeap ::m_percolateUp (int index)
{
	Node temp = m_heap[index];
	size_t i = index;
	size_t parent = (i - 1) / 2;
	while (i != 0)
	{
		if (temp.weight < m_heap[parent].weight)
		{
			m_heap[i] = m_heap[parent];
			i = parent;
			parent = (i - 1) / 2;
		}
		else
		{
			m_heap[i] = temp;
			break;
		}
	}
	if (0 == i)
	{
		m_heap[0] = temp;
	}
}
//	---------------------

//	---------------------
void BinaryHeap ::m_percolateDown (int index)
{
	Node temp = m_heap[index];
	int i = index;
	int child = i * 2 + 1;
	while (child < m_currentSize)
	{
		if (child != m_currentSize - 1 && m_heap[child + 1].weight < m_heap[child].weight)
			++child;
		if (temp.weight > m_heap[child].weight)
		{
			m_heap[i] = m_heap[child];
			i = child;
			child = i * 2 + 1;
		}
		else
		{
			m_heap[i] = temp;
			break;
		}
	}
	if (child >= m_currentSize)
	{
		m_heap[i] = temp;
	}
}
//	---------------------

//	Public methods:
//	---------------------
BinaryHeap ::BinaryHeap (int size)
{
	if (size <= 0)
	{
		std ::cerr << "Wrong size." << std ::endl;
		return;
	}
	m_heap = new Node[size];
	if (NULL == m_heap)
	{
		std ::cerr << "Out of space." << std ::endl;
		return;
	}
	m_size = size;
	m_currentSize = 0;
}
//	---------------------

//	---------------------
BinaryHeap ::~BinaryHeap (void)
{
	delete []m_heap;
}
//	---------------------

//	---------------------
bool BinaryHeap ::isEmpty (void)
{
	if (0 == m_currentSize)
		return true;
	else
		return false;
}
//	---------------------

//	---------------------
bool BinaryHeap ::isFull (void)
{
	if (m_currentSize == m_size)
		return true;
	else
		return false;
}
//	---------------------

//	---------------------
bool BinaryHeap ::insert (int startIndex, int endIndex, int weight)
{
	if (isFull())
	{
		std ::cerr << "Heap is full already." << std ::endl;
		return false;
	}
	if (startIndex >= m_size || startIndex < 0 ||
		endIndex >= m_size || endIndex < 0)
	{
		std ::cerr << "Wrong start index or end index." << std ::endl;
		return false;
	}
	m_heap[m_currentSize].startIndex = startIndex;
	m_heap[m_currentSize].endIndex = endIndex;
	m_heap[m_currentSize].weight = weight;
	m_percolateUp(m_currentSize);
	++m_currentSize;
    
	return true;
}
//	---------------------

//	---------------------
bool BinaryHeap ::deleteMin (Node * const pNode)
{
	if (isEmpty())
	{
		std ::cerr << "Heap is empty already." << std ::endl;
		return false;
	}
	*pNode = m_heap[0];
	m_heap[0] = m_heap[--m_currentSize];
	m_percolateDown(0);
	
	return true;
}
//	---------------------

转自:http://blog.csdn.net/golden_shadow/article/details/6730500


参考:http://blog.csdn.net/listeningsea/article/details/7718484

http://blog.chinaunix.net/uid-20937170-id-3331263.html

http://en.wikipedia.org/wiki/Binary_heap

http://baike.baidu.com/link?url=325xU5U7MrGN46ESO3LqK7tJ-6mtB3EqAyyxHnyKo8vl8qX8xzTKWp73oqwTHHT5

http://blog.csdn.net/morewindows/article/details/6976468


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值