Define Priority Queue:
Name the 3 ways to implement Priority Queue:
Complexity of Priority Queue using Unsorted Array?
Complexity of Priority Queue using Sorted Array?
Complexity of Priority Queue using AVL Tree ?
How are Elements sorted in AVL tree for Priority Queue?
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
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?
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?
DEFINITION of BHT
COMPLETE Binary Tree which is either empty or Satisfies the following conditions;
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?
Idea of Bubble Down?
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)]
How do you represent heap value for parent in BUBBLE UP?
heap[parent(i)]
What is Update in BHT?
-Modifying the priority of element in tree
Idea of Update?
How do we build Build a Binary Heap Tree using items in an array IN RANDOM ORDER?