Data Structure and Algorithm
Data Structure and Algorithm
Question 02
(A) What are Binary trees? How many types of Binary trees are
there, discuss?
(B) Discuss the linked storage representation of binary tree.
A binary tress is a rooted tree that is also an ordered tree in which every node has at
most two children. A rooted tree naturally imparts a notion of levels, thus for every
node a notion of children may be defined as the nodes connected to it level below.
Types of binary trees:-
Skewed binary tree:-
A binary tree which has only left subtree is called left skewed tree. It has only right
subtree is called right skewed tree.
Skewed trees are not sufficient in memory management because generally an n node
binary tree needs an array whose length is between n+1 and 2n. but while storing
skewed binary tree it wastes most of the memory space.
Question 03
Explain the algorithms of Bubble sort and Merge sort.
Bubble sort:-
Bubble sort is a straightforward and simplistic method of sorting data that is used very
commonly. This algorithm is not suitable for large data sets as its average and worst-
case time complexity is quite high.
Bubble sort algorithm starts at the beginning of the data set. It compares the first two
elements and if the first is greater than the second, then it swaps them. It continues
doing this for each pair of adjacent element to the end of the data set. Then it starts
again with the first two elements, repeating until no swaps have occurred on the last
pass.
This algorithm is highly inefficient and is rarely used except as simplistic.
Procedure of Bubble sort.
1.[initialize] LAST N
2.[Loop on pass index]
Repeat thru step 5 for pass = 1,2, … N-1
3.[initialize exchange counter for this pass]
EXCHS 0
4. [ perform pairwise comparisons on unsorted elements]
Repeat for 1 = 1,2,…. LAST-1
If K[1] >k[i+1]
Then K[i] > K[i+1]
EXCHS EXCHS +1
5. [were any exchanges made on this pass]
If EXCHS=0
Then return
Else LAST LAST-1
6.[finished]
Return.
In this algorithm the interchange marker EXCHs is initialized to zero. This maker is
incremented each time an interchange is made. If at the end of the pass, EXCHS has a
value of zero, the sort us complete.
An understand with example, consider an array:
50, 90, 34,55,22,53
In the first step we focus on first two element which are compared and swapped. In
this case, since the element at index 1 is larger than the one at index 0, no swap takes
place.
50, 90, 34,55,22,53
Then focus is move to the elements at index 1 and 2 which are compared and swapped
In our example, 67 is larger than 12 so the two elements are swapped.
The process is repeated until the focus moves to the end of the array, at which point
the largest of all the elements end up at the highest possible index. The remaining
steps and result are:
50, 34, 90,55,22,53
You can see that one complete bubble step move the largest element to the last
position.
Merge sort:-
The merge sort algorithm is a sorting algorithm that is based on the Divide and
Conquer paradigm. In this algorithm, the array is initially divided into two equal
halves and then they are combined in a sorted manner.
Merge sort algorithm processed as follow:
Divide the array with size of m as middle =m/2
Update left from 1 to middle
Update right from middle +1 to m
Repeat 1 to 3 until m<=1
Call merge.
Merge_sort[m]
If length[m] <=1
Return m
Middle length[m] /2
For x 1 to middle
Add x to left
For x middle to m
Add x t oright
Left merge_sort[left]
Right merge_sort[right]
result merge[left,right]
return result
merge[left,right]
while length [left]>0 or length[right]>0
if length[left]>0 and length[right]>0
if first[left]<=first[right]
append first[left] to result
left rest[left]
else
append first[right] to result
right rest[right]
else if length[left]>0
append first[left] to result
left rest[left]
else if length[right] >0
append first[right] to result
right rest[right]
end while
return result
Given input with the size of m=8.
7234564
Algorithm start with dividing the array until m<=1.
7234|564
7 2 | 3 4 |5 6 | 4
7|2|3|4|5|6|4
After dividing the given element process processed with merge sort. While
merging, the element will also be sorted in the order and the result will be stored
in the resultant array.
7 2 3 4 5 6 8
2 7 3 4 5 6 8
2 3 4 7 5 6 8
2345678
Question 04
(A)What is dynamic memory storage and how is link list stored in
memory? Write the algorithm for traversal of a singly link list.
(B) What are the different types of link list. Write an algorithm to
create circular list.
What is dynamic memory storage and how is link list stored in memory? Write
the algorithm for traversal of a singly link list.
Dynamically storage usually placed in a program segment known as the heap or the
free store. Exact amount of space or number of items does not have to be known by
the compiler in advance.
It requires a new node and executing two next pointer maneuvers to store a link list.
Consider node N is to be inserted into list between node A and B. It does not take into
account that the memory space for the new node N will come from AVAIL list.so the
first node in the AVAIL will be used as the new node N.
Now AVAIL points to the second node in the free storage, where node N previously
pointed. That’s how link stored in memory.
Algorithm: traversing a singly link list.
1. Set P = START
2.Reapting step 3 and 4 while P not equal to NULL.
3. Apply PROCESS to DATA[P]
4. Set P = LINK[P], [ P points to the next node]
[End of step 2 loop]
5. Exit.
What are the different types of link list. Write an algorithm to create circularly
list.
There are four types of linked list:
1. Singly linked list:-
A singly linked list is unidirectional linked list. It is data structure that stores as
sequence of element. Each element in the list is called a node, and each node has a
reference to the next node in the list.
To create singly linked list we need to create a node class and linked list class.
To add an element in to the list we need to create a new node and set the next
reference of the previous tail node to point to the new node.
2. Doubly linked list:-
A doubly linked list is a bi-directional list. So we can traverses it in both directions.
This linked list need needs two link field one is to point the next node is called nect
link field and another to point the previous node is called previous link field.
Here the first nodes previous link field and the last nodes next field are marked as
null.
Previou Data field Next link
s link field
field
3. Circularly linked list:-
Circularly linked list is the one where the null pointer is the last node is replaced with
the address of the first node so that it forms a circle.
The advantage of this circular linked list is easy accessibility of node.
You can traverse it only one direction.
Circular linked list ideal for applications where data needs to be processed in a
continuous loop.
The figure shows a singly circular list where the link of the last node is points to the
first node.
4. Doubly circular linked list:-
Doubly circular linked list has properties of both doubly linked list and circular linked
list in which two consecutive elements are linked or connected by the previous and
next pointer and the last node points to the first node by the next pointer and also the
first node points to the last node but the previous pointer.
Question 05
Write the Algorithm to find the maximum and minimum items in a
set of n element. Also explain the working of the algorithm.
Algorithm to find the maximum and minimum item in a set of n elements.
Question 06
(A) What is Stack? Discuss the Array implementation of a stack
along with push() and pop() algorithm.
(B) What is Queue? Discuss the Array implementation of queue
along with enqueue() and dequeue() algorithm.
What is Stack? Discuss the Array implementation of stack along with push() and
pop() algorithm.
A stack is a data structure in which insertion and deletion of items are made at the one
end.
There are two basic operations in stack it’s called push and pull.
Push is used to insert an item into a stack. And pull used to delete an item from stack.
Array implementation of stack along with push() and pop() algorithm:-
Implementation of stack can be done either using array or one way list.
Main disadvantage of array implementation of stack is that the size of the array has to
be declared a head.
Push() algorithm:-
The push operation is used to add an item into a stack.
Before executing push operation must check for the OVERFLOW condition. Since
the stack is not full the TOP pointer is incremented by 1, TOP=TOP+1. So, now TOP
points to a new location and then the ITEM is inserted into that position.
PUSH(STACK, TOP, MAXSTK, ITEM)
Step 1. [Stack already full?]
If TOP=MAXSTK, then :print: OVERFLOW, and return.
Step 2. Set TOP = TOP+1.[ Increases TOP by 1]
Step 3. Set STACK[TOP] = ITEM. [Insert ITEM in new TOP position]
Step 4. Return.
Pop() algorithm:-
The pop operation is used to remove an item from stack.
Before executing the pop operation, one must check for the UNDERFLOW
condition.
First must check for the underflow condition. Since the stack is not empty the top
element in the stack is assigned to ITEM, ITEM = STACK[TOP]. Then the top
decreased by 1.
POP(STACK, TOP, ITEM)
Step 1. [STACK is empty?]
If TOP = 0, then: print: UNDERFLOW, and return.
Step 2. Set ITEM = STACK [TOP]. [Assign Top element to ITEM]
Step 3. Set TOP = TOP-1. [Decreases Top by 1]
Step 4. Return.
(B) What is Queue? Discuss the Array implementation of queue along with
enqueue() and dequeue() algorithm.
A Queue is defined as a linear data structure that is open at both end and the
operations are performed in First In First Out order.
Here are two methods offered by queue for adding and deleting element from the
queue.
One is an enqueue, it adds a new item at the back of the queue.
Second is a remove, it removes the item at the front of the queue.
Array implementation of queue along with enqueue() and dequeue() algorithm:-
Implementation of array starts with initializing variable FRON and REAR. Whenever
the element is deleted from the queue the value of the FRONT is increased by 1 this
can be implemented by FRONT= FRONT+1. Similarly, whenever an element is
added to the queue, the value of REAR id increased by 1 by assigning REAR =
REAR+1.
Enqueue() algorithm:-
After the N number of insertions, the rear element of the queue will occupy
QUEUE[N]. if we insert a new element when REAR= N, it is simply moving the
entire queue to the beginning of the array and change FRONT AND REAR
accordingly an then insert an item.
Before insertion we need to check the overflow status of the queue and find the
current position of REAR and increment is the new position for inserting a new item.
Dequeue() algorithm:-
Dequeue check for the underflow status if queue contains item it deleted an first
element from the queue by assigning it to the variable ITEM and calculate new value
for REAR.
Before deleting an element we need to check the underflow condition front ==1 to
check whether there is at least one element available for the deletion or not.
END