Title: Priority Queue
1- Advance Java with Data Structures and Beyond
24 pt
- Lecture 16
- Priority Queues-I
20 pt
20 pt
2Learn applications of Stacks and Queues
How to Implement Stack using Singly Linked List
What are Operations on Linked Queue
3Lecture Objectives
- By the end of this class, you will be able to
- Gain an understanding of the concept and
importance of priority queues in data structures. - Explore various methods and data structures for
implementing priority queues. - Introduce the concept of heaps and their role in
priority queue implementations. - Learn about complete binary trees and how they
are used to implement priority queues efficiently.
4Priority queue and its characteristics
Heap and types of heap
Complete and full Binary Tree
5 6- A priority queue is an abstract data type that
behaves similarly to the normal queue except that
each element has some priority, i.e., the element
with the highest priority would come first in a
priority queue.
7Characteristics of a Priority Queue
- Every element in a priority queue has some
priority associated with it.
- An element with a higher priority will be deleted
before the deletion of the lesser priority.
- If two elements in a priority queue have the same
priority, they will be arranged using the FIFO
principle.
8Types of Priority Queue
- Ascending order priority queue
- In an ascending order priority queue, a lower
priority number is given as a higher priority
priority.
- Descending order priority queue
- In descending order priority queue, a higher
priority number is given as a higher priority in
a priority.
9Priority queue and its characteristics
Heap and types of heap
Complete and full Binary Tree
10 11Capturing and Non-capturing Groups
- A heap is a tree-based data structure that forms
a complete binary tree and satisfies the heap
property. - If A is a parent node of B, then A is ordered
concerning the node B for all nodes A and B in a
heap. - It means that the value of the parent node could
be more than or equal to the value of the child
node, or the value of the parent node could be
less than or equal to the value of the child
node.
12 13Max heap
- The max heap is a heap in which the value of the
parent node is greater than the value of the
child nodes. - In Java, a max heap can be implemented using the
PriorityQueue class from java.util package. The
PriorityQueue class is a priority queue that
provides a way to store elements in a queue-like
data structure in which each element has a
priority associated with it. - Syntax PriorityQueue maxHeap new
PriorityQueueltgt(Comparator.reverseOrder())
14Min heap
- The min heap is a heap in which the value of the
parent node is less than the value of the child
nodes. - In Java, a min heap can be implemented using the
PriorityQueue class from java.util package. The
PriorityQueue class is a priority queue that
provides a way to store elements in a queue-like
data structure in which each element has a
priority associated with it. - Syntax PriorityQueueltIntegergt minHeap new
PriorityQueueltIntegergt()
15Priority queue and its characteristics
Heap and types of heap
Complete and full Binary Tree
16- Priority Queue Operations
17Inserting the element in a priority queue (max
heap)
- If we insert an element in a priority queue, it
will move to the empty slot by looking from top
to bottom and left to right. - If the element is not in a correct place then it
is compared with the parent node if it is found
out of order, elements are swapped. This process
continues until the element is placed in the
correct position.
18 2. Removing the minimum element from
the priority queue
- As we know in a max heap, the maximum element is
the root node. When we remove the root node, it
creates an empty slot. - The last inserted element will be added to this
empty slot. Then, this element is compared with
the child nodes, i.e., left-child and
right-child, and swapped with the smaller of the
two. It keeps moving down the tree until the heap
property is restored.
19 20Complete Binary Tree
- A complete binary tree is a binary tree in which
all the levels are completely filled except
possibly the lowest one, which is filled from the
left. - A complete binary tree is just like a full binary
tree, but with two major differences - All the leaf elements must lean towards the left.
- The last leaf element might not have a right
sibling i.e. a complete binary tree doesn't have
to be a full binary tree.
21Terminology of Complete Binary Tree
- Root Node in which no edge is coming from the
parent. For example, node A
- Child The node having some incoming edge is
called the child. For example, nodes B, and F
are the child of A and C respectively.
- Sibling Nodes having the same parent are
siblings. For example, D and E are siblings as
they have the same parent B.
- Degree of a node Number of children of a
particular parent. For example- The degree of A
is 2 and the Degree of C is 1. The degree of D is
0.
- Internal/External nodes Leaf nodes are external
nodes and non leaf nodes are internal nodes.
- Level Count nodes in a path to reach a
destination node. For example, the level of node
D is 2 as nodes A and B form the path.
22Properties of Complete Binary Tree
- A complete binary tree is said to be a proper
binary tree where all leaves have the same depth.
- In a complete binary tree, the number of nodes at
depth d is 2d.
- In a complete binary tree with n nodes, the
height of the tree is log(n1).
- All the levels except the last level are full.
23Creation of Complete Binary Tree
- Algorithm
- For the creation of a Complete Binary Tree, we
require a queue data structure to keep track of
the inserted nodes. - Step 1 Initialize the root with a new node when
the tree is empty. - Step 2 If the tree is not empty then get the
front element .If the front element does not have
a left child, then set the left child to a new
node .If the right child is not present, then set
the right child as a new node - Step 3 If the node has both children, then pop
it from the queue. - Step 4 Enqueue the new data.
A complete Binary Tree can be represented
using an array. If the parent is index i the left
child is at 2i1 and the right child is at 2i2.
24Full Binary Tree
- A full binary tree can be defined as a binary
tree in which all the nodes have 0 or two
children. In other words, the full binary tree
can be defined as a binary tree in which all the
nodes have two children except the leaf nodes. - A full binary tree is a binary tree in which all
of the nodes have either 0 or 2 offspring. In
other terms, a full binary tree is a binary tree
in which all nodes, except the leaf nodes, have
two offspring.
25Priority queue and its characteristics
Heap and types of heap
Complete and Full Binary Tree
26(No Transcript)
27(No Transcript)
28(No Transcript)