[题集]Lecture 4. Leftist Heaps and Skew Heaps

本文探讨了左偏堆的性质,包括节点数量与空路径长度的关系,以及合并、删除操作的影响。同时,分析了偏斜堆的特性,如插入操作后的平衡状态,以及合并的时间复杂度。还涉及了合并二叉堆的具体步骤及其时间复杂性,并讨论了不同操作在不同堆结构中的效率比较。
摘要由CSDN通过智能技术生成

1.A leftist heap with the null path length of the root being r must have at least 2 r + 1 − 1 2^{r+1}-1 2r+11 nodes

用递归:假如r成立,则对r+1,右路径r个点,则右子树有 2 r + 1 − 1 2^{r+1}-1 2r+11个点。

而左子树Npl至少也为r,则左子树的右路径至少r,则左子树至少 2 r + 1 − 1 2^{r+1}-1 2r+11 2 ( 2 r + 1 − 1 ) + 1 = 2 r + 2 − 1 2(2^{r+1}-1)+1=2^{r+2}-1 2(2r+11)+1=2r+21

T

2.Merge the two leftist heaps in the following figure. Which one of the following statements is FALSE?

在这里插入图片描述

A.the null path length of 6 is the same as that of 2

B.1 is the root with 3 being its right child

C.Along the left most path from top down, we have 1, 2, 4, and 5

D.6 is the left child of 2

C
在这里插入图片描述

总是和右子树合并,

小的作为根,左子树照抄

沿着右侧上去检查Npl。Npl是孩子中Npl较小的那个+1

3 Delete the minimum number from the given leftist heap. Which one of the following statements is TRUE?

在这里插入图片描述

A.9 is NOT the root

B.24 is the left child of 18

C.18 is the right child of 11

D.13 is the left child of 12

D

在这里插入图片描述

4.With the same operations, the resulting skew heap is always more balanced than the leftist heap.

这个是不一定的

F

5.The right path of a skew heap can be arbitrarily long.

T

在这里插入图片描述
一直插入类似这种结构(第一个节点不是,是为了方便画)
这样,转过去以后,就会增长左边

左边转过来,就变成右边了

6.Which one of the following statements is FALSE about a skew heap?

A.Skew heaps do not need to maintain the null path length of any node

B.Comparing to leftist heaps, skew heaps are always more efficient in space

C.Skew heaps have O(logN) worst-case cost for merging

D.Skew heaps have O(logN) amortized cost per operation

B是对的,因为不需要存储Npl

C摊销是O(logN),最差应该大于logN

D是对的

7.The amortized time bound of insertions for a skew heap of size N is O(logN).

摊销是logN

T

8.The result of inserting keys 1 to 2^​k−1 for any k>4 in order into an initially empty skew heap is always a full binary tree.

这个很神奇,但是测试一下发现是对的。

9.Merge the two leftist heaps in the following figure. Which one of the following statements is FALSE?

在这里插入图片描述

A.2 is the root with 11 being its right child

B.the depths of 9 and 12 are the same

C.21 is the deepest node with 11 being its parent

D.the null path length of 4 is less than that of 2

插入结果如图
在这里插入图片描述

D

10.We can perform BuildHeap for leftist heaps by considering each element as a one-node leftist heap, placing all these heaps on a queue, and performing the following step: Until only one heap is on the queue, dequeue two heaps, merge them, and enqueue the result. Which one of the following statements is FALSE?

A.in the k-th run, ⌈N/2​^ k⌉ leftist heaps are formed, each contains 2^​k
​​ nodes

B.the worst case is when N=2^​K for some integer K

C.the time complexity T(N)=O(​N/2​​log2^​0+N/2^2​​log2^​1​​ +​N/2^​3log2^​2​​+⋯+​N/2^Klog2^(K−1)) for some integer K so that N=2^​K
​​
D.the worst case time complexity of this algorithm is Θ(NlogN)

对于D:分治,和归并差不多。T(N)=2T(N/2)+O(logN),复杂度为O(N)

其他没有问题。

11.Insert keys 1 to 15 in order into an initially empty skew heap. Which one of the following statements is FALSE?

A.the resulting tree is a complete binary tree

B.there are 6 leaf nodes

C.6 is the left child of 2

D.11 is the right child of 7

如果个数满足一个完全二叉树,那么用skew插入是可以插出完全二叉树的。1,2,4,8.叶子是8个(先别急着一个一个插入)

12.Merge the two skew heaps in the following figure. Which one of the following statements is FALSE?

在这里插入图片描述

A.15 is the right child of 8

B.14 is the right child of 6

C.1 is the root

D.9 is the right child of 3

A 应该是left child。插入结果如图, 1还得交换孩子,这个图有问题:

B14应该是left child, 原因是当插入到了一个NULL之后就返回了,不会交换孩子

在这里插入图片描述

13. If we merge two heaps represented by complete binary trees, the time complexity is Θ(N) provided that the size of each heap is N

Merge就是建新堆,复杂度是O(N) T

14.A skew heap is a heap data structure implemented as a binary tree. Skew heaps are advantageous because of their ability to merge more quickly than balanced binary heaps. The worst case time complexities for Merge, Insert, and DeleteMin are all O(N), while the amorited time complexities for Merge, Insert, and DeleteMin are all O(logN).

T,均摊复杂度为O(logN),最坏是O(N)

15.The NPL of each node in a heap is supposed to be calculated from top down.

应该从叶子开始算,F

16.Which one of the following may be a leftist heap?

A在这里插入图片描述B在这里插入图片描述C在这里插入图片描述D在这里插入图片描述
必须先是堆才行,也就是说大小顺序不能错,大的在上方,或者小的在上方,不能一会儿大一会儿小。

17.Merge the two given skew heaps. Which one of the following statements is FALSE?

在这里插入图片描述

A.3 is the root

B.17 is the right child of 16

C.39 is the right child of 25

D.25 is the left child of 14

C,插入结果如图

在这里插入图片描述
插入的时候注意:记录下右侧被插入的节点。只有这些节点需要交换孩子,例如6-14中,只有14的右侧被插入了,因此只有14的孩子需要交换。而6的孩子不需要交换。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值