1. Assignment On Data Structure
Nowrin Islam Nishat
ID: 193-35-483
Dept. Of Software Engineering
Mr. S A M Matiur Rahman
Associate Professor and Associate Head
Department of Software Engineering
Daffodil International University
[Queue , Linked List , Tree]
Submitted By : Submitted To :
N-!-s-h-a-t
19-08-2020 1
2. Queue
A Queue is a linear structure which follows a particular order in which the operations are
performed. The order is First In First Out (FIFO). A good example of a queue is any queue of
consumers for a resource where the consumer that came first is served first. The difference
between stacks and queues is in removing. In a stack we remove the item the most recently
added; in a queue, we remove the item the least recently added.
[NOTE] :
The process to add an element into queue is called Enqueue and the
process of removal of an element from queue is called Dequeue.
N-!-s-h-a-t
19-08-2020
2
3. Basic features of Queue
1. Like stack, queue is also an ordered list of elements of similar data
types.
2. Queue is a FIFO( First in First Out ) structure.
3. Once a new element is inserted into the Queue, all the elements
inserted before the new element in the queue must be removed, to
remove the new element.
4. peek( ) function is oftenly used to return the value of first element
without dequeuing it
N-!-s-h-a-t
19-08-2020
3
4. Application Of Queue
Queue, as the name suggests is used whenever we need to manage any group of objects in an
order in which the first one coming in also gets out first while the others wait for their turn
like in the following scenarios.
1. Serving requests on a single shared resource, like a printer, CPU task scheduling etc.
2. In real life scenario, call center phone system uses queues to hold people calling them in
an order until a service representive in free.
3. Handling of interrupts in real time systems.
N-!-s-h-a-t
19-08-2020
4
5. Queue can be implemented using an Array, Stack or Linked List.
The easiest way of implementing a queue is by using an Array.
Initially the head(FRONT) and the tail(REAR) of the queue points at
the first index of the array (starting the index of array from 0). As we
add elements to the queue, the tail keeps on moving ahead, always
pointing to the position where the next element will be inserted,
while the head remains at the first index.
Implementation of Queue Data Structure
When we remove an element from Queue, we can follow
two possible approaches (mentioned [A] and [B] in above
diagram). In [A] approach, we remove the element
at head position, and then one by one shift all the other
elements in forward position.
In approach [B] we remove the element from head position
and then move head to the next position.
In approach [A] there is an overhead of shifting the
elements one position forward every time we remove the
first element.
In approach [B] there is no such overhead, but whenever we
move head one position ahead, after removal of first
element, the size on Queue is reduced by one space each
time. N-!-s-h-a-t
19-08-2020
5
6. Algorithm for ENQUEUE operation
1. Check if the queue is full or not.
2. If the queue is full, then print overflow error and exit the program.
3. If the queue is not full, then increment the tail and add the element.
Algorithm for DEQUEUE operation
1. Check if the queue is empty or not.
2. If the queue is empty, then print underflow error and exit the program.
3. If the queue is not empty, then print the element at the head and increment
the head.
Complexity Analysis of Queue Operations
Just like Stack, in case of a Queue too, we know exactly, on which position new element will be
added and from where an element will be removed, hence both these operations requires a single
step.
> Enqueue: O(1)
> Dequeue: O(1)
> Size: O(1)
N-!-s-h-a-t
19-08-2020
6
7. Linked List
Linked List is a very commonly used linear data structure which consists of group of nodes in a
sequence.
Each node holds its own data and the address of the next node hence forming a chain like
structure.
Linked Lists are used to create trees and graphs.
N-!-s-h-a-t
19-08-2020
7
8. Advantages of Linked Lists
They are a dynamic in nature which allocates the memory when required.
Insertion and deletion operations can be easily implemented.
Stacks and queues can be easily executed.
Linked List reduces the access time.
Disadvantages of Linked Lists
The memory is wasted as pointers require extra memory for storage.
No element can be accessed randomly; it has to access each node sequentially.
Reverse Traversing is difficult in linked list.
N-!-s-h-a-t
19-08-2020
8
9. Applications of Linked Lists
Linked lists are used to implement stacks, queues, graphs, etc.
Linked lists let you insert elements at the beginning and end of the list.
In Linked Lists we don't need to know the size in advance.
Types of Linked Lists
There are 3 different implementations of Linked List available, they are:
1.Singly Linked List
2.Doubly Linked List
3.Circular Linked List
Let's know more about them and how they are different from each other.
N-!-s-h-a-t
19-08-2020
9
10. Singly linked lists contain nodes which have a data part as well as an address
part i.e. next, which points to the next node in the sequence of nodes.
The operations we can perform on singly linked lists
are insertion, deletion and traversal.
Singly Linked List
Doubly Linked List
In a doubly linked list, each node contains a data part and two
addresses, one for the previous node and one for the next node.
Circular Linked List
In circular linked list the last node of the list holds the address of the
first node hence forming a circular chain.
N-!-s-h-a-t
19-08-2020 10
11. Tree
A binary tree is a hierarchical data structure in which each node has at most two children generally referred as
left child and right child.
Each node contains three components:
1. Pointer to left subtree
2. Pointer to right subtree
3. Data element
The topmost node in the tree is called the root. An empty tree is represented by NULL pointer.
A representation of binary tree is shown:
N-!-s-h-a-t
19-08-2020
11
12. Common Terminologies
•Root: Topmost node in a tree.
•Parent: Every node (excluding a root) in a tree is connected by a directed edge from
exactly one other node. This node is called a parent.
•Child: A node directly connected to another node when moving away from the root.
•Leaf/External node: Node with no children.
•Internal node: Node with atleast one children.
•Depth of a node: Number of edges from root to the node.
•Height of a node: Number of edges from the node to the deepest leaf. Height of the tree
is the height of the root.
In the above binary tree we see that root node is A.
The tree has 10 nodes with 5 internal nodes,
i.e, A,B,C,E,G and 5 external nodes, i.e, D,F,H,I,J. The
height of the tree is 3. B is the parent
of D and E while D and E are children of B.
N-!-s-h-a-t
19-08-2020
12
13. Advantages of Trees
Trees are so useful and frequently used, because they have some very serious advantages:
•Trees reflect structural relationships in the data.
•Trees are used to represent hierarchies.
•Trees provide an efficient insertion and searching.
•Trees are very flexible data, allowing to move subtrees around with minumum effort.
Types of Trees (Based on Structure)
Rooted binary tree: It has a root node and every node has atmost two children.
Full binary tree: It is a tree in which every node in the
tree has either 0 or 2 children.
•The number of nodes, n, in a full binary tree is atleast n =
2h – 1, and atmost n = 2h+1 – 1, where h is the height of
the tree.
•The number of leaf nodes l, in a full binary tree is
number, L of internal nodes + 1, i.e, l = L+1.
N-!-s-h-a-t
19-08-2020
13
14. Perfect binary tree: It is a binary tree in which all interior nodes have two children and all leaves have the
same depth or same level.
•A perfect binary tree with l leaves has n =
2l-1 nodes.
•In perfect full binary tree, l = 2h and n =
2h+1 - 1 where, n is number of nodes, h is
height of tree and l is number of leaf nodes
Complete binary tree: It is a binary tree in which every level, except possibly the last, is completely filled, and
all nodes are as far left as possible.
The number of internal nodes in a
complete binary tree of n nodes
is floor(n/2).
N-!-s-h-a-t
19-08-2020 14
15. Balanced binary tree: A binary tree is height balanced if it satisfies the following constraints:
1.The left and right subtrees' heights differ by at most one, AND
2.The left subtree is balanced, AND
3.The right subtree is balanced
An empty tree is height balanced.
The height of a balanced binary
tree is O(Log n) where n is
number of nodes.
N-!-s-h-a-t
19-08-2020 15
16. Degenarate tree: It is a tree is where each parent node has only one child
node. It behaves like a linked list.
N-!-s-h-a-t
19-08-2020
16