DS Solution Manual

USN
1 P E
PESIT Bangalore South Campus
Hosur road, 1km before Electronic City, Bengaluru -100
Department of Computer Science And Engineering
INTERNAL ASSESSMENT TEST – 3 Solution Manual
Subject & Code : Data Structures and Applications(15CS33)
Name of faculty : Mr. Sandesh BJ,Mrs.Vandana M L
Section
: B,C
Note: All questions are compulsory
1.
Write a C function for inorder traversal in a binary tree. Trace the code for traversing the
given tree. What is the total no of recursive calls.
Solution:
void inorder(tree_pointer ptr)
/* inorder tree traversal */
{
if (ptr!=NULL) {
inorder(ptr->left_child);
printf(“%d”, ptr->data);
indorder(ptr->right_child);
}
}
B.E. III Semester
8
USN
1 P E
PESIT Bangalore South Campus
Hosur road, 1km before Electronic City, Bengaluru -100
Department of Computer Science And Engineering
Trace operations of In order traversals
Total 19 Calls
2.
Write a C function to delete a node from a BST. Explain all cases with a suitable example
void delete(int key)
{
tree_pointer cur,prev,succ,rep;
if(root==NULL)
{printf(“empty tree”);retrun;}
/* Searching for the node with key value
While(cur!=NULL)
{prev=cur;
if(key==cur->data)break;
B.E. III Semester
8
USN
1 P E
PESIT Bangalore South Campus
Hosur road, 1km before Electronic City, Bengaluru -100
Department of Computer Science And Engineering
if(key<cur->data)cur=cur->left_child;
else cur=cur->right_child;}
if(cur==NULL)
{printf(“Data not found”);return;}
/*case1
If(cur->left_child==NULL)
rep=cur->right_child;
Else if(cur->right_child==NULL)
rep=cur->left_child;
Else
if(cur->left_child!=NULL&&Cur->right_child!=NULL)
{succ=cur->right_child
While(succ->left_child!=NULL)
Succ=succ->left_child;
Succ->left_child=Cur->left_child;
rep=cur->right_child;
}
If(prev==NULL) root=rep;
If(cur=prev->left_child)
Prev->left_child=rep;
Else
Prev->right_child=rep;
B.E. III Semester
USN
1 P E
PESIT Bangalore South Campus
Hosur road, 1km before Electronic City, Bengaluru -100
Department of Computer Science And Engineering
Free (cur);return;
}
3. a
Explain with suitable example different ways to represent a Graph
Solution
4
n
Adjacency Matrix
n
Adjacency Lists
n
Adjacency Multilists
Adjacency Matrix
Adjacency List
B.E. III Semester
USN
1 P E
PESIT Bangalore South Campus
Hosur road, 1km before Electronic City, Bengaluru -100
Department of Computer Science And Engineering
b Write a C function for traversing a graph using BFS
void bfs(int src,int a[10][10],int n)
{
int u,i;
int f=-1,r=-1;
int q[10];
vis[src]=1;
q[++r]=src;
while(f<r)
{
u=q[++f];
for(i=0;i<n;i++)
{
if(a[u][i]==1 && vis[i]==0)
{
vis[i]=1;
q[++r]=i;
}
}
}
}
B.E. III Semester
4
USN
1 P E
PESIT Bangalore South Campus
Hosur road, 1km before Electronic City, Bengaluru -100
Department of Computer Science And Engineering
Write a C program for Radix Sort. Trace the code for the given example
4.
345,654,924,123,567,472,555,808,911
B.E. III Semester
8
USN
1 P E
PESIT Bangalore South Campus
Hosur road, 1km before Electronic City, Bengaluru -100
Department of Computer Science And Engineering
B.E. III Semester
USN
1 P E
PESIT Bangalore South Campus
Hosur road, 1km before Electronic City, Bengaluru -100
Department of Computer Science And Engineering
5. a
Explain different Hash functions with an example
1.Division Function
•
Idea:
•
Map a key k into one of the m slots by taking the remainder of k divided by
m
h(k) = k mod m
•
Advantage:
•
•
fast, requires only one operation
Disadvantage:
•
Certain values of m are bad, e.g.,
•
power of 2
•
non-prime numbers
2.Multiplication Method
Idea:
•
Multiply key k by a constant A, where 0 < A < 1
•
Extract the fractional part of kA
•
Multiply the fractional part by m
•
Take the floor of the result
h(k) =
= m (k A mod 1)
•
Disadvantage: Slower than division method
•
Advantage: Value of m is not critical, e.g., typically 2p
3.Folding:
•
B.E. III Semester
Partition the identifier x into several parts, and add the parts together to
4
USN
1 P E
PESIT Bangalore South Campus
Hosur road, 1km before Electronic City, Bengaluru -100
Department of Computer Science And Engineering
obtain the hash address, e.g., x=12320324111220
•
Partition x into 123,203,241,112,20; And return the address
123+203+241+112+20=699
4.Digit analysis:
•
If all the keys have been known, then we could delete the digits of keys
having the most skewed distributions, and use the rest digits as hash
address.
b What do you mean by Threaded Binary Tree. Give the node representation for threaded
binary tree
Solution:
typedef struct threaded_tree {
short int left_thread;
threaded_tree *left_child;
char data;
threaded_tree *right_child;
short int right_thread; };
typedef struct threaded_tree* threaded_pointer;
B.E. III Semester
4
USN
1 P E
PESIT Bangalore South Campus
Hosur road, 1km before Electronic City, Bengaluru -100
Department of Computer Science And Engineering
********
B.E. III Semester
B
USN
1 P E
PESIT Bangalore South Campus
Hosur road, 1km before Electronic City, Bengaluru -100
Department of Computer Science And Engineering
*
/
A
B.E. III Semester
C
E