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

Ds 3rd Internals Answers

Uploaded by

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

Ds 3rd Internals Answers

Uploaded by

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

DATA STRUCTURES AND APPLICATIONS

3RD INTERNALS
SECTION - A
1. Define binary tree and write different types of binary tree with examples.
➢ Binary tree
A tree in which every node can have a maximum of two children is called binary tree. In binary a tree,
every node can have either o children or 1 child or 2 children but not more than 2 children.
Example

➢ Types of Binary tree


• Strictly binary tree
A binary tree in which every node has either two or zero number of children is called Strictly Binary Tree.
Strictly binary tree is also called as Full Binary Tree or Proper Binary Tree or 2-Tree.

• Complete binary tree


A Complete Binary Tree is a type of binary tree in which all levels, except possibly the last, are completely
filled, and all nodes are as far left as possible.

• Extended binary tree


A binary tree can be converted into Full Binary tree by adding empty nodes to existing nodes wherever
required. The full binary tree obtained by adding empty nodes to a binary tree is called as Extended Binary
Tree.
• Full binary tree
A full Binary tree is a special type of binary tree in which every parent node/internal node has either two
or no children.

2. Write the function to perform the deletion from binary search tree and inserting the
element into binary search tree.
➢ Inserting the element into binary tree
NODE insert(int item, NODE root){
NODE temp, start, prev;
temp = getnode();
temp->info = item;
temp->llink = NULL;
temp->rlink = NULL;
if (root == NULL)
return temp;
prev = NULL;
start = root;
while (start != NULL){
prev = start;
if (item == start->info){
printf("duplicate items not allowed");
free(temp);
return root;
}
if (item < start->info)
start = start->llink;
else
start = start->rlink;
}
if (item < prev->info)
prev->llink = temp;
else
prev->rlink = temp;
return root;
}

➢ Deletion from binary search tree


NODE delete(int item, NODE root){
NODE cur, parent, suc, q;
if (root == NULL) {
printf("Tree empty");
return root;
}
parent = NULL;
cur = root;
while (cur != NULL){
if (item == cur->info)
break;
parent = cur;
if (item < cur->info)
cur = cur->llink;
else
cur = cur->rlink;
}
if (cur == NULL){
printf("Item not found");
return root;
}
if (cur->llink == NULL)
q = cur->rlink;
else if (cur->rlink == NULL)
q = cur->llink;
else {
suc = cur->rlink;
while (suc->llink != NULL)
suc = suc->llink;
cur->info = suc->info;
if (cur->rlink == suc)
cur->rlink = suc->rlink;
else
suc->llink = cur->llink;
free(suc);
return root;
}
if (cur == parent->llink)
parent->llink = q;
else
parent->rlink = q;
free(cur);
return root;
}

3. For the given list construct the binary tree (EXAMPLE)


➢ 100, 85, 45, 55, 110, 20, 70, 65, 50

100

85 110

45

20 55

50 70

65
➢ Inorder: B C A E D G H F I
Preorder: A B C D E F G H I

B D

C E F

G I

4. Construct binary tree for the given expression (EXAMPLE)


➢ ((6+(3-2)*5)^2+3

^ 3

+ 2

* 6

- 5

3 2
5. For the given sparse matrix write the diagrammatic linked list representation.

6. Define threaded binary tree and discuss the in threaded binary tree.
➢ Threaded binary tree
In a binary tree more than 50% of link fields have null values and more space is wasted by
the presence of null values. These link field which contain null character can be replaced
by address of some nodes in the tree which facilitate upward movement in the tree. These
extra links which contain addresses of some nodes are called threads and the tree is termed
as threaded binary tree.
➢ In-threaded binary tree
In a binary tree if link of any node contains null if it is replaced by the address of in-order predecessor,
then the resulting tree is called left in-threaded binary tree. In a binary tree if rlink of any node contains
null and if it is replaced by the in-order successor the resulting tree is called right in-threaded tree. An in-
threaded binary tree or in-order threading of a binary tree is the once which is both left in-threaded and
right in-threaded.
SECTION – B

1. Write the function for BFS and DFS


➢ BFS
#include<stdio.h>
#include<stdlib.h>
int f,r,a[10][10],q[20],v,u,n;
int s[10]={0};
void bfs(){
r=-1,f=0;
printf("Nodes visited from %d\n",u);
s[u]=1;
q[++r]=u;
printf("%d\t",u);
while(f<=r){
u=q[f++];
for(v=1;v<=n;v++){
if(a[u][v]==1){
if(s[v]==0){
printf("%d\t",v);
q[++r]=v;
s[v]=1;
}
}
}
}
}
int main(){
printf("Enter the number of nodes:");
scanf("%d",&n);
printf("Enter adjacency matrix:\n");
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
scanf("%d",&a[i][j]);
}
}
printf("Enter the starting node:");
scanf("%d",&u);
bfs();
return 0;
}

➢ DFS
#include<stdio.h>
#include<stdlib.h>
int f,r,a[10][10],q[20],v,u,n;
int visited[10]={0};
void dfs(int u){
visited[u]=1;
for(v=1;v<=n;v++){
if(visited[v]==0 && a[u][v]==1){
printf("%d\t",v);
dfs(v);
}
}
}
int main(){
printf("Enter the number of nodes:");
scanf("%d",&n);
printf("Enter adjacency matrix:\n");
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
scanf("%d",&a[i][j]);
}
}
printf("Enter the starting node:");
scanf("%d",&u);
printf("Nodes visited from %d \n ",u);
printf("%d\t",u);
dfs(u);
return 0;
}
2. Define graph for the given graph show the adjacency matrix and adjacent list
representation.
➢ Graph
A Graph is a non-linear data structure consisting of vertices and edges. The vertices are sometimes also
referred to as nodes and the edges are lines or arcs that connect any two nodes in the graph. More formally
a Graph is composed of a set of vertices( V ) and a set of edges( E ). The graph is denoted by G(E, V).
➢ (EXAMPLE)
B

A
A E
C
A A
A
D F
A A

3. What is collision. What are the methods to resolve the collision. Explain the linear
probing with an example.
➢ Collision
In hash tables, collision resolution is a method used to handle situations where two or more keys hash to
the same index is called collision. There are several techniques for collision resolution.
➢ Methods to resolve collision
There are two types of collision resolution techniques.
• Separate chaining (open hashing)
Separate chaining is a method used to handle collisions in hash tables. When a collision occurs at a slot,
a linked list is created to store all the keys that hash to that slot. New keys are simply added to the linked
list. This approach is particularly useful when the number of keys to be added or removed is
unpredictable, as it provides flexibility in managing collisions.
• Open addressing (closed hashing)
Open addressing is a collision-resolution technique used in hash tables to prevent collisions. Instead of
storing keys outside the table, all keys are kept within the hash table itself. This means the table size must
always be larger than the number of keys. Open addressing is also called closed hashing. It includes
several sub-methods:
o Linear Probing
o Quadratic Probing
o Double Hashing
➢ Linear probing
Linear probing resolves collisions in hash tables by checking the next available slot sequentially until an
empty one is found. It keeps all keys within the table and avoids separate chains. However, it may cause
clustering, slowing searches and insertions if the table is too full.
➢ Example
Insert the following sequence of keys in the hash table
{9, 7, 11, 13, 12, 8}
Use linear probing technique for collision resolution
h(k, i) = [h(k) + i] mod m
h(k) = 2k + 5
m=10
• First Draw an empty hash table of Size 10.
The possible range of hash values will be [0, 9].

First Key to be inserted in the hash table = 9.


h(k) = 2k + 5
h(9) = 2*9 + 5 = 23
h(k, i) = [h(k) + i] mod m
h(9, 0) = [23 + 0] mod 10 = 3
So, key 9 will be inserted at index 3 of the hash table
Next Key to be inserted in the hash table = 7.
h(k) = 2k + 5
h(7) = 2*7 + 5 = 19
h(k, i) = [h(k) + i] mod m
h(7, 0) = [19 + 0] mod 10 = 9
So, key 7 will be inserted at index 9 of the hash table

Next Key to be inserted in the hash table = 11.


h(k) = 2k + 5
h(11) = 2*11 + 5 = 27
h(k, i) = [h(k) + i] mod m
h(11, 0) = [27 + 0] mod 10 = 7
So, key 11 will be inserted at index 7 of the hash table

Next Key to be inserted in the hash table = 13.


h(k) = 2k + 5
h(13) = 2*13 + 5 = 31
h(k, i) = [h(k) + i] mod m
h(13, 0) = [31 + 0] mod 10 = 1
So, key 13 will be inserted at index 1 of the hash table

Next key to be inserted in the hash table = 12.


h(k) = 2k + 5
h(12) = 2*12 + 5 = 27
h(k, i) = [h(k) + i] mod m
h(12, 0) = [27 + 0] mod 10 = 7
Here Collision has occurred because index 7 is already filled.
Now we will increase i by 1.
h(12, 1) = [27 + 1] mod 10 = 8
So, key 12 will be inserted at index 8 of the hash table.
Next key to be inserted in the hash table = 8.
h(k) = 2k + 5
h(8) = 2*8 + 5 = 21
h(k, i) = [h(k) + i] mod m
h(8, 0) = [21 + 0] mod 10 = 1
Here Collision has occurred because index 1 is already filled.
Now we will increase i by 1 now i become 1.
h(k) = 2k + 5
h(8) = 2*8 + 5 = 21
h(k, i) = [h(k) + i] mod m
h(8, 0) = [21 + 1] mod 10 = 2
index 2 is vacant so 8 will be inserted at index 2.

4. Write the function for the optimal binary search tree.


int optimalBST(int keys[], int freq[], int n) {
int cost[n][n];
for (int i = 0; i < n; i++) {
cost[i][i] = freq[i];
}
for (int length = 2; length <= n; length++) {
for (int i = 0; i <= n - length; i++) {
int j = i + length - 1;
cost[i][j] = INT_MAX;
int freqSum = 0;
for (int k = i; k <= j; k++) {
freqSum += freq[k];
}
for (int r = i; r <= j; r++) {
int c = (r > i ? cost[i][r - 1] : 0) +
(r < j ? cost[r + 1][j] : 0) +
freqSum;
if (c < cost[i][j]) {
cost[i][j] = c;
}
}
}
}
return cost[0][n - 1];
}

You might also like