题目
单项选择题
Heap Algo 4 You are using the HeapPriorityQueue class implemented with a binary min-heap, which is stored as an array (Python list) in level-order. The heap currently stores the following key-value pairs: [(2, 'A'), (4, 'B'), (6, 'C'), (8, 'D'), (5, 'E')] After calling remove_min() once, what will be the contents of the heap (in array/list format)? (Assume that the heap properties are restored correctly after the operation.) Pseudocode for remove_min() remove_min(): swap the root with the last element remove the last element (which is the min) call _downheap(0) Pseudocode for _downheap(j) _downheap(j): if left child exists: small_child = left if right child exists and right < left: small_child = right if small_child < j: swap(j, small_child) _downheap(small_child) In this implementation, for an element at index j: Left child is at index 2j + 1 Right child is at index 2j + 2 Parent is at index (j - 1) // 2
选项
A.[(4, 'B'), (8, 'D'), (6, 'C')]
B.[(5, 'E'), (4, 'B'), (6, 'C'), (8, 'D')]
C.[(4, 'B'), (8, 'D'), (6, 'C'), (5, 'E')]
D.[(4, 'B'), (5, 'E'), (6, 'C'), (8, 'D')]
查看解析
标准答案
Please login to view
思路分析
We start by restating the problem setup and then inspect each answer choice in light of how remove_min and _downheap operate on a binary min-heap stored in an array.
First, the initial heap is [(2, 'A'), (4, 'B'), (6, 'C'), (8, 'D'), (5, 'E')]. When remove_min is called, the typical steps are to swap the root with the last element, remove the last element (the original min), and then restore the heap order by downheaping from the root.
Step-by......Login to view full explanation登录即可查看完整答案
我们收录了全球超50000道考试原题与详细解析,现在登录,立即获得答案。
类似问题
A max‑heap is always a:
How many children can a node in a binary heap have?
What is the index of the right child of a node at index i in an Array representation of a Max heap? Assume the cell at index 0 is empty.
What is the index of the right child of a node at index i in an Array representation of a Max heap? Assume the cell at index 0 is empty.
更多留学生实用工具
希望你的学习变得更简单
加入我们,立即解锁 海量真题 与 独家解析,让复习快人一步!