Heap(栈、priority_queue、优先队列)原理、C++实现及应用

原理

定义

A heap is a specialized tree-based data structure which is essentially an almost complete tree that satisfies the heap property: in a max heap, for any given node C, if P is a parent node of C, then the key (the value) of P is greater than or equal to the key of C. In a min heap, the key of P is less than or equal to the key of C The node at the “top” of the heap (with no parents) is called the root node.

与优先队列(priority_queue)的关系

The heap is one maximally efficient implementation of an abstract data type called a priority queue, and in fact, priority queues are often referred to as “heaps”, regardless of how they may be implemented. In a heap, the highest (or lowest) priority element is always stored at the root.

堆不是有序的数据结构

However, a heap is not a sorted structure; it can be regarded as being partially ordered. A heap is a useful data structure when it is necessary to repeatedly remove the object with the highest (or lowest) priority.

时间复杂度

一般来说,
pop_root/弹出顶元素,时间复杂度在 O ( l o g 2 ( n ) ) O(log_2(n)) O(log2(n))
bubbuleDown/下溯/降低优先级,时间复杂度在 O ( l o g 2 ( n ) ) O(log_2(n)) O(log2(n))
insert/插入元素/上溯,时间复杂度在 O ( l o g 2 ( n ) ) O(log_2(n)) O(log2(n))

基本实现

头文件
#include <stdio.h>
#include <tchar.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<functional>

using namespace std;

template <class T>
class myHeap {
private:
	vector<T> list;

	void bubbleUp();
	void bubbleDown();
	void swap(int child, int parent);
	int getLeftChild(int parent);
	int getRightChild(int parent);
	int getParent(int child);
	bool isRoot(int node);
	bool isLeaf(int node);
	int getMinChild(int node);
public:
	myHeap();
	void insert(T);
	T popRoot();
	int getSize();

};
注释
  • insert插入一个元素
  • popRoot弹出root,即优先级最高(低)的元素
  • bubbleUp上溯,被insert调用
  • bubbleDown下溯,被popRoot调用
  • swap交换节点(node)
函数定义
template <class T>
myHeap<T> ::myHeap() {
	// you should never access to the first element
	list.push_back(INT_MAX);
}

template <class T>
int myHeap<T> ::getSize() {
	return list.size() - 1;
}

template <class T>
void myHeap<T>::swap(int child, int parent) {
	T temp;
	temp = list[child];
	list[child] = list[parent];
	list[parent] = temp;
}

template <class T>
int myHeap<T> ::getParent(int child) {
		return child / 2;
}

template<class T>
inline bool myHeap<T>::isRoot(int node)
{
	return node == 1;
}

template<class T>
inline bool myHeap<T>::isLeaf(int node)
{
	return 2*node > getSize();
}

template<class T>
inline int myHeap<T>::getMinChild(int node)
{
	// comp
	if (list[getLeftChild(node)] <= list[getRightChild(node)])
		return getLeftChild(node);
	else
		return getRightChild(node);
}

template <class T>
int myHeap<T> ::getLeftChild(int parent) {
	return 2 * parent;
}

template <class T>
int myHeap<T> ::getRightChild(int parent) {
	return 2 * parent + 1;
}

template <class T>
void myHeap<T> ::insert(T value) {

	list.push_back(value);
	bubbleUp();

}

template <class T>
void myHeap <T>::bubbleUp() {
	// getSize() already references to the last element
	int child = getSize();
	int parent = getParent(child);
	
	// comp
	while (!isRoot(child) && list[child] < list[parent])
	{
		swap(child, parent);
		child = parent;
		parent = getParent(child);
	}

}


template <class T>
T myHeap<T> ::popRoot() {
	int lastleaf = getSize();
	swap(lastleaf, 1);
	T value = list.back();
	list.pop_back();
	bubbleDown();
	return value;
}

myHeap和C++标准库里的Heap有类似的行为

int main()
{
	std::cout << "Min heap:\n";

	std::vector<int> v1{ 3, 2, 4, 1, 5, 9 };

	std::cout << "initially, v1: ";
	for (auto i : v1) std::cout << i << ' ';
	std::cout << '\n';

	std::make_heap(v1.begin(), v1.end(), std::greater<>{});

	std::cout << "after make_heap, v1: ";
	for (auto i : v1) std::cout << i << ' ';
	std::cout << '\n';

	std::pop_heap(v1.begin(), v1.end(), std::greater<>{});

	std::cout << "after pop_heap, v1: ";
	for (auto i : v1) std::cout << i << ' ';
	std::cout << '\n';

	auto top1 = v1.back();
	v1.pop_back();
	std::cout << "former top element: " << top1 << '\n';

	std::cout << "after removing the former top element, v1: ";
	for (auto i : v1) std::cout << i << ' ';
	std::cout << '\n';

	myHeap<int> h;
	
	h.insert(3);
	h.insert(2);
	h.insert(4);
	h.insert(1);
	h.insert(5);
	h.insert(9);

	cout << "popRoot" << h.popRoot() << endl;
}

应用

Top K Frequent Elements

题目来自LeetCode全球版

Given a non-empty array of integers, return the k most frequent elements.
Example 1:
Input: nums = [1,1,1,2,2,3], k = 2
Output: [1,2]
Example 2:
Input: nums = [1], k = 1
Output: [1]

题解

typedef pair<int, int>PAIR2;
bool comp2(const PAIR2& p1, const PAIR2& p2) {
	return p1.second < p2.second;
}
vector<int> topKFrequent(vector<int>& nums, int k) {
	unordered_map<int, int> m;

	for (int elem : nums) {
		++m[elem];
	}

	vector<PAIR2> v(m.begin(), m.end());
	make_heap(v.begin(), v.end(), comp2);
	vector<int> res;
	for (int i = 0; i < k; i++) {
		res.push_back(v.front().first);
		pop_heap(v.begin(), v.end(), comp2);
		v.pop_back();
	}
	return res;

}

Find Median from Data Stream

Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value.
For example,
[2,3,4], the median is 3
[2,3], the median is (2 + 3) / 2 = 2.5
Design a data structure that supports the following two operations:
void addNum(int num) - Add a integer number from the data stream to the data structure.
double findMedian() - Return the median of all elements so far.

class MedianFinder2 {
	priority_queue<int> lo;                              // max heap
	priority_queue<int, vector<int>, greater<int>> hi;   // min heap

public:
	// Adds a number into the data structure.
	void addNum(int num)
	{
		lo.push(num);                                    // Add to max heap

		hi.push(lo.top());                               // balancing step
		lo.pop();

		if (lo.size() < hi.size()) {                     // maintain size property
			lo.push(hi.top());
			hi.pop();
		}
	}

	// Returns the median of current data stream
	double findMedian()
	{
		return lo.size() > hi.size() ? lo.top() : ((double)lo.top() + hi.top()) * 0.5;
	}
};

Design Twitter - 设计推特

Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and is able to see the 10 most recent tweets in the user’s news feed. Your design should support the following methods:
postTweet(userId, tweetId): Compose a new tweet.

  • getNewsFeed(userId): Retrieve the 10 most recent tweet ids in the user’s news feed. Each item in the news feed must be posted by users who the user followed or by the user herself. Tweets must be ordered from most recent to least recent.
  • follow(followerId, followeeId): Follower follows a followee.
  • unfollow(followerId, followeeId): Follower unfollows a followee.

Example:

Twitter twitter = new Twitter();

// User 1 posts a new tweet (id = 5).
twitter.postTweet(1, 5);

// User 1's news feed should return a list with 1 tweet id -> [5].
twitter.getNewsFeed(1);

// User 1 follows user 2.
twitter.follow(1, 2);

// User 2 posts a new tweet (id = 6).
twitter.postTweet(2, 6);

// User 1's news feed should return a list with 2 tweet ids -> [6, 5].
// Tweet id 6 should precede tweet id 5 because it is posted after tweet id 5.
twitter.getNewsFeed(1);

// User 1 unfollows user 2.
twitter.unfollow(1, 2);

// User 1's news feed should return a list with 1 tweet id -> [5],
// since user 1 is no longer following user 2.
twitter.getNewsFeed(1);

题解

使用unsorted_map、heap实现。运用lambda、指针、pair等,推荐新手学习。

#include<iostream>
#include<map>
#include<unordered_map>
#include<unordered_set>
#include<vector>
#include<string>
#include<memory>
#include<utility>
#include<algorithm>
#include<set>
#include<queue>
using namespace std;
struct Post
{
	int tweetId;
	int timeStamp;
	Post(int id, int time):timeStamp(time), tweetId(id) {}
};
auto comp = [](const pair<Post*, Post*>& p1, const pair<Post*, Post*>& p2) {
	// as we use push_back to append a post
	// the last one is the most recent post
	return p1.second->timeStamp < p2.second->timeStamp;
};
class Twitter {
public:
	static int globalTime;
	unordered_map<int, vector<Post>> m;
	unordered_map<int, unordered_set<int>> followMap;
	/** Initialize your data structure here. */
	Twitter() {

	}

	/** Compose a new tweet. */
	void postTweet(int userId, int tweetId) {
		m[userId].emplace_back(tweetId, globalTime++);
		
	}

	/** Retrieve the 10 most recent tweet ids in the user's news feed. Each item in the news feed must be posted by users who the user followed or by the user herself. Tweets must be ordered from most recent to least recent. */
	vector<int> getNewsFeed(int userId) {
		vector<int> r;

		std::vector<std::pair<Post*, Post*>> h; // pair of pointers (begin, current)

		// retrive the list of followees and their posts
		for (auto& u : followMap[userId])
		{
			auto& t = m[u];
			if (t.size() > 0)
				h.emplace_back(t.data(), t.data() + t.size() - 1);
		}
		auto& t = m[userId]; // self
		if (t.size() > 0)
			h.emplace_back(t.data(), t.data() + t.size() - 1);

		make_heap(h.begin(), h.end(), comp);
		for (int i = 0; i < 10 && !h.empty(); ++i) {
			// pop_heap will place the root element at the tail 
			pop_heap(h.begin(), h.end(), comp);
			// get the root element
			auto& elem = h.back();
			r.push_back(elem.second->tweetId);
			// backward tail pointer
			if (elem.first == elem.second--)
				h.pop_back();
			else
				// rearrange the heap
				// don't use sort_heap to arrange the heap,
				// it does different things
				push_heap(h.begin(), h.end(), comp);

		}

		return r;
	}

	/** Follower follows a followee. If the operation is invalid, it should be a no-op. */
	void follow(int followerId, int followeeId) {
		if (followerId != followeeId)
			followMap[followerId].insert(followeeId);
	}

	/** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
	void unfollow(int followerId, int followeeId) {
		followMap[followerId].erase(followeeId);
	}

};

int Twitter::globalTime = 0;
void test2() {
	Twitter obj;
	obj.follow(1, 3);
	obj.postTweet(2, 5);
	obj.postTweet(1, 3);
	obj.postTweet(1, 101);
	obj.postTweet(2, 13);
	obj.postTweet(2, 10);
	obj.postTweet(1, 2);
	obj.postTweet(2, 94);
	obj.postTweet(2, 505);
	obj.postTweet(1, 333);
	obj.postTweet(1, 22);
	obj.getNewsFeed(2);
	obj.follow(2,1);
	obj.getNewsFeed(2);
}

int main()
{
	test2();
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值