Data Structure and Algorithm Sem 2
Data Structure and Algorithm Sem 2
INTERNAL ASSIGNMENT
SET-I
Q1. What are the characteristics and Building Blocks of an Algorithm? And what are
Control Mechanism and Control structures?
Q2. A. What are Binary trees? How many types of Binary trees are there, discuss?
A binary tree is characterized by the fact that any node can have at most two branches, i.e.,
there is no node with degree greater than two.
Definition: A binary tree is a special case of tree where no node of a tree can have a degree of
more than two. Therefore, a binary tree is a set of zero or more nodes T such that: i) there is a
specially designated node called the root of the tree ii) the remaining nodes are partitioned
into two disjointed sets, T1 and T2, each of which is a binary tree. T1 is called the left subtree
and T2 is called right subtree.
There are 3 types of binary trees:
Full binary tree: A binary tree is a full binary tree if and only if :-
o Each non leaf node has exactly two child nodes
o All leaf nodes are at the same level
Complete binary tree A complete binary tree is a binary tree in which every level, except
possibly the last, is completely filled, and all nodes are as far left as possible. Complete
binary tree from left to right A complete binary tree of depth k is a tree with n nodes in which
these n nodes can be numbered sequentially from 1 to n, as if it would have been the first n
nodes in a full binary tree of depth k.
The most popular way to present a binary tree is Linked storage representation. Binary tree
consists of nodes which can have at most two child, linked representation of such tree will be
stored in the form shown below, where LPTR and RPTR denote the address or locations of
the left and right subtrees respectively of a particular root node.
Bubble sort is a straightforward and simplistic method of sorting data that is used very
commonly. 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 elements to the end of the data
set. It then 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 a simplistic
example. For example, if only one element is not in order, bubble sort will only take 2n time.
If two elements are not in order, bubble sort will only take at most 3n time. Bubble sort
average case and worst case are both O(n²).
Before each pass, the interchange marker EXCHS is initialized to zero. This marker is
incremented each time an interchange is made. If, at the end of a pass, EXCHS has a value of
zero, the sort is complete
Merge sort:
Divide-and conquer is a general algorithm design paradigm: Divide: divide the input data S
in two disjoint subsets S1and S2
Recur: solve the sub problems associated with S1 and S2
Conquer: combine the solutions for S1 and S2 into a solution for S
The base case for the recursion is sub problems of size 0 or 1 Merge-sort on an input
sequence S with n elements consists of three steps:
Divide: partition S into two sequences S1 and S2 of about n/2 elements each
Recur: recursively sort S1 and S2
Conquer: merge S1 and S2 into a unique sorted sequence.
Merge sort algorithm proceeds as follows:
1) Divide the array with the size of m as middle= m/2.
2) Update left from 1 to middle.
3) Update right from middle+1 to m
4) Repeat 1 to 3 until m≤1 5)
Call merge [left, right].
merge_sort[m]
if length[m] ≤ 1
return m
middle length[m] / 2
for x 1 to middle
add x to left
SET-II
Q4. A. What is dynamic memory storage and how is link list stored in memory? Write
the algorithm for traversal of a singly link list.
There are two main memory regions used for dynamic allocation:
Heap: A more flexible memory pool that can grow or shrink as needed during
program execution. It's typically used for dynamically allocated data structures like
linked lists and trees.
Stack: A faster memory region with a fixed size, often used for local variables within
functions. The size of data on the stack is known at compile time.
Linked list is a linear collection of data elements called nodes. Each node is divided into two
parts: the first part is called data field where the data are stored and the second part is called
the link field or next field, contains the address of the next node in the list.
If we represent linked lists in memory it requires two arrays one store the data part and one
for the address part such that DATA [K] and LINK [K] respectively. It also requires a
variable name START which contains the address of the first node and a next pointer sentinel
denoted by NULL which indicates the end of the list. The figure 2.3 shows how the linked
list is stored in memory. The nodes of a list need not occupy adjacent elements in the array
DATA and LINK.
Generally the data field of a node may be a record with more than one data item. In such
case, the data must be store in some type of record structure or in a collection of parallel
arrays.
Traversing a Linked List Traversing a linked list means processing each node of list exactly
once.
Algorithm: Traversing a linked list
1. Set P: = START. [ Initializes pointer PTR]
2. Repeat Step 3 and 4 while P ≠ NULL.
3. Apply PROCESS to DATA [P].
4. Set P := LINK [P] .[P points to the next node] [End of Step 2 loop]
B. What are the different types of link list. Write an algorithm to create circular list.
1. Doubly Linked List: In some situation we need to traverse both forward and backward of a
linked list. The linked list with this property needs two link field one to point the next node is
called next link field and another to point the previous node is called previous link field. The
linked list containing this type of nodes is called doubly linked list or two- way list. shows the
structure of doubly linked list. The operations of doubly linked list are same as one- way list
they are traversing, searching, insertion and deletion.
2. Circularly linked list Circular linked list is the one where the null pointer in 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 i.e. every node is accessible from a given
node. The circular linked list can be implemented both in one- way list and two- way lists.
struct Node {
int data;
Node* next;
};
void insertNode(Node** head, int data) {
Node* newNode = new Node;
newNode->data = data;
// Handle first node
if (*head == NULL) {
newNode->next = newNode;
*head = newNode;
return;
}
Q5. Write the Algorithm to find the maximum and minimum items in a set of n element.
Also explain the working of the algorithm.
function find_max_and_min(set):
if set is empty:
return None, None // or appropriate values indicating an empty set
Initialization:
The algorithm starts by initializing max_item and min_item with the first element of the set.
This is because we need a baseline to compare all other elements against.
Iteration:
The algorithm then iterates through each element in the set. For each element, it checks if the
element is greater than the current max_item. If it is, it updates max_item to this new
element.
Similarly, it checks if the element is less than the current min_item. If it is, it updates
min_item to this new element.
Result:
After completing the iteration through the set, the max_item will contain the highest value
found in the set, and min_item will contain the lowest value.
Example
This algorithm ensures that each element is compared only once, making it efficient with a
time complexity of O(n)O(n)O(n), where nnn is the number of elements in the set.
Q6. A. What is Stack? Discuss the Array implementation of a stack along with push()
and pop() algorithms.
A stack is a data structures in which insertion and deletion of items are made at the one end,
called the top of the stack.
Array implementation of stack:
As per the procedure first it checks 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.
Pop operation
The pop operation is used to remove an item from a stack.
Procedure: POP (STACK, TOP, ITEM)
1. [Stack is empty?]
If TOP = 0, then: Print: UNDERFLOW, and Return.
2. Set ITEM := STACK [TOP] . [Assign Top element to ITEM]
3. Set TOP := TOP – 1. [Decreases Top by 1]
4. Return.
As per the procedure, first it checks 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 is
decremented by 1.
B. What is Queue? Discuss the Array implementation of a queue along with enqueue()
and dequeue() algorithms.
Queue representation
Following are the two methods offered by queue for adding and deleting element from the
queue. enqueue - add a new item at the back of the queue
dequeue - remove the item at the front of the queue
Dequeue Algorithm:
QDELETE (QUEUE, N, FRONT, REAR, ITEM)
1. If FRONT: =NULL, then write : UNDERFLOW and Return.
2. Set ITEM:=QUEUE[FRONT].
3. If FRONT = REAR, then: // only one element.
Set FRONT: =NULL and REAR: =NULL.
Else if FRONT := N then:
Set FRONT: =1.
Else:
Set FRONT: = FRONT +1.
4. Return.