cs 61b Heaps and PQs

The priority Queue: Allowing tracking and removal of the smallest item in a priority queue.

  1. 找到每天最大的几条信息,下面是直接的方法
public List<String> unharmoniousTexts(Sniffer sniffer, int M) {
    ArrayList<String>allMessages = new ArrayList<String>();
    //遍历每天的所有信息,并将其加入到新的列表
    for (Timer timer = new Timer(); timer.hours() < 24; ) {
        allMessages.add(sniffer.getNextMessage());
    }
	//用camparator排序并找出最大的messages
    Comparator<String> cmptr = new HarmoniousnessComparator();
    Collections.sort(allMessages, cmptr, Collections.reverseOrder());

    return allMessages.sublist(0, M);

缺点是空间复杂度是θ(N),然而我们可以通过MinPQ把空间复杂度调整为θ(M)。代码如下

public List<String> unharmoniousTexts(Sniffer sniffer, int M) {
	Comparator<String> cmptr = new HarmoniousnessComparator();
    MinPQ<String> unharmoniousTexts = new HeapMinPQ<Transaction>(cmptr);
    for (Timer timer = new Timer(); timer.hours() < 24; ) {
        unharmoniousTexts.add(sniffer.getNextMessage());
        if (unharmoniousTexts.size() > M){
			unharmoniousTexts.removeSmallest();
		}
    }
	ArrayList<String> textlist = new ArrayList<String>();
	while (unharmoniousTexts.size() > 0){
		textlist.add(unharmoniousTexts.removeSmallest);
	}
	return textlist;
}	   

How we implement a MinPQ?
Some other possibilities:

  1. Ordered Array
  2. Bushy BST
  3. HashTable
    在这里插入图片描述

Heaps (堆)

Binary min-heap: Binary tree that is complete and obeys min-heap property.

Min-heap: Every node is less than or equal to both of its children.

Complete: Missing items only at the bottom level (if any), all nodes are as far left as possible.*

在这里插入图片描述
在这里插入图片描述
1.add(x):
insert(3):

(1).add to the end of heap temporarily;(2)Swim up the hierarchy to your right place.(树的左侧是最高优先级);

2. deletionMin(x)
(1)把末端的数放到根位置;
(2)然后下沉这个数

Heap operations summary
Given a heap, how do we implement PQ operations?

getSmallest() - return the item in the root node.

add(x) - place the new employee in the last position, and promote as high as possible.

removeSmallest() - assassinate the president (of the company), promote the rightmost person in the company to president. Then demote repeatedly, always taking the ‘better’ successor.

Representation

(1) Three normal approaches

  1. Key key,key.left,ley.right,key.middle;(normal approach)
  2. Key 类型的数组;
  3. Sibling Tree,
    4在这里插入图片描述
    (2) Store keys in an array. Store parentIDs in an array.
    #根节点作为单独的一个数组,然后另一个数组ID,可以把根节点的ID设置为0,这里需要将节点sort编号(也就是单独的节点数列的作用)在这里插入图片描述
    #如果树是完整的,也就是说树的内部已经根据大小排好了序,那么节点的数组就可以省略了。

(3) approach 3
#Write the parent(k) method

int parent(int k){
	return (k-1)/2;
}

%approach 3B
在这里插入图片描述
In this approach, we assume that our tree is complete. This is to ensure that there are no “gaps” inside of our array representation. Thus, we will take this complex 2D structure of the tree and flatten it into an array.
只是比前一个计算起来更简便,可以把第一个node看成sentinel node

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值