Heaps Flashcards
(50 cards)
What is a heap?
A special tree-based data structure that satisfies the heap property.
What is the heap property?
In a max-heap, each parent is greater than or equal to its children; in a min-heap, each parent is less than or equal to its children.
What is a max-heap?
A heap where the largest element is at the root.
What is a min-heap?
A heap where the smallest element is at the root.
What is a binary heap?
A binary tree that maintains the heap property and is complete (all levels filled except possibly the last).
What is a priority queue?
An abstract data type where each element has a priority and elements are served based on priority.
How is a priority queue typically implemented?
Using a binary heap.
What are the main operations of a heap?
Insert, extract-min/extract-max, peek-min/peek-max, and heapify.
What is the time complexity of inserting into a binary heap?
O(log n)
What is the time complexity of extracting the root from a binary heap?
O(log n)
What is the time complexity of peeking at the root of a heap?
O(1)
What is heapify?
The process of rearranging elements to maintain the heap property.
What is the time complexity of heapify?
O(log n) for a node, O(n) for an entire array.
What is build-heap?
Converting an arbitrary array into a heap.
What is the time complexity of build-heap?
O(n)
How is a heap represented in an array?
The root is at index 0, left child at 2i+1, right child at 2i+2.
What is the parent index of a node at index i in a heap array?
(i - 1) // 2
How do you insert into a heap?
Add the element to the end and bubble it up to restore the heap property.
How do you remove the root of a heap?
Replace it with the last element and bubble it down (heapify).
What is the space complexity of a heap with n elements?
O(n)
What is a complete binary tree?
A binary tree where all levels are fully filled except possibly the last, which is filled left to right.
What is a d-ary heap?
A generalization of a binary heap where each node has d children.
What is a Fibonacci heap?
A complex heap structure with better amortized performance for some operations.
What is the time complexity of decrease-key in a Fibonacci heap?
O(1) amortized.