Ch 5: Heaps Flashcards
(41 cards)
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
explain the heapsort algorithm
- uses 2 loops to sort an array
- first loop heapifies the array using MaxHeapPercolateDown
- second loop removes the maximum value, stores that value at the end index, and decrements the end index, until the end index is 0
what is a priority queue?
a queue where each item has a priority and items with higher priority are closer to the front of the queue than items with lower priority
what is enqueue for a priority queue?
an operation that inserts an item such that the item is closer to the front than all items of lower priority, and closer to the end than all items of equal or higher priority
what is dequeue for a priority queue?
an operation that removes and returns the item at the front of the queue, which has the highest priority
is the priority the same for every priority queue?
no, it can be changed for each queue, sometimes it compares the data, sometimes it compares the priority value, sometimes order is least-greatest or greatest-least
what is peek for a priority queue?
an operation that returns the highest priority item, without removing the item from the front of the queue
what is EnqueueWithPriority for a priority queue?
an enqueue operation that includes an argument for the enqueued item’s priority
what is the common way to implement priority queues?
heaps