博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Priority queue and Heap
阅读量:7039 次
发布时间:2019-06-28

本文共 2027 字,大约阅读时间需要 6 分钟。

I have seen lots of solutions confuse priority queue with heap. I find a good  and list the talk below.

Concept:

1.Heap is a kind of data structure. It is a name for a particular way of storing data that makes certain operations very efficient. We can use a tree or array to describe it.

18  /	\ 10	 16/ \   / \9  5  8  1218, 10, 16, 9, 5, 8, 12

2.Priority queue is an abstract datatype. It is a shorthand way of describing a particular interface and behavior, and says nothing about the underlying implementation.

A heap is a very good data structure to implement a priority queue. The operations which are made efficient by the heap data structure are the operations that the priority queue interface needs.

Implementation: c++

1.priority_queue

struct compare {    bool operator()(const ListNode* l, const ListNode* r) {        return l->val > r->val;    }};ListNode *mergeKLists(vector
&lists) { //priority_queue priority_queue
, compare> q; for(auto l : lists) { if(l) q.push(l); } if(q.empty()) return NULL; ListNode* result = q.top(); q.pop(); if(result->next) q.push(result->next); ListNode* tail = result; while(!q.empty()) { tail->next = q.top(); q.pop(); tail = tail->next; if(tail->next) q.push(tail->next); } return result;}

2.make_heap:

static bool heapComp(ListNode* a, ListNode* b) {        return a->val > b->val;}ListNode* mergeKLists(vector
& lists) { //make_heap ListNode head(0); ListNode *curNode = &head; vector
v; for(int i =0; i
heap data strcture while(v.size()>0){ curNode->next=v.front(); pop_heap(v.begin(), v.end(), heapComp); v.pop_back(); curNode = curNode->next; if(curNode->next) { v.push_back(curNode->next); push_heap(v.begin(), v.end(), heapComp); } } return head.next;}
 

转载于:https://www.cnblogs.com/lhqblog/p/6404989.html

你可能感兴趣的文章
leetcode Sort List
查看>>
Maven com.sun.jdmk:jmxtools:jar 下载不下来
查看>>
DevExpress之Skin自定义使用
查看>>
可变参数
查看>>
[日推荐]『饿了么外卖服务』饿了么官方小程序,无需下载安装!
查看>>
JavaScript 作用域
查看>>
Linux Ubuntu 16.04 主机名设置
查看>>
CCNP 静态路由
查看>>
单链表二[不带头节点链表]
查看>>
Html中居中问题小结
查看>>
Spring mvc 拦截器
查看>>
MySQL GROUP BY 和GROUP_CONCAT的一些用法
查看>>
关于box2d的例子testbed
查看>>
## mysqldump 导出数据库各参数详细说明
查看>>
java中URL编码和中文相互转换
查看>>
影评:《云图》:生命并非微不足道
查看>>
hibernate4之一对一关系映射(二)
查看>>
我的友情链接
查看>>
Android第五课 编译错误分析
查看>>
Excel表格模板:教育系统清资报表下载
查看>>