0% found this document useful (0 votes)
3 views

Data Structure Updated Corections (2) (1)

The document is a question paper on data structures, covering various topics such as abstract data types, trees, stacks, sorting algorithms, and linked lists. It includes definitions, operations, and examples for concepts like height-balanced trees, insertion sort, queues, graphs, and circular linked lists. Additionally, it contains programming tasks related to binary search trees, binary search algorithms, and evaluating postfix expressions.

Uploaded by

rohanmore19105
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Data Structure Updated Corections (2) (1)

The document is a question paper on data structures, covering various topics such as abstract data types, trees, stacks, sorting algorithms, and linked lists. It includes definitions, operations, and examples for concepts like height-balanced trees, insertion sort, queues, graphs, and circular linked lists. Additionally, it contains programming tasks related to binary search trees, binary search algorithms, and evaluating postfix expressions.

Uploaded by

rohanmore19105
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

1

Data Structure Question Paper1 with Answers

Q1. Attempt any Eight (out of Ten)

a) What is an abstract data type?

An abstract data type (ADT) is a model for data types where only
behavior is defined, not implementation.

b) Define Tree.

A tree is a hierarchical data structure consisting of nodes, with a single


node as the root. Each node contains data and links to its child nodes.
Trees are used to represent data with a hierarchical relationship such as
folder structures, organization charts, etc.

Inorder, Preorder, Postorder, and Level Order.

c) What are the different operations performed on stack?

Push, pop, peek/top, isEmpty, and isFull.

d) List out different types of sorting?

Bubble sort, Insertion sort, Selection sort, Merge sort, Quick sort, Heap
sort.

e) What is pointer to pointer?

A pointer that stores the address of another pointer variable.

f) What is self-referential structure?

A structure that contains a pointer to a structure of the same type.

g) Define Data structure.

A data structure is a way of organizing and storing data in a computer


so that it can be accessed and modified efficiently.
2

h) Explain typedef keyword with syntax or example.

`typedef int marks;` creates alias 'marks' for 'int'.

i) What is a complete binary tree?

A binary tree where every level, except possibly the last, is completely
filled and all nodes are as far left as possible.

j) List out different types of linked list:

There are four main types of linked lists:

1. Singly Linked List:

Each node contains two parts: data and a pointer to the next node. It
only allows traversal in one direction.

2. Doubly Linked List:

Each node contains three parts: data, a pointer to the next node, and a
pointer to the previous node. It allows traversal in both forward and
backward directions.

3. Circular Linked List:

In this type, the last node points back to the first node, forming a circle.
It can be singly or doubly circular.

4. Circular Doubly Linked List:

Similar to a doubly linked list, but the last node points back to the first
and vice versa, forming a bidirectional circular list.
3

Q2) Attempt any Four of the following (out of Five):

a) What is height-balanced tree? Explain LL, LR, RR and RL rotations


with an example.

A height-balanced tree (AVL tree) is a binary search tree where the


difference between the heights of the left and right subtrees of every
node is at most one.

**LL Rotation (Left-Left):**


Occurs when a node is inserted into the left subtree of the left child. A
right rotation is applied.
Example: Inserting 10 → 5 → 2.

10
/
5
/
2

Becomes:

5
/\
2 10

**LR Rotation (Left-Right):**


Occurs when a node is inserted into the right subtree of the left child.
Example: Inserting 10 → 5 → 7.
Requires left rotation on 5, then right rotation on 10.

10
/
5
\
4

Becomes:

7
/\
5 10

**RR Rotation (Right-Right):**


Occurs when a node is inserted into the right subtree of the right child.
A left rotation is applied.
Example: Inserting 10 → 15 → 20.

10
\
15
\
20

Becomes:

15
/ \
10 20

**RL Rotation (Right-Left):**


Occurs when a node is inserted into the left subtree of the right child.
Example: Inserting 10 → 15 → 13.
Requires right rotation on 15, then left rotation on 10.

10
\
15
/
5

13

Becomes:

13
/ \
10 15

A height-balanced tree, also known as an AVL tree, is a self-balancing


binary search tree where the difference between the heights of the left
and right subtrees of any node is at most one.

LL Rotation (Left-Left Rotation):


Occurs when a node is inserted into the left subtree of the left child,
causing imbalance. To rebalance, a right rotation is performed.
Example: Insert 10, then 5, then 2:

10 5
/ --> /\
5 2 10
/
2

LR Rotation (Left-Right Rotation):


Occurs when a node is inserted into the right subtree of the left child. To
rebalance, a left rotation on the left child, followed by a right rotation on
the current node, is done.
Example: Insert 10, then 5, then 7:

10 10 7
/ --> / --> /\
5 7 5 10
\ /
7 5
6

A binary tree where for every node, height difference of left and right
subtrees is at most one.

b) Explain insertion sort technique with an example.

Insertion sort is a simple sorting algorithm that builds the final sorted
array one item at a time.

Example:
Input: [5, 2, 4, 6, 1, 3]
Sorted: [1, 2, 3, 4, 5, 6]

Algorithm:
1. Iterate from the second element to the end.
2. Compare current element to its predecessor.
3. If smaller, move the predecessor ahead.
4. Insert the current element at the correct position.

c) What is queue? Explain different operations performed on queue.


A queue is a linear data structure that follows FIFO (First In First Out)
principle.

Operations:
1. Enqueue: Add an element to the rear.
2. Dequeue: Remove an element from the front.
3. Front: Get the front element.
4. Rear: Get the last element.
5. isEmpty: Check if queue is empty.

d) What is Graph? Explain adjacency list of graph.


A graph is a non-linear data structure consisting of nodes (vertices) and
edges.
7

Adjacency List:
An array of lists is used. Size of array is equal to number of vertices.
Each entry list represents a vertex and contains all adjacent vertices.

Example:
Graph:
0→1
0→4
1→2
1→3

Adjacency List:
0: 1 → 4
1: 2 → 3

e) Write an algorithm to convert given infix expression to postfix


expression.
Algorithm:
1. Initialize an empty stack for operators and empty list for output.
2. Read tokens in the infix expression:
- If operand, add to output.
- If '(', push to stack.
- If ')', pop till '(' is found.
- If operator, pop operators with higher precedence from stack and
add to output.
3. Pop all remaining operators to output.

Example:
Infix: A + B * C
Postfix: A B C * +

newNode->data = data;
if (pos == 1) {
newNode->next = head;
8

return newNode;
}
struct Node* temp = head;
for (int i = 0; i < pos - 2 && temp != NULL; i++)
temp = temp->next;
newNode->next = temp->next;
temp->next = newNode;
return head;
}

Q3)

a) Write a function to create and display circular singly linked list.

Answer: A circular singly linked list is a linear data structure where the
last node points back to the first node. Unlike a singly linked list, it does
not end at NULL. This structure is useful when we want to continuously
loop through data.

Operations:

1. Creation:

Allocate memory for new nodes.

Set the next pointer of each node to point to the next node.

Set the next pointer of the last node to point to the first node (head).

2. Display:

Start from the head node.

Continue traversing using next pointer until we reach back to head.

Algorithm:

Function createCircularList(n):

Create head and temp pointers as NULL


9

For i = 1 to n:

Create newNode

Assign data to newNode

If head is NULL:

head = newNode

temp = newNode

Else:

temp.next = newNode

temp = newNode

End For

temp.next = head // Circular link

Return head

Function displayCircularList(head):

Set temp = head

Do:

Print temp.data

temp = temp.next

While temp != head


10

c) Write a function to traverse a graph using DFS technique.

Answer: Depth First Search (DFS) is a fundamental graph traversal


method that explores as far along a branch as possible before
backtracking. It uses stack (explicit or recursive) and visited array.

DFS Algorithm (using adjacency matrix):

Function DFS(graph, vertex, visited):

Mark visited[vertex] = true

Process vertex (e.g., print it)

For each adjacent_vertex of vertex:

If visited[adjacent_vertex] == false:

Call DFS(graph, adjacent_vertex, visited)

Steps:

1. Start from a source vertex.

2. Mark it as visited.

3. Recursively visit all unvisited adjacent vertices.

d) Write a function to insert a node in linked list at given position.

Answer: Inserting at a given position in a singly linked list involves


pointer adjustments and proper traversal.

Steps:

1. Create a new node with the given data.

2. If inserting at position 1:

Make new node point to head.


11

Update head to new node.

3. Else:

Traverse to (position - 1) node.

Adjust pointers to insert new node.

Algorithm:

Function insertAtPosition(head, data, pos):

Create newNode with data

If pos == 1:

newNode.next = head

head = newNode

Else:

temp = head

For i = 1 to pos-2:

temp = temp.next

newNode.next = temp.next

temp.next = newNode

e) Write a function to check whether a given string is palindrome or not


(use Stack).

A palindrome is a string that reads the same forward and backward.

C Program using Stack:

#include <stdio.h>
12

#include <string.h>

#define MAX 100

char stack[MAX];
int top = -1;

void push(char c) {
stack[++top] = c;
}

char pop() {
return stack[top--];
}

int main() {
char str[MAX], rev[MAX];
printf("Enter a string: ");
gets(str);

for (int i = 0; i < strlen(str); i++)


push(str[i]);
for (int i = 0; i < strlen(str); i++)
rev[i] = pop();
rev[strlen(str)] = '\0';

if (strcmp(str, rev) == 0)
printf("Palindrome\n");
else
printf("Not a Palindrome\n");
return 0;
}
13

Q4) Attempt any Four of the following (out of Five):

b) Construct Binary search tree of following data: 15, 30, 20, 5, 10, 2, 7

BST Insertion Order:


15
|-- 5
| |-- 2
| |-- 10
| |-- 7
|-- 30
|-- 20

c) Write a function to search the element from the array using binary
search.

C Program:
#include <stdio.h>

int binarySearch(int arr[], int l, int r, int x) {


while (l <= r) {
int mid = l + (r - l) / 2;
if (arr[mid] == x)
return mid;
if (arr[mid] < x)
l = mid + 1;
else
r = mid - 1;
}
return -1;
}
14

int main() {
int arr[] = {2, 5, 7, 10, 15, 20, 30};
int x = 10;
int result = binarySearch(arr, 0, 6, x);
if (result != -1)
printf("Element found at index %d", result);
else
printf("Element not found");
return 0;
}

d) What is a circular queue? Explain it with an example.


A circular queue is a linear data structure in which the last position is
connected back to the first position.
Operations wrap around if the end of the array is reached.

Example: Insertion after end moves to front if front elements are


dequeued.

e) Write a ‘C’ Program to count the number of nodes from singly linked
list.
C Program:
#include <stdio.h>
#include <stdlib.h>

struct Node {
int data;
struct Node* next;
};

int countNodes(struct Node* head) {


int count = 0;
while (head != NULL) {
15

count++;
head = head->next;
}
return count;
}

Q5

a) Evaluate the expressions postfix expression where a=2,b=10,c=4,d=1

i) AB-CD*/

A * B + C / D = 2 * 10 + 6 / 2 = 20 + 3 = 23

ii) ABC*+

AB-CD/* = (2 * 10 - 6 / 2) = (20 - 3) = 17

b) Define the following terms:

i) Bridge: An edge whose removal increases the number of connected


components in the graph.

ii) Cyclic graph: A graph that contains at least one cycle.

iii) Pendant vertex: A vertex with only one edge (degree = 1).

You might also like