Ds 3rd Internals Answers
Ds 3rd Internals Answers
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
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;
}
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
^ 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
➢ 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].