Problem Set 1D
Problem Set 1D
Problem Set 1D
1. Write a program to implement binary search tree with the following operations:
Insertion
Deletion
Search
Input:
1 x Insert x in the tree
2 x Delete x from the tree
3 x Search x in the tree
Sample Input:
1 10
1 20
1 30
15
2 12
2 20
3 20
3 30
Sample Output:
12 is not present in the tree
20 deleted
CS6103 SS Lab Problem Set 1D Page 2 of 4
20 not found
30 found
2. Write a program to implement following traversal in a binary search tree.Insert the elements in the
order given.
Postorder
Preorder
Inorder
Input:
First line contains number of test cases. Each test cases contains the value N, the total number of
nodes and next line contains N values each indicating values to be inserted in BST in given order.
Sample Input:
1
6
123450
Sample Output:
Postorder: 0 5 4 3 2 1
Preorder: 1 0 2 3 4 5
Inorder: 0 1 2 3 4 5
3. Perform level order traversal in a binary search tree. Insert the elements in the order given.
Input:
First line contains number of test cases. Each test cases contains the value N, the total number of
nodes and next line contains N values each indicating values to be inserted in BST in given order.
Sample Input:
1
8
4 8 1 7 2 10 15 11
Sample Output:
4 1 8 2 7 10 15 11
4. Construct a undirected graph with its adjacency list representation. Print the list.
Input:
First line contains number of test cases. Each test cases contains two integers denoting the no of
vertices(n) and edges(m).Next m lines denote pair of vertices.Vertex ordering follow the natural number
sequence starting from 1
Output:
For each vertex print the vertices with which it is connected.
Sample Input:
1
46
14
13
12
23
Cont.
CS6103 SS Lab Problem Set 1D Page 3 of 4
24
34
Sample Output:
1 -> 2 3 4
2 -> 1 4 3
3 -> 1 4 2
4 -> 1 2 3
Output:
Print the shortest distance from source to every other vertex.
Sample Input:
1
9
040000080
4 0 8 0 0 0 0 11 0
080704002
0 0 7 0 9 14 0 0 0
0 0 0 9 0 10 0 0 0
0 0 4 14 10 0 2 0 0
000002016
8 11 0 0 0 0 1 0 7
002000670
0
Sample Output:
0 4 12 19 21 11 9 8 14
N.B. For Floyd Warshall start vertex will not be given. You have to display the resultant matrix.
Cont.
CS6103 SS Lab Problem Set 1D Page 4 of 4
Output:
For each test case print the weight of the minimum spanning tree.
Sample Input:
1
46
141
134
125
236
243
342
Sample Output:
Prim: 6
Kruskal: 6
Input:
Input:
First line contains number of test cases. Each test cases contains two integers denoting the no of
vertices(n) and edges(m).Next m lines denote the pair of vertices representing edge. Next line contains
the source vertex.Vertex ordering follow the natural number sequence starting from 0
Output:
For each test case print the BFS and DFS traversal in two lines
Sample Input:
1
46
01
02
12
20
23
33
0
Sample Output:
DFS -> 0 1 2 3
BFS -> 0 1 2 3
The End.