CPSC 221: Priority queues and binary heaps

Ace your homework & exams now with Quizwiz!

what is a complete binary tree?

- all levels, except the bottom, are completely filled - the bottom level leaves are as far to the left as possible

property of a priority queue

- for two elements x and y, if pv(x) > pv(y), x will be removed before y

what are the attributes of a heap in its implemented definition?

- int size // number of stored elements - capacity // maximum capacity of array - LIT* array // array in dynamic memory

describe the process of inserting an element into the heap

- the insertion algorithm first ensures that the tree is complete by placing it in the first available leaf on the bottom level/first free slot in array - then partial ordering is maintained with heapifyUp

describe the removal of an element from a heap

- the removal only happens to the root of the heap as such is the property of a PQ - ensure heap is complete by relacing the root node with its right most leaf - then partial ordering is maintained using heapifyDown

what is the time complexity of insertion into the heap?

O(logn) = O(1) for insertion + O(logn) compare/swaps (based on height of a complete tree)

what is the time complexity to remove the max/min from the heap?

O(logn) = O(1) for replacement + O(logn) compare/swaps

properties of a heap

a binary tree that is complete and partially ordered to maintain PQ property

what is a priority queue?

a collection organized to allow fast access to and removal of the largest/smallest element

what are the insert and remove time complexities for a priority queue implemented using a binary heap?

both are O(logn)

what happens if the underlying array of the heap is full?

expand array and copy elements into their respective indices

how are heap nodes indexed in an array?

it is arranged by the natural method of indexing tree nodes, from top to bottom -> left to right i.e. level order traversal

how are children node indices calculated given the parent's index 'i'?

left child's index = 2i + 1 right child's index = 2i + 2

what is the range that nodes on a particular level 'l' can take as indices?

level l's nodes are indexed from: - 2^(l) - 1 to 2^(l+1)-2 where root node is level 0 - ex: nodes on level 5 are of indices: 2^(5) - 1 to 2^(6) - 2 = 32 - 1 to 64 - 2 = 31 to 62

what is prioritization based on?

max or min value among elements

a heap can be constructed by repeatedly inserting items into an empty heap. what is its time complexity?

n elements * logn operations = O(nlogn)

how is the index of the parent node calculated given the child's index 'i'?

parent's index = (i-1) / 2 (round down if 'i' is even)

difference between a heap and AVL tree

same asymptotic performance but simpler implementation for a heap

define heapifyUp

the repetitive operation of compare and swapping a newly inserted element in the heap with its parents until partial order is maintained

define heapifyDown

used in removal operation where we swap the new root with its largest (or smallest) valued child until the partially ordered property holds

are duplicates allowed in a PQ?

PQ's maintain a multiset of items so they allow duplicate entries


Related study sets

Last nineweeks exam computer app

View Set

2.4 Market equilibrium: demand and supply

View Set

Ch.1 - Integrating research, evidence-based practice, and quality improvement processes

View Set

CH 28 - Cystic Fibrosis and Bronchiectasis

View Set

How Art Made the World' Episode 2: The Day Pictures Were Born Quiz

View Set

Ch 48: Management of Patients with Kidney Disorders

View Set

P&P of Rad_CH 30_Central Nervous System Tumors

View Set