BCSL 33 em
BCSL 33 em
di
ea
R
e
lin ks
n o
O b o
o r -
f E
b n d
u a
H
h e
T
1
B.C.S.L.-33
Data and File Structures Lab
Disclaimer/Special Note: These are just the sample of the Answers/Solutions to some of the Questions given in the
Assignments. These Sample Answers/Solutions are prepared by Private Teacher/Tutors/Authors for the help and guidance
of the student to get an idea of how he/she can answer the Questions given the Assignments. We do not claim 100%
accuracy of these sample answers as these are based on the knowledge and capability of Private Teacher/Tutor. Sample
answers may be seen as the Guide/Help for the reference to prepare the answers of the Questions given in the assignment.
As these solutions and answers are prepared by the private Teacher/Tutor so the chances of error or mistake cannot be
g
denied. Any Omission or Error is highly regretted though every care has been taken while preparing these Sample Answers/
n
Solutions. Please consult your own Teacher/Tutor before you prepare a Particular Answer and for up-to-date and exact
i
information, data and solution. Student should must read and refer the official study material provided by the university.
a d
Q. 1. Write an algorithm and program that creates a Binary Search Tree with a set of given inputs. Then,
e
it should prompt for a key value and print a message about the presence of key value in the Binary Search
R
Tree.
e
Ans. Algorithm:
in ks
If root = NULL
l
return NULL;
n o
If number==root->data
O o
return root->data;
r - b
If number <root->data
o
return search(root->left)
If number > root->data
f E
b d
return search(root->right)
Program:
u a n
H
#include<stdio.h>
#include<stdlib.h>
struct node
{
h e
int data;
struct node* left; T
struct node* right;
};
struct node* createNode(value){
struct node* newNode=malloc(sizeof(struct node));
newNode->data = value;
newNode-> = N ULL;
2
newNode-> right= NULL;
return newNode;
}
struct node* insert(struct node* root, int data)
{
if (root ==NULL) return createNode(data);
if (data<root->data)
root->left = insert (root->left, data);
else if (data > root-> data)
root->right = insert(root->right, data);
return root;
g
}
n
void inorder(struct node* root){
if (root ==NULL) return;
di
a
inorder(root->left);
e
printf("%d->", root->data);
R
inorder(root->right);
}
int main(){
e
in ks
struct node *root = NULL;
root = insert (root,8);
insert (root,3); l
n o
insert (root,1);
O b o
insert (root,6);
o r -
f E
insert (root,7);
d
insert (root,10);
insert (root,14);
insert (root,4); b
u a n
inorder(root);
H
e
}
h
The output of the program will be
T
1->3 ->4 ->6 ->7 ->8 ->10 ->14 ->
Binary Search Tree, is a node-based binary tree data structure which has the following properties:
The left subtree of a node contains only nodes with keys lesser than the node’s key.
The right subtree of a node contains only nodes with keys greater than the node’s key.
The left and right subtree each must also be a binary search tree.
There must be no duplicate nodes.
3
n g
di
ea
The above properties of Binary Search Tree provide an ordering among keys to that the operations like search,
minimum and maximum can be done fast. If there is no ordering, then we may have to compare every key to search
a given key.
R
e
Searching a key: To search a given key in Binary Search Tree, we first compare it with root, if the key is present
in ks
at root, we return root. If key is greater than root’s key, we recur for right subtree of root node. Otherwise we recur
for left subtree.
l
n o
// C function to search a given key in a given BST
O
struct node* search(struct node* root, int key)
b o
{
o r -
f E
// Base Cases: root is null or key is present at root
d
if (root==NULL| | root->key==key)
return root;
// key is greater than root’t key b
u a n
if (root-> key < key)
H
e
return search(root->right, key);
T h
//Key is smaller than root’t key
return search(root->left, key);
}
Illustration to search 6 in below tree:
1. Start from root.
2. Compare the inserting element with root, if less then root, then recurse for left, else recurse for right.
3. If element to search is found anywhere, return true, else return false.
4
n g
di
ea
Insertion of a key: A new key is always inserted at leaf. We start searching a key from root till we hit a leaf node.
R
Once a leaf node is found, the new node is added as a child of the leaf node.
e
lin ks
n o
O b o
o r -
f E
b d
// C program to demonstrate insert operation in binary search tree
u a n
#include<stdio.h>
#include<stdlib.h>
H
struct node
e
{
h
int key;
struct node *left, *right;
};
T
// A utility function to create a new BST node
struct node *newNode(int item)
{
struct node *temp = (struct node*) malloc (sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
5
// A utility function to do inorder traversal of BST
void inorder(struct node *root)
{
if (root ! = NULL)
{
inorder (root->left);
printf("%d\n", root->key);
inorder(root->right);
}
}
/*A utility function to insert a new node with given key in BST*/
struct node* insert (struct node* node, int key)
{
g
/* If the tree is empty, return a new node*/
n
if (node ==NULL) return newNode (key);
/* Otherwise, recur down the tree*/
if (key < node-> key)
di
node->left = insert (node->left, key);
else if (key > node->key)
ea
R
node->right = insert (node->right, key);
/* return the (unchanged) node pointer */
return node;
e
in ks
}
// Driver Program to test above functions
in main() l
n o
O o
{
r - b
/* Let us create following BST
fo E
b n d
u a
H
e
struct node *root = NULL;
h
root = insert(root, 50);
T
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);
// print inoder traversal of the BST
inorder (root);
return 0;
}
6
Output:
20
30
40
50
60
70
80
Illustration to insert 2 in below tree:
1. Start from root.
2. Compare the inserting element with root, if less than root, then recurse for left, else recurse for right.
3. After reaching end, just insert that node at left (if less than current) else right.
n g
di
ea
R
e
lin ks
n o
O b o
o r -
f E
b n d
u a
Time Complexity: The worst case time complexity of search and insert operations is O(h) where h is height of
H
Binary Search Tree. In worst case, we may have to travel from root to the deepest leaf node. The height of skewed
e
tree may become n and the time complexity of search and insert operation may become O(n).
h
Some Interesting Facts:
Inorder traversal of BST always produces sorted output.
T
We can construct a BST with only Preorder or Postorder or Level Order traversal. Note that we can always
get inorder traversal by sorting the only given traversal.
Number of unique BSTs with n distinct keys is Catalan Number.
Q. 2. Write an algorithm and program for multiplication of two Polynomials.
Ans. Algorithm:
multiply (A[0.m-1], B[0..n01])
(1) Create a product array prod[] of size m+n–1.
(2) Initialize all entries in prod[] as 0.
(3) Travers array A [] and do following for every element A[i]
7
...(3.a) Traverse array B[] and do following for every element B[j]
prod[i+] = prod [i+] + A[i] * B[j]
(4) Return prod[].
Programme:
//C++ implementation of the above approach
#include <bits/stdc++.h>
using namespace std;
//Node structure containing powerer
//and oefficient of variable
struct Node {
int coeff, power;
Node* next;
};
g
// Function add a new node at the end of list
n
Node* addnode(Node* start, int coeff, int power)
{
// Create a new node
di
Node* newnode = new Node;
newnode->coeff=coeff;
ea
R
newnode->power = power;
newnode->next=NULL;
// If linked list is empty
e
in ks
if (start == NULL)
return newnode;
//If linked list has nodes l
n o
O o
Node* ptr = start;
r - b
while (ptr->next! = NULL)
o
ptr = ptr->next;
ptr->next = newnode;
f E
b d
return start;
n
}
u a
// Function To Display The Linked list
H
void printList (struct Node*ptr)
e
{
while (ptr->next!= NULL) {
ptr = ptr->next; T h
cout <<ptr->coeff<<“x^” << ptr-> power<<“+”
}
cout << ptr->coeff<<“\n”;
}
\\Function to add coefficients of
\\two elements having same powerer
void remove Duplicates(Node*start)
8
{
Node *ptr1, *ptr2, *dup;
ptr1 = start;
\*Pick elements one by one*/
While (ptr 1 ! =NULL && ptr1->next!=NULL) {
ptr2 = ptr1;
}
e
in ks
else
}
ptr2 = ptr2-> next;
l
n o
O o
ptr2=ptr2->next;
r - b
}
o
ptr1=ptr1->next;
}
f E
b d
}
u a n
// Function two Multiply two polynomial Numbers
H
Node* multiply(Node* poly1, Node* poly2,
e
Node* poly3)
{
T h
// Create two pointer and store the
// address of 1st and 2nd polynomials
Node *ptr1, *ptr2;
ptr1 = poly1;
ptr2 = poly2;
while (ptr1 ! = NULL) {
while (ptr2! = NULL) {
in coeff, power;
// Multiply the coefficient of both
// polynomials and store it in coeff
9
coeff = ptr1->coeff* ptr2->coeff;
//Add the powerer of both polynomials
// and store it in power
power = ptr1->power + ptr2->power;
// Invoke addnode function to create
// a newnode by passing three parameters
poly3 = addnode (poly3, coeff, power);
// move the pointer of 2nd polynomial
// two get it next term
ptr2 = ptr2->next;
}
// Move the 2nd pointer to the
// starting point of 2nd polynomial
g
ptr2 = poly2;
n
// move the pointer of 1st polynomial
ptr1 = ptr1->next;
}
di
// this function will be invoke to add
// the coefficient of the elements
ea
R
while (ptr->next !=NULL) {
// having same powerer from the resultant linked list
removeDuplicates(poly3);
e
in ks
return poly3;
}
// Driver Code l
n o
O o
int main ()
r - b
{
o
Node *poly1 = NULL, *poly2 = NULL, *poly3 = NULL;
f E
// Creation of 1st Polynomial: 3×^2 + 5×^1 + 6
b d
poly1 = addnode (poly1, 3, 2);
n
poly1 = addnode (poly1, 5, 1);
poly1 = addnode(poly1, 6, 0);
u a
H
e
// Creation of 2nd polynomial: 6×^1 + 8
poly2 = addnode (poly2, 6, 1);
T h
poly2 = addnode (poly2, 8, 0);
10
printList (poly3);
return 0;
}
Output:
1st Polynomial:- 3×^2 + 5×^1 + 6
2nd Polynomial:-6×^1 + 8
Resultant Polynomial: 18×^2 + 54×^2 + 76×^1 + 48.
n g
di
ea
R
e
lin ks
n o
O b o
o r -
f E
b n d
u a
H
h e
T
11