ch 6 priority queues (heaps) Flashcards
binary heap
a binary tree that is completely filled with exception of the bottom level (complete binary tree)
locating child/parent
left child = 2i
right child = 2i + 1
i/2 = parent
heap order property
smallest element is the root
insert (percolate up)
first, make a hole in the next avalailable spot
if x can be placed in the spot without violating heap order, place it there
otherwise, find parent, place it there, and place x in the hole that the parent created
element is percolated up until correct location
O(logn)
deletemin (percolate down)
delete root
find the smallest of the children and insert into the hole
keep repeating
O(logn)
buildHeap
all items are inserted into the tree
first exam the subtrees on the very bottom. Do necessary percolate ups if there is a leaf that is larger than the parent
keep doing this for each level of the tree