You are on page 1of 12

1.What is a queue?

Explain and write the algorithms for operations on queues

Queue is an abstract data structure, somewhat similar to Stacks. Unlike stacks, a


queue is open at both its ends. One end is always used to insert data (enqueue) and
the other is used to remove data (dequeue). Queue follows First-In-First-Out
methodology, i.e., the data item stored first will be accessed first.

Queue Representation
As we now understand that in queue, we access both ends for different reasons. The
following diagram given below tries to explain queue representation as data structure

As in stacks, a queue can also be implemented using Arrays, Linked-lists, Pointers


and Structures. For the sake of simplicity, we shall implement queues using one-
dimensional array.

Basic Operations
Queue operations may involve initializing or defining the queue, utilizing it, and then
completely erasing it from the memory. Here we shall try to understand the basic
operations associated with queues −
• enqueue() − add (store) an item to the queue.
• dequeue() − remove (access) an item from the queue.
Few more functions are required to make the above-mentioned queue operation
efficient. These are −
• peek() − Gets the element at the front of the queue without removing it.
• isfull() − Checks if the queue is full.
• isempty() − Checks if the queue is empty.
In queue, we always dequeue (or access) data, pointed by front pointer and while
enqueing (or storing) data in the queue we take help of rear pointer.
Let's first learn about supportive functions of a queue −

peek()
This function helps to see the data at the front of the queue. The algorithm of peek()
function is as follows −
Algorithm
begin procedure peek
return queue[front]
end procedure

isfull()
As we are using single dimension array to implement queue, we just check for the
rear pointer to reach at MAXSIZE to determine that the queue is full. In case we
maintain the queue in a circular linked-list, the algorithm will differ. Algorithm of isfull()
function −
Algorithm
begin procedure isfull

if rear equals to MAXSIZE


return true
else
return false
endif

end procedure

isempty()
Algorithm of isempty() function −
Algorithm
begin procedure isempty

if front is less than MIN OR front is greater than rear


return true
else
return false
endif

end procedure

If the value of front is less than MIN or 0, it tells that the queue is not yet initialized,
hence empty.

Enqueue Operation
Queues maintain two data pointers, front and rear. Therefore, its operations are
comparatively difficult to implement than that of stacks.
The following steps should be taken to enqueue (insert) data into a queue −
• Step 1 − Check if the queue is full.
• Step 2 − If the queue is full, produce overflow error and exit.
• Step 3 − If the queue is not full, increment rear pointer to point the next empty
space.
• Step 4 − Add data element to the queue location, where the rear is pointing.
• Step 5 − return success.

Sometimes, we also check to see if a queue is initialized or not, to handle any


unforeseen situations.

Algorithm for enqueue operation


procedure enqueue(data)

if queue is full
return overflow
endif

rear ← rear + 1
queue[rear] ← data
return true

end procedure

Dequeue Operation
Accessing data from the queue is a process of two tasks − access the data
where front is pointing and remove the data after access. The following steps are
taken to perform dequeue operation −
• Step 1 − Check if the queue is empty.
• Step 2 − If the queue is empty, produce underflow error and exit.
• Step 3 − If the queue is not empty, access the data where front is pointing.
• Step 4 − Increment front pointer to point to the next available data element.
• Step 5 − Return success.

Algorithm for dequeue operation


procedure dequeue

if queue is empty
return underflow
end if

data = queue[front]
front ← front + 1
return true

end procedure

What is a circular queue? Explain the operations on circular queues.

A circular queue is the extended version of a regular queue where the last
element is connected to the first element. Thus forming a circle-like
structure.
Circular queue representation
The circular queue solves the major limitation of the normal queue. In a
normal queue, after a bit of insertion and deletion, there will be non-usable
empty space.

Limitation of the regular Queue


Here, indexes 0 and 1 can only be used after resetting the queue (deletion
of all elements). This reduces the actual size of the queue.

How Circular Queue Works


Circular Queue works by the process of circular increment i.e. when we try
to increment the pointer and we reach the end of the queue, we start from
the beginning of the queue.
Here, the circular increment is performed by modulo division with the queue
size. That is,

if REAR + 1 == 5 (overflow!), REAR = (REAR + 1)%5 = 0 (start of queue)

Circular Queue Operations


The circular queue work as follows:

• two pointers FRONT and REAR

• FRONT track the first element of the queue


• REAR track the last elements of the queue
• initially, set value of FRONT and REAR to -1
1. Enqueue Operation

• check if the queue is full

• for the first element, set value of FRONT to 0


• circularly increase the REAR index by 1 (i.e. if the rear reaches the end,
next it would be at the start of the queue)
• add the new element in the position pointed to by REAR

2. Dequeue Operation

• check if the queue is empty

• return the value pointed by FRONT

• circularly increase the FRONT index by 1


• for the last element, reset the values of FRONT and REAR to -1
However, the check for full queue has a new additional case:

• Case 1: FRONT = 0 && REAR == SIZE - 1


• Case 2: FRONT = REAR + 1

The second case happens when REAR starts from 0 due to circular increment
and when its value is just 1 less than FRONT , the queue is full.
Enque and Deque Operation

What is a binary tree? What are the tree traversal operations? Explain the
traversal operations.
Traversal is a process to visit all the nodes of a tree and may print their values too.
Because, all nodes are connected via edges (links) we always start from the root
(head) node. That is, we cannot randomly access a node in a tree. There are three
ways which we use to traverse a tree −

• In-order Traversal
• Pre-order Traversal
• Post-order Traversal
Generally, we traverse a tree to search or locate a given item or key in the tree or to
print all the values it contains.

In-order Traversal
In this traversal method, the left subtree is visited first, then the root and later the right
sub-tree. We should always remember that every node may represent a subtree itself.
If a binary tree is traversed in-order, the output will produce sorted key values in an
ascending order.

We start from A, and following in-order traversal, we move to its left subtree B. B is
also traversed in-order. The process goes on until all the nodes are visited. The output
of inorder traversal of this tree will be −
D→B→E→A→F→C→G

Algorithm
Until all nodes are traversed −
Step 1 − Recursively traverse left subtree.
Step 2 − Visit root node.
Step 3 − Recursively traverse right subtree.

Pre-order Traversal
In this traversal method, the root node is visited first, then the left subtree and finally
the right subtree.

We start from A, and following pre-order traversal, we first visit A itself and then move
to its left subtree B. B is also traversed pre-order. The process goes on until all the
nodes are visited. The output of pre-order traversal of this tree will be −
A→B→D→E→C→F→G

Algorithm
Until all nodes are traversed −
Step 1 − Visit root node.
Step 2 − Recursively traverse left subtree.
Step 3 − Recursively traverse right subtree.

Post-order Traversal
In this traversal method, the root node is visited last, hence the name. First we
traverse the left subtree, then the right subtree and finally the root node.
We start from A, and following Post-order traversal, we first visit the left
subtree B. B is also traversed post-order. The process goes on until all the nodes are
visited. The output of post-order traversal of this tree will be −
D→E→B→F→G→C→A

Algorithm
Until all nodes are traversed −
Step 1 − Recursively traverse left subtree.
Step 2 − Recursively traverse right subtree.
Step 3 − Visit root node.

Explain the insert, delete operations on binary search tree?


A tree is a data structure composed of nodes that has the following characteristics:

1. Each tree has a root node at the top (also known as Parent Node) containing some value
(can be any datatype).

2. The root node has zero or more child nodes.

3. Each child node has zero or more child nodes, and so on. This creates a subtree in the
tree. Every node has its own subtree made up of its children and their children, etc. This
means that every node on its own can be a tree.
A binary search tree (BST) adds these two characteristics:
1. Each node has a maximum of up to two children.

2. For each node, the values of its left descendent nodes are less than that of the current
node, which in turn is less than the right descendent nodes (if any).
The BST is built on the idea of the binary search algorithm, which allows for fast lookup,
insertion and removal of nodes. The way that they are set up means that, on average, each
comparison allows the operations to skip about half of the tree, so that each lookup, insertion
or deletion takes time proportional to the logarithm of the number of items stored in the
tree, O(log n) . However, some times the worst case can happen, when the tree isn't
balanced and the time complexity is O(n) for all three of these functions. That is why self-
balancing trees (AVL, red-black, etc.) are a lot more effective than the basic BST.
Insert
It is very similar to the search function. You again start at the root of the tree and go down
recursively, searching for the right place to insert our new node, in the same way as explained
in the search function. If a node with the same value is already in the tree, you can choose to
either insert the duplicate or not. Some trees allow duplicates, some don't. It depends on the
certain implementation.

Deletion
There are 3 cases that can happen when you are trying to delete a node. If it has,

1. No subtree (no children): This one is the easiest one. You can simply just delete the
node, without any additional actions required.

2. One subtree (one child): You have to make sure that after the node is deleted, its child is
then connected to the deleted node's parent.

3. Two subtrees (two children): You have to find and replace the node you want to delete
with its inorder successor (the leftmost node in the right subtree)

You might also like