DS 3
DS 3
DS 3
Queues
Applications of Queue
Queues can be used to schedule jobs and ensure that they are executed in the correct
order.
Queues can be used to manage the order in which print jobs are sent to the printer.
Queues are used to implement the breadth-first search algorithm.
Queues can be used to manage incoming calls and ensure that they are handled in the
correct order.
Queues can be used to manage the order in which processes are executed on the CPU.
Queues can be used for buffering data while it is being transferred between two
systems. When data is received, it is added to the back of the queue, and when data is
sent, it is removed from the front of the queue.
Enqueue Operation
Inserting a new element in the position REAR is called the enqueue operation.
We must check if the queue is full before insertion. After the enqueue operation
the REAR is incremented by 1 and will point to the newly inserted element.
For inserting the first element in a queue, the value of FRONT has to be set to 0.
Consider a queue of size 5. Suppose we have to insert A, B, C, D and E into the
queue. The following changes will be made to the queue.
Dequeue Operation
The size of the above queue is 5. It has 3 elements A, B and C. The FRONT, REAR and
MAX pointers will have values 0, 2 and 5 respectively.The algorithms for enqueue and
dequeue operations are given below.
The disadvantage of using an array to represent a queue is that the array must be
specified to have a fixed size. If the array size cannot be known ahead of time, then the
linked representation is used.
The HEAD pointer of the linked list is used as the FRONT. Also, use another pointer
called REAR to store the address of the last element of the queue.
The algorithms for enqueue and dequeue operations are given below.
Algorithm: LINK_ENQUEUE
Step 1: Allocate memory for the new node and name it as PTR
Step 2: SET PTR -> DATA = VAL
Step 3: IF FRONT = NULL
SET FRONT = REAR = PTR
SET FRONT -> NEXT = REAR -> NEXT = NULL
ELSE
SET REAR -> NEXT = PTR
SET REAR = PTR
SET REAR -> NEXT = NULL
[END OF IF]
Step 4: END
Algorithm: LINK_DEQUEUE
Step 1: IF FRONT = NULL
Write Underflow
Go to Step 5
[END OF IF]
Step 2: SET PTR = FRONT
Step 3: SET FRONT = FRONT -> NEXT
Step 4: FREE PTR
Step 5: END
Disadvantage of Queue
Look at the following queue.
You cannot insert a new value now, because the queue is completely full. Consider the
following case: two consecutive deletions are made.
Even though there is enough space to hold a new element, the overflow condition still occurs
since the condition REAR = MAX – 1 is still valid. This is a major drawback of a linear
queue. A solution to this problem is to make the queue circular.
Simple Queue or Linear Queue
In Linear Queue, an insertion takes place from one end while the deletion occurs from
another end. The end at which the insertion takes place is known as the rear end, and the end
at which the deletion takes place is known as front end. It strictly follows the FIFO rule.
The major drawback of using a linear Queue is that insertion is done only from the rear end.
If the first three elements are deleted from the Queue, we cannot insert more elements even
though the space is available in a Linear Queue. In this case, the linear Queue shows the
overflow condition as the rear is pointing to the last element of the Queue.
Circular Queue
In Circular Queue, all the nodes are represented as circular. It is similar to the linear Queue
except that the last element of the queue is connected to the first element. It is also known as
Ring Buffer, as all the ends are connected to another end.
The drawback that occurs in a linear queue is overcome by using the circular queue. If the
empty space is available in a circular queue, the new element can be added in an empty space
by simply incrementing the value of rear. The main advantage of using the circular queue is
better memory utilization.
Priority Queue
It is a special type of queue in which the elements are arranged based on the priority. It is a
special type of queue data structure in which every element has a priority associated with it.
Suppose some elements occur with the same priority, they will be arranged according to the
FIFO principle. The representation of priority queue is shown in the below image -
Insertion in priority queue takes place based on the arrival, while deletion in the priority
queue occurs based on the priority. Priority queue is mainly used to implement the CPU
scheduling algorithms.
There are two types of priority queue that are discussed as follows -
In Deque or Double Ended Queue, insertion and deletion can be done from both ends of the
queue either from the front or rear. It means that we can insert and delete elements from both
front and rear ends of the queue. Deque can be used as a palindrome checker means that if we
read the string from both ends, then the string would be the same.
Deque can be used both as stack and queue as it allows the insertion and deletion operations
on both ends. Deque can be considered as stack because stack follows the LIFO (Last In First
Out) principle in which insertion and deletion both can be performed only from one end. And
in deque, it is possible to perform both insertion and deletion from one end, and Deque does
not follow the FIFO principle.
There are two types of deque that are discussed as follows -
o Input restricted deque - As the name implies, in input restricted queue, insertion
operation can be performed at only one end, while deletion can be performed from
both ends.
o Output restricted deque - As the name implies, in output restricted queue, deletion
operation can be performed at only one end, while insertion can be performed from
both ends.
Tree
Tree data structure is a hierarchical structure that is used to represent and organize data in
a way that is easy to navigate and search. It is a collection of nodes that are connected by
edges and has a hierarchical relationship between the nodes .
The topmost node of the tree is called the root, and the nodes below it is called the child
nodes. Each node can have multiple child nodes, and these child nodes can also have their
own child nodes, forming a recursive structure .
Why Tree is considered a non-linear data structure?
The data in a tree are not stored in a sequential manner i.e., they are not stored linearly.
Instead, they are arranged on multiple levels or we can say it is a hierarchical structure. For
this reason, the tree is considered to be a non-linear data structure .
Applications of Tree
To search an element in a set quickly, Binary Search Trees(BSTs) are used.
Heap sort is done by a kind of tree called Heap.
Tries are modified version of trees used to store routing informations in routers.
Compiler use a syntax tree to parse program you write.
Shortest path trees and spanning Trees are used in bridges and routers.
Tree data structure can be classified into three types based upon the number of children
each node of the tree can have. The types are:
Binary tree: In a binary tree, each node can have a maximum of two children linked to
it.
Ternary Tree: A Ternary Tree is a tree data structure in which each node has at most
three child nodes, usually distinguished as “left”, “mid” and “right”.
N-ary Tree or Generic Tree : Generic trees are a collection of nodes where each node
is a data structure that consists of records and a list of references to its
children(duplicate references are not allowed). Unlike the linked list, each node stores
the address of multiple nodes.
Binary Tree
The Binary tree means that the node can have maximum two children. Here, binary name
itself suggests that 'two'; therefore, each node can have either 0, 1 or 2 children. A non-empty
binary tree consist of the following:
A node called the root node.
A left sub-tree.
A right sub-tree.
Both the sub-trees are themselves binary trees.
Balanced Binary Tree: A binary tree is balanced if the height difference between the left and
right subtrees of every node is at most 1. Balanced trees ensure optimal performance in
search operations.
Full Binary Tree: A full binary tree is a binary tree type where every node has either 0 or 2
child nodes.
Complete Binary Tree: A complete binary tree is a binary tree type where all the leaf nodes
must be on the same level. However, root and internal nodes in a complete binary tree can
either have 0, 1 or 2 child nodes.
Perfect Binary Tree: A perfect binary tree is a binary tree type where all the leaf nodes are
on the same level and every node except leaf nodes have 2 children.
Degenerate Binary Tree:A degenerate binary tree, also known as a pathological tree, is a
special type of binary tree where each parent node has only one child node. In other words, it
is a tree in which all the internal nodes have a degree of 1, and only the leaf nodes have a
degree of 0.
Degenerate binary trees can also be classified into two types
1. Left-skewed: A degenerate binary tree in which all the nodes lean towards the left
side of the tree.
2. Right-skewed: A degenerate binary tree in which all the nodes lean towards the right
side of the tree.
struct node {
struct node *left;
int data;
struct node *right;
};
The root node is pointed by a pointer ROOT. The tree is empty if ROOT = NULL