Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

1.i. Develop a program to display the elements of tree in preorder traversal.

Input Format
The input should have numbers (until the user enters a negative value)
Output Format
The output prints the preorder traversal.

ANS :
#include<bits/stdc++.h>
using namespace std;
struct node
{
int data;
node*left=NULL;
node*right=NULL;
};
struct node*root;
void insert(int data)
{
node*newnode=new node();
newnode->data=data;
if(root==NULL)
{
root=newnode;
}
else
{
node*temp=root;
node*parentnode=NULL;
while(temp!=NULL)
{
parentnode=temp;
if(temp->data>data)
{
temp=temp->left;
}
else
{
temp=temp->right;
}
}
if(parentnode->data>data)
{
parentnode->left=newnode;
}
else
{
parentnode->right=newnode;
}
}
}
int display(node* n)
{
node*temp=n;
if(temp==NULL)
return 0;
cout<<temp->data<<" ";
display(temp->left);
display(temp->right);
}
int main()
{
int data;
while(1)
{
cin>>data;
if(data>0)
{
insert(data);
}
else
break;
}
cout<<"Preorder Traversal:\n";
display(root);
}

2.i. Create a binary search tree consisting of only positive values & print the height of the
tree.
Note
An infinite line of integer input representing the nodes of the binary search tree. If any value
less than 1 is encountered, the input loop must break and print the height of the tree.
Input
2310
Output
2
Explanation
This BST has 2 levels in it. 2 is the root and 3 and 1 are the right and left child respectively.
Hence the height is 2.

ANS :

// You are using GCC


#include <iostream>
#include <algorithm> // For std::max
// Define the structure for a binary tree node
struct TreeNode {
int data;
TreeNode* left;
TreeNode* right;
};
// Function to create a new node
TreeNode* createNode(int value) {
TreeNode* newNode = new TreeNode;
newNode->data = value;
newNode->left = nullptr;
newNode->right = nullptr;
return newNode;
}
// Function to insert a value into the binary search tree
TreeNode* insert(TreeNode* root, int value) {
if (root == nullptr) {
return createNode(value);
}
if (value < root->data) {
root->left = insert(root->left, value);
} else if (value > root->data) {
root->right = insert(root->right, value);
}
return root;
}
// Function to calculate the height of the BST
int getHeight(TreeNode* root) {
if (root == nullptr) {
return 0; // Height of an empty tree is 0
}
int leftHeight = getHeight(root->left);
int rightHeight = getHeight(root->right);
return 1 + std::max(leftHeight, rightHeight); // Height is max of left and right
}
int main() {
TreeNode* root = nullptr;
int data;
std::cin>>data;
root = insert(root, data);
while(true){
std::cin>>data;
if(data>0)
insert(root, data);
else
break;
}
int treeHeight = getHeight(root);
std::cout <<treeHeight << std::endl;
return 0;
}
3.i. Given a connected undirected graph. Perform a Depth First Traversal of the graph.
Note
Use recursive approach.
Input
2
54
01020324
43
011203
Output
01243
0123
Explanation
Visit all the vertices using depth first search algorithm using 0 as a source node.

ANS :

// You are using GCC


// You are using GCC
#include<bits/stdc++.h>
using namespace std;
void addedge(vector<int>graph[],int v1,int v2)
{
graph[v1].push_back(v2);
graph[v2].push_back(v1);
}
void dfs(vector<int>graph[],bool visited[],int v)
{
visited[v]=true;
cout<<v<<" ";
for(int adj:graph[v])
{
if(!visited[adj])
dfs(graph,visited,adj);
}
}
int main()
{
int t,v,e;
cin>>t;
while(t--)
{
int v,e;
cin>>v>>e;
vector<int>graph[v];
bool visited[v];
for(int i=0;i<e;i++)
{
int v1,v2;
cin>>v1>>v2;
addedge(graph,v1,v2);
}
cout<<endl;
dfs(graph,visited,0);
}
}

4.i. Build a program to implement hashing by double hashing and search for an element in
it.
Note: Consider the table size as 10.
Input Format
The first line of the input consists of the value of n.
The next n inputs are the elements.
The last input is the element to be searched.
Output Format
The first line of the output prints the result of the search.
The output prints the hash table.

ANS :

// You are using GCC


#include<bits/stdc++.h>
using namespace std;
void insert(int ht[],int data)
{
int p=0,index;
int funt1=data%10;
int funt2=7-(data%7);
index=funt1;
while(ht[index]!=0)
{
p++;
index=(funt1+(p*funt2))%10;
}
ht[index]=data;
}
int main()
{
int s;
cin>>s;
int hashtable[10];
for(int i=0;i<10;i++)
{
hashtable[i]=0;
}
for(int i=0;i<s;i++)
{
int data;
cin>>data;
insert(hashtable,data);
}
int n;
cin>>n;
int c=0,in;
for(int i=0;i<10;i++)
{
if(n==hashtable[i])
{
c++;
in=i;
}
}
if(c==1)
{
cout<<"value is found at index "<<in<<endl;
}
else
{
cout<<"value is not found"<<endl;
}
for(int i=0;i<10;i++)
{
cout<<"Element at position "<<i<<": "<<hashtable[i]<<endl;
}
}

5.i. Build a program to implement hashing by double hashing and search for an element in
it.
Note: Consider the table size as 10.
Input Format
The first line of the input consists of the value of n.
The next n inputs are the elements.
The last input is the element to be searched.
Output Format
The first line of the output prints the result of the search.
The output prints the hash table.

ANS :

// You are using GCC


#include<bits/stdc++.h>
using namespace std;
void insert(int ht[],int data)
{
int p=0,index;
int funt1=data%10;
int funt2=7-(data%7);
index=funt1;
while(ht[index]!=0)
{
p++;
index=(funt1+(p*funt2))%10;
}
ht[index]=data;
}
int main()
{
int s;
cin>>s;
int hashtable[10];
for(int i=0;i<10;i++)
{
hashtable[i]=0;
}
for(int i=0;i<s;i++)
{
int data;
cin>>data;
insert(hashtable,data);
}
int n;
cin>>n;
int c=0,in;
for(int i=0;i<10;i++)
{
if(n==hashtable[i])
{
c++;
in=i;
}
}
if(c==1)
{
cout<<"value is found at index "<<in<<endl;
}
else
{
cout<<"value is not found"<<endl;
}
for(int i=0;i<10;i++)
{
cout<<"Element at position "<<i<<": "<<hashtable[i]<<endl;
}
}

6.i. Develop a C++ program to find the maximum element in the tree.
Input Format
The input should have numbers (until the user enters a negative value)
Output Format
The output prints the maximum element.

ANS :

#include<bits/stdc++.h>
using namespace std;
struct node
{
int data;
node*right=NULL;
node*left=NULL;
};
struct node* root;
void insert(int data)
{
node* newnode=new node();
newnode->data=data;
if(root==NULL)
{
root=newnode;
}
else
{
node* temp=root;
node* parentnode=NULL;
while(temp!=NULL)
{
parentnode=temp;
if(temp->data>data)
{
temp=temp->left;
}
else
{
temp=temp->right;
}
}
if(parentnode->data>data)
parentnode->left=newnode;
else
parentnode->right=newnode;
}
}
int display(node*root)
{
node* temp=root;
if(temp==NULL)
{
return 0;
}
display(temp->left);
cout<<temp->data<<" ";
display(temp->right);
}
int main()
{
int data;
while(1)
{
cin>>data;
if(data>0)
{
insert(data);
}
else
break;
}
cout<<"Tree values are:\n";
display(root);

7.i. Given a directed graph. The task is to do a Breadth-First Search of this graph.
Input
1
54
01020324
Output
01234
Explanation
V = 5, E = 4
01
02
03
24

ANS :

// You are using GCC


#include<bits/stdc++.h>
using namespace std;
void addedge(vector<int>graph[],int v1,int v2)
{
graph[v1].push_back(v2);
}
void BFS(vector<int>graph[],bool visited[],int v)
{
queue<int>q;
q.push(v);
visited[v]=true;
while(!q.empty())
{
int curr=q.front();
q.pop();
cout<<curr<<" ";
for(int adj:graph[curr])
{
if(!visited[adj])
{
q.push(adj);
visited[adj]=true;
}
}
}
}
int main()
{
int t,v,e;
cin>>t;
while(t--)
{
int v,e;
cin>>v>>e;
vector<int>graph[v];
bool visited[v];
for(int i=0;i<e;i++)
{
int v1,v2;
cin>>v1>>v2;
addedge(graph,v1,v2);
}
cout<<endl;
BFS(graph,visited,0);
}
}

8.i. Build a program to implement linear probing hashing to insert an element into hash
table.
Note
Use hash function as arr[i]%n [n -number of elements]
Number of elements to be insert is same as size of hash table
Input
5 // Table size and number of elements to be insert
5 6 4 8 12
Output
5 6 12 8 4
Explanation
Use the above hash function to store value inside hash table. Print the value from zeroth
index.

ANS :

#include<bits/stdc++.h>
using namespace std;
void insert(int ht[],int data,int s)
{
int p=0;
int hf=(data+p)%s;
while(ht[hf]!=-1)
{
if(p==s-1)
p=-1;
p++;
hf=(data+p)%s;
}
ht[hf]=data;
}
int main()
{
int s;
cin>>s;
int hashtable[s];
for(int i=0;i<s;i++)
hashtable[i]=-1;
for(int i=0;i<s;i++)
{
int data;
cin>>data;
insert(hashtable,data,s);
}
for(int i=0;i<s;i++)
{
if(hashtable[i]!=-1)
cout<<hashtable[i]<<" ";
}
}

You might also like