Ch 5: Heaps Flashcards
what is a max-heap?
a complete binary tree that maintains the simple property that a node’s key is greater than or equal to the node’s children’s keys
what is the root node?
a node with the maximum key in the entire tree, and therefore has no parents
what is insert?
an operation that inserts the node into the tree’s last level then swaps node with the parent until no max-heap property violation occurs
how is a level added and when is a new level added during insert?
a level is filled left to right before adding another level so that the tree’s height is always the minimum possible
what is percolating?
an upward movement of a node in a max-heap
what is remove?
an operation that removes the root node by replacing the root with the last level’s last node and swapping that node with its greatest child until no max-heap property violation occurs
what is the height of a max-heap?
logN
what is a min-heap?
similar to a max-heap but a node’s key is less than or equal to its children’s keys
how are heaps typically implemented?
arrays
what is used to refer to the nodes?
indices, not pointers since it is an array and not linked list
what is the formula to find the parent index from the child index?
lower of (i-1)/2
what is the formula to find the child index/indices from the parent index
(2i)+1 / (21i)+2
what is heap sort?
- a sorting algorithm that takes advantage of a max-heap’s properties by repeatedly removing the max and building a sorted array in reverse order
- sorts from least to greatest
what is heapify?
an operation that is used to turn an array into a heap
what is the formula to find the largest internal node index?
lower of N/2 -1
explain how heapsort works
- heapify array into a max-heap and initializing an index value to the size of the array minus 1
- swap last item w root and lower size by 1 (removes last item)
- percolate top node down to satisfy heap ordering property
- repeat until end index is 0