Binary Heap Trees Flashcards
(6 cards)
What is a binary heap ?
A complete binary tree represented as an array,
Max-heap - Every parent node is greater than or equal to its children,
Min-heap - Every parent node is less than or euqal to its children.
What is the Insertion operation for a binary heap ?
Insert at the next available spot (bottom layer, left or right),
Swap upwards with the partent until the heap property is restored.
Average: O(1)
What is the Deletion opertaion for a binary heap ?
Replace root with the last element,
Swap down with the appropriate child until the heap property is restored.
O(log n)
What is the Construction operation for a binary heap ?
Heapify,
Start from the middle of the array and “bubble down”
More effiecient than inserting one at a time.
O(1)
How can we impement Binary heaps ?
Using array,
Left child = 2 * i
Right child = 2* i + 1,
Parent = i /2
What are the advantages and disadvantages of Binary heaps ?
Advantages:
Fast insertion and deletion,
Ideal for quick min/max access,
Memory efficient,
Foundation for HeapSort.
Disadvantages:
Poort at searching,
Not flexible for other types of operations,
Not stable