用大根堆实现大值优先队列

        今天学了数据结构中的堆,堆分为大根堆和小根堆。大根堆是什么呢?大根堆是指一颗即

是完全二叉树又是大根树的树。那什么是大根树,和完全二叉树呢?去百度google一下就知道了。

小根堆是一个与大根堆类似的概念,联想一下吧。

         大根堆和小根堆有何用呢?用处当然是有的,有人说可以用来实现大值优先队列和小值优

先队列。所以下面就大根堆来实现一个大值优先队列heap。这个队列提供两个操作函数,一个是

push,一个是pop。push是把一个节点入列,pop是把节点出列,也就是删除一个节点。

下面是代码实现

#include <stdlib.h>
#include <stdio.h>

#define MAX_ELEMENTS 200
#define HEAP_FULL(n) (n == MAX_ELEMENTS - 1)
#define HEAP_EMPTY(n) (!n)

typedef struct
{
	int key;
	char c;
	/* add other fields */
} element;

element heap[MAX_ELEMENTS];

void push(element item, int *n)
{
	int i = 0;

	if(HEAP_FULL(*n))
	{
		fprintf(stderr, "The heap is full.\n");
		exit(0);
	}
	i = ++(*n);
	while(i != 1 && item.key > heap[i / 2].key)
	{
		heap[i] = heap[i / 2];
		i /= 2;
	}
	heap[i] = item;
}

// delete element with the highest key from the heap
element pop(int *n)
{
	int parent = 0, child = 0;
	element item, temp;

	if(HEAP_EMPTY(*n))
	{
		fprintf(stderr, "the heap is empty.\n");
		exit(0);
	}
	// save value of the element with the highest key
	item = heap[1];
	// use last element in heap to adjust heap parent
	temp = heap[(*n)--];
	parent = 1;
	child = 2;
	while(child <= *n)
	{
		// find the larger child of the current parent
		if(child < *n && heap[child].key < heap[child + 1].key)
			child++;
		if(temp.key >= heap[child].key)
			break;

		// move to the next lower level
		heap[parent] = heap[child];
		parent = child;
		child *= 2;
	}
	heap[parent] = temp;

	return item;
}

int main()
{
	int n = 0; // node counts
	int num[] = {2, 5, 18, 7, 9, 4, 3};
	element item;

	// push all node into queue
	for(int i = 0; i < sizeof(num) / sizeof(int); i++)
	{
		item.key = num[i];
		item.c = 'a';
		push(item, &n);
	}

	// pop all node out of queue
	while(n > 0)
	{
		item = pop(&n);
		printf("item.key = %d, ch = %c\n", item.key, item.c);
	}
	getchar();
	return 0;
}
大家可以把代码拷贝调试看看,会发现在内存heap中不是按key值从大到小排列的,但pop打印的数值是从大到小,很神奇吧。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
大根堆(Max Heap)是一种特殊的完全二叉树,其中每个节点的值都大于或等于其子节点的值,通常用于实现优先队列,因为最小元素总是在根部。在C++中,标准库`std::priority_queue`就是基于最大堆的数据结构。 以下是使用C++实现一个简单的最大堆优先队列的基本步骤: ```cpp #include <queue> #include <vector> // 使用vector作为底层存储,因为它的内部是动态调整大小的,适合堆操作 template<typename T> class MaxHeap { private: std::vector<T> heap; int size; public: // 构造函数,初始化一个空的最大堆 MaxHeap() : size(0) {} // 插入元素并调整堆结构 void push(T value) { heap.push_back(value); siftUp(size - 1); size++; } // 删除并返回堆顶元素(最大值) T pop() { if (size == 0) return T(); T max_val = heap; heap = heap[size - 1]; // 将最后一个元素移到顶部 heap.pop_back(); // 减小堆大小 siftDown(0); // 调整堆结构 return max_val; } // 其他辅助方法 bool empty() const { return size == 0; } int size() const { return size; } private: // siftUp方法用于向上调整堆 void siftUp(int i) { while (i > 0 && heap[parent(i)] < heap[i]) { swap(heap[i], heap[parent(i)]); i = parent(i); } } // siftDown方法用于向下调整堆 void siftDown(int i) { while (true) { int left = leftChild(i), right = rightChild(i); int largest = i; if (left < size && heap[left] > heap[largest]) largest = left; if (right < size && heap[right] > heap[largest]) largest = right; if (largest != i) { swap(heap[i], heap[largest]); i = largest; } else { break; } } } // 辅助函数获取父节点、左孩子和右孩子的索引 int parent(int i) const { return (i - 1) / 2; } int leftChild(int i) const { return 2 * i + 1; } int rightChild(int i) const { return 2 * i + 2; } }; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值