Name:
Reg No:
DATA STRUCTURE AND ALGORITHM (THEORY)
FINAL EXAMINATION
QUESTION NO: 01
CODE:
import java.util.Scanner;
public class LinkListInsertionAndBSTCreation {
class Node{
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
class NodeBST {
int key;
NodeBST left, right;
public NodeBST(int data){
key = data;
left = right = null;
// BST root node
NodeBST root;
// Constructor for BST =>initial empty tree
LinkListInsertionAndBSTCreation(){
root = null;
//Represent the head and tail of the singly linked list
public Node head = null;
public Node tail = null;
//addAtStart() will add a new node to the beginning of the list
public void addAtStart(int data) throws IllegalAccessException{
//Create a new node
Node newNode = new Node(data);
//Checks if the list is empty
if(head == null) {
//If list is empty, both head and tail will point to new node
head = newNode;
tail = newNode;
else {
if (head.data == data){
throw new IllegalAccessException(Integer.toString(data));
//Node temp will point to head
Node temp = head;
//newNode will become new head of the list
head = newNode;
//Node temp(previous head) will be added after new head
head.next = temp;
public void insertNth(int data, int position) throws IllegalAccessException{
//create new node.
Node newNode = new Node(data);
if (head == null) {
//if head is null and position is zero then exit.
if (position != 0) {
return;
} else { //node set to the head.
head = newNode;
tail = newNode;
if (head != null && position == 0) {
if (head.data == data){
throw new IllegalAccessException(Integer.toString(data));
newNode.next = this.head;
this.head = newNode;
return;
Node current = this.head;
Node previous = null;
if(position > 0){
int i = 1;
while (i < position) {
if (current.data == data){
throw new IllegalAccessException(Integer.toString(data));
}
previous = current;
current = current.next;
if (current == null) {
break;
i++;
newNode.next = current;
previous.next = newNode;
else{
tail.next = newNode;
//newNode will become new tail of the list
tail = newNode;
//display() will display all the nodes present in the list
public void display() {
//Node current will point to head
Node current = head;
if(head == null) {
System.out.println("List is empty");
return;
System.out.println("Nodes of the list: ");
while(current != null) {
//Prints each node by incrementing pointer
System.out.print(current.data + " ");
current = current.next;
System.out.println();
public int getLength() {
//Node current will point to head
Node current = head;
int listLength = 0;
if(head == null) {
return 0;
while(current != null) {
current = current.next;
listLength++;
return listLength;
}
// insert a node in BST
void insert(int key) {
root = insert_Recursive(root, key);
//recursive insert function
NodeBST insert_Recursive(NodeBST root, int key) {
//tree is empty
if (root == null) {
root = new NodeBST(key);
return root;
//traverse the tree
if (key < root.key) //insert in the left subtree
root.left = insert_Recursive(root.left, key);
else if (key > root.key) //insert in the right subtree
root.right = insert_Recursive(root.right, key);
// return pointer
return root;
void inorder() {
inorder_Recursive(root);
}
// recursively traverse the BST
void inorder_Recursive(NodeBST root) {
if (root != null) {
inorder_Recursive(root.left);
System.out.print(root.key + " ");
inorder_Recursive(root.right);
public void createTree(){
LinkListInsertionAndBSTCreation bstree = new LinkListInsertionAndBSTCreation();
Node current = head;
System.out.println("Adding nodes to the BST: ");
while(current != null) {
bstree.insert(current.data);
current = current.next;
bstree.inorder();
public static void main(String[] args) {
LinkListInsertionAndBSTCreation sList = new LinkListInsertionAndBSTCreation();
Scanner sc = new Scanner(System.in);
try {
sList.addAtStart(7);
sList.addAtStart(2);
sList.addAtStart(6);
sList.addAtStart(8);
sList.insertNth(4, -1);
sList.insertNth(10, -1);
sList.insertNth(1, -1);
sList.insertNth(9, -1);
sList.insertNth(3, -1);
catch (IllegalAccessException e) {
System.out.println("Duplicate Entry into list ---> "+e.getMessage());
/*while (true) {
try{
System.out.println("Enter Element and position :");
int e = sc.nextInt();
int p = sc.nextInt();
sList.insertNth(e, p);
System.out.println("Go again? enter y to continue, n to stop ");
String goAgain = sc.next();
if (!goAgain.equals("n") || !goAgain.equals("N")) {
break;
catch (IllegalAccessException e) {
System.out.println("Duplicate Entry into list ---> "+e.getMessage());
}
} */
sList.display();
int listlength = sList.getLength();
// System.out.println(listlength);
if(listlength >= 10){
sList.createTree();
OUTPUT:
QUESTION NO: 02
a)Division method with m=97
it using mod with the keys to generate code
Hash address with divisioon method is
For mouseKey 4614%97=55
For KeyboardKey 5882%97=62
For cableKey 6713%97=20
For webcamKey 4409%97= 44
For monitorKey 1825%97=79
b) Mid square method
Hash address
For mouseKey 4614*4614=21288996=88
For KeyboardKey 5882*5882=34597924=97
For cableKey 6713*6713=45064369=64
For webcamKey 4409*4409 =19439281=39
For monitorKey 1825*1825=3330625= 30
it multiply keys and pick the middle two elements
c) Folding method without reversing
Hash address with Folding method without reversing is
For mouseKey 4+6+1+3=15
For KeyboardKey5+8+8+2=23
For cableKey6+7+1+3=17
For webcamKey4+4+0+9=17+1=18(could be done)
For monitorKey1+8+2+5=16
digits in the keys are added to generate code
d) Folding method with reversing
Hash address with Folding method with reversing is
For mouseKey 4+6+1+3=15 reverse =51
For KeyboardKey5+8+8+2=23 reverse 32
For cableKey6+7+1+3=17 reverse=71
For webcamKey4+4+0+9=17 reverse =71+1=72(could be done)
For monitorKey1+8+2+5=16 reverse 61
digits in the keys are reversed and are added to generate code
Program in java
class hashCode
public static void main(String args[])
int mouseKey = 4614;
int KeyboardKey =5882;
int cableKey =6713;
int webcamKey = 4409;
int monitorKey = 1825;
//division method with m=97
//uses mod97
System.out.println("Hash address with divisioon method is ");
System.out.println("For mouseKey" +mouseKey%97);
System.out.println("For KeyboardKey"+KeyboardKey%97);
System.out.println("For cableKey"+cableKey%97);
System.out.println("For webcamKey"+webcamKey%97);
System.out.println("For monitorKey"+monitorKey%97);
System.out.println();
System.out.println("Hash address with mid square method is ");
System.out.println("For mouseKey" +gethash(mouseKey));
System.out.println("For KeyboardKey"+gethash(KeyboardKey));
System.out.println("For cableKey"+gethash(cableKey));
System.out.println("For webcamKey"+gethash(webcamKey));
System.out.println("For monitorKey"+gethash(monitorKey));
System.out.println();
System.out.println("Hash address with Folding method without reversing is ");
System.out.println("For mouseKey" +gethashFoldingMethod(mouseKey));
System.out.println("For KeyboardKey"+gethashFoldingMethod(KeyboardKey));
System.out.println("For cableKey"+gethashFoldingMethod(cableKey));
System.out.println("For webcamKey"+gethashFoldingMethod(webcamKey));
System.out.println("For monitorKey"+gethashFoldingMethod(monitorKey));
System.out.println();
System.out.println("Hash address with Folding method with reversing is ");
System.out.println("For mouseKey" +gethashFoldingMethodR(mouseKey));
System.out.println("For KeyboardKey"+gethashFoldingMethodR(KeyboardKey));
System.out.println("For cableKey"+gethashFoldingMethodR(cableKey));
System.out.println("For webcamKey"+gethashFoldingMethodR(webcamKey));
System.out.println("For monitorKey"+gethashFoldingMethodR(monitorKey));
System.out.println();
static long gethash(int key)
//example for key 4614
key = key * key; //key *key = 21288996
key = key / 1000; //key/1000=21288
key = key % 100; // key%100= 21288%100= 88
return key; //88 is returned
static int gethashFoldingMethod(int key)
int sum=0;
int r=0;
for(int i=key;i>0;i=i/10) //to extract digit
{
r=i%10;
sum=sum+r;
return sum;
static int gethashFoldingMethodR(int key)
int sum=0;
int r=0;
for(int i=key;i>0;i=i/10) //to extract digit
r=i%10;
sum=sum+r;
int rev=0;
for(int i=sum;i>0;i=i/10) //to extract digit and reverse
r=i%10;
rev=(rev*10)+r;
return rev;
}
}
OUTPUT:
QUESTION NO: 03(a)
Answers.
A priority queue can have any implementation, like a array that you search linearly when you
pop. All it means is that when you pop you get the value with either the minimum or the
maximum depending. A classic heap as it is typically referred to is usually a min heap.
An implementation that has good time complexity (O(log n) on push and pop) and no memory
overhead.
QUESTION NO: 03(b)
QUESTION NO: 04(a)
Consider the cut of a graph G, such that one particular vertex v is on one side of cut and
remaining V-v vertices are on other side of cut.
In the minimum spanning tree, either vertex v could be a leaf node of MST or it could act as a
bridge to connect two component of minimum spanning tree. Also MST is unique when every
edge weight are distinct.
Let e be minimum weight edge that is incident on v.
If vertex v is leaf node of MST, then vertex v will be connected with remaining portion of MST
by least weight edge e which is incident on v to minimize the total weight of MST. So minimum
weight edge e incident on v will be taken for MST.
If vertex v is not leaf node of MST then it must be connecting two connected components of
MST. In this case also, edge e will be connected with one of the connected component of MST to
minimize the total weight of MST and another edge will connect other connected component of
MST.
So in either case, the minimum weight edge which is incident on any vertex v must be the part of
MST.
Since we know that for every vertex having minimum weight edge incident on it must be the part
of MST. So since there are |V| vertices in the graph, we can select |V|/2 edges such that each of
these edges is minimum weight edge incident on atleast one vertex out of 2 vertices that this
edge connect. Hence we could select |V|/2 edges and then contract these edges to reduce the size
of graph to |V|/2 vertices and edge size will also reduce.
Since contracting one edge of graph requires transformation of graph which could take O(|V|+|
E|) time as per operations mentioned in question. So contacting |V|/2 edges will require O(|V|(|V|
+|E|)) time complexity.
Since this algorithm perform the same task on new graph with |V|/2 vertices, so the time
complexity recurrence will be
T(|V|) = T(|V|/2) + O(|V|2+|V||E|),
which gives T(|V|) = O(|V|2+|V||E|)
QUESTION NO: 04(b)
A graph is a data structure that consists of the following two components:
1. A finite set of vertices also called as nodes.
2. A finite set of ordered pair of the form (u, v) called as edge. The pair is ordered because (u, v)
is not the same as (v, u) in case of a directed graph(di-graph). The pair of the form (u, v)
indicates that there is an edge from vertex u to vertex v. The edges may contain
weight/value/cost.
Graphs are used to represent many real-life applications: Graphs are used to represent networks.
The networks may include paths in a city or telephone network or circuit network. Graphs are
also used in social networks like linkedIn, Facebook. For example, in Facebook, each person is
represented with a vertex(or node). Each node is a structure and contains information like person
id, name, gender, and locale. See this for more applications of graph.
Following is an example of an undirected graph with 5 vertices.
The following two are the most commonly used representations of a graph.
1. Adjacency Matrix
2. Adjacency List
There are other representations also like, Incidence Matrix and Incidence List. The choice of
graph representation is situation-specific. It totally depends on the type of operations to be
performed and ease of use.
Adjacency Matrix:
Adjacency Matrix is a 2D array of size V x V where V is the number of vertices in a graph. Let
the 2D array be adj[][], a slot adj[i][j] = 1 indicates that there is an edge from vertex i to vertex j.
Adjacency matrix for undirected graph is always symmetric. Adjacency Matrix is also used to
represent weighted graphs. If adj[i][j] = w, then there is an edge from vertex i to vertex j with
weight w.