Professional Documents
Culture Documents
Cia 2 Answer
Cia 2 Answer
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 :
ANS :
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 :
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 :
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 :
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]<<" ";
}
}