DSA - Priority Queue & Heap Trees Flashcards
Define Priority Queue:
- Abstract Data Type that maintains a collection of item which has an associated priority value
- Gets & removes the items with highest priority efficiently
- Items with arbitrary values can be added at any time
Name the 3 ways to implement Priority Queue:
- Unsorted Array
- Sorted Array
- AVL tree
Complexity of Priority Queue using Unsorted Array?
- Inserting an item is O(1)
- get is O(n)
Complexity of Priority Queue using Sorted Array?
- Inserting an item is O(n)
- get is O(1)
Complexity of Priority Queue using AVL Tree ?
- Insert & Get is O(log(n))
How are Elements sorted in AVL tree for Priority Queue?
- Sort Elements using Priority Value
Explain What Binary Heap Tree is?
A binary tree is a BHT if it is Complete meaning that every level except last is COMPLETELY FILLED & all leaves are placed as far to the left as possible
- Binary with the a given number of nodes, the tidiest, most balanced and compact possible
- DOES NOT requirements for BST
How do we Implement Priority Queue as A BHT?
Using an ARRAY
- Place Nodes in the array Root, Left Child then right
IF Binary Tree is NOT COMPLETE?
- Wasteful implementation strategy because space is still reserved for missing nodes
- Array cell need to be marked to indicate that it is not part of tree
MUST use ;
- Invalid values for this application
- Separate data structure to indicate whether the corresponding cell contains a real value
IF Binary Tree is COMPLETE?
- Tree is sufficient to identify the END of the TREE
- Not need to mark missing nodes because none before the end of the tree are missing
DEFINITION of BHT
COMPLETE Binary Tree which is either empty or Satisfies the following conditions;
- Priority of the root is greater or equal to that of its children
- Left & Right subtrees of the root are Heap Trees
- NO Restriction on the relationship between the left & right children of any Node
NO NEED TO SORT BHT
IDEA of BHT Insertion?
Insert the value at the end of the last level and then keeps bubbling up as long as it is larger than its parent
AS we bubble up, when we swap the value of a node i with that of its parent, don’t compare i with sibling as if it is larger than parent must be larger than sibling
What is Worst case for BHT insertion?
Bubbling to the Root which is log(n) operations
What is the idea of Deleting the ROOT?
1- Remove the last RIGHTMOST node and replace it with ROOT
2- Bubble down:
-keep swapping it with the higher priority child as long as
any of its children have HIGHER PRIORITY
What is the idea of Deleting an Arbitrary node?
(DELETING an NODE Anywhere)
1- Remove the last RIGHTMOST node and replace it with the node to be Deleted
2- Node may be smaller or Larger than the Children thus Bubble UP or DOWN where necessary, DO BUBBLE UP THEN BUBBLE DOWN
– NEED TO CHECK WHICH METHOD IT SHOULD DO
Code for Inserting?
public void insert ( int p ) {
if ( n == MAXSIZE )
throw HeapFullException ;
n = n + 1 ;
heap [ n ] = p ;
//insert the new value as the last
// node o f t h e l a s t l e v e l
bubbleUp ( n ) ; // and b u b b l e i t up
}
Idea of Bubble Up?
- check if ur at rook by checking if i ==1
- otherwise check whether heap(i) is greater than heap(parent(i))
if so swap and them around and call bubble up again - else return Heap tree
Idea of Bubble Down?
- Check if root has no children by doing left(i) > n, if true return tree
- Then check if it has one Child LEFT do right(i)>n
if Left child is greater than heap(i) then swap them and return tree - Otherwise there a 2 Children:
First Check Left is Greater than Right child then check if Left is Greater than heap(i), if so swap and then Bubble Down left(i) - Else Right is greater :
Check if right is greater than heap(i), if so swap and then bubble down left(i)
How do you represent heap value for left(i) in BUBBLE DOWN?
heap[left(i)]
How do you represent heap value for right(i) in BUBBLE DOWN?
heap[right(i)]