B+ and Heaps
B+ and Heaps
• Max Heap : every node’s key is larger than the key’s of it’s children
• Min Heap: every node’s key is smaller than the key’s of it’s children
Heaps as Priority Queues
• Enqueue
Heaps as Priority Queues (Enqueue)
Heaps as Priority Queues (Denqueue)
Organizing Arrays as Heaps
Organizing Arrays as Heaps
• After dequeue, the tree must satisfy the heap condition and remain
complete
}
heapArray[index] = top
Heap Tree –Array enqueue
insert(key, value)
if array is full
return false
}
heapArray[index] = bottom
Efficiency
• trickeleUp and trickleDown are the most time consuming
• Remember: level of a complete tree is given by:
• 𝐿𝑒𝑣𝑒𝑙(𝐿)=log2𝑁
• Trickle up/down, in the worst case, cycle through their loops L-1 times
• Insertion
• Best Case: 𝑂(1)
• Worst Case: 𝑂(log2𝑁)
• Deletion
• Worst Case: 𝑂(log2𝑁)