PRACTICAL-04
Name:Jayendra
Class:SE-C
Batch:C2
Roll no.:S213042
Problem Statement:
Creating a Tree finding its longestpath, displaying it,
mirror ,maximum&minimum,searching.
Code:
#include<iostream>
using namespace std;
class node
{
public:
int info;
node *left;
node *right;
}*root;
class BST
{
public:
node* root;
BST(){
root = NULL;
}
void insert(node* tree, node* newnode)
{
if (tree == NULL)
{
tree = newnode;
if (root == NULL)
{
root = tree;
cout << "Root Node is Added\n";
}
return;
}
if (tree->info == newnode->info)
{
cout << "Element already exists in the tree\n";
return;
}
if (tree->info > newnode->info)
{
if (tree->left != NULL)
{
insert(tree->left, newnode);
}
else
{
tree->left = newnode;
cout << "Node added to the left\n";
}
}
else
{
if (tree->right != NULL)
{
insert(tree->right, newnode);
}
else
{
tree->right = newnode;
cout << "Node added to the right\n";
}
}
}
void display(node* ptr, int level)
{
if (ptr != NULL)
{
display(ptr->right, level + 1);
cout << endl;
if (ptr == root)
{
cout << "Root->: " << ptr->info;
}
else
{
for (int i = 0; i < level; i++)
{
cout << " ";
}
cout << ptr->info;
}
display(ptr->left, level + 1);
}
}
int findLongestPath(node* tree)
{
if (tree == NULL)
return 0;
int leftHeight = findLongestPath(tree->left);
int rightHeight = findLongestPath(tree->right);
return max(leftHeight, rightHeight) + 1;
}
int findMinValue(node* tree)
{
if (tree == NULL)
return -1;
node* current = tree;
while (current->left != NULL)
{
current = current->left;
}
return current->info;
}
int findMaxValue(node* tree)
{
if (tree == NULL)
return -1;
node* current = tree;
while (current->right != NULL)
{
current = current->right;
}
return current->info;
}
bool searchValue(node* tree, int value)
{
if (tree == NULL)
return false;
if (tree->info == value)
return true;
if (value < tree->info)
return searchValue(tree->left, value);
return searchValue(tree->right, value);
}
void mirror(node* tree){
node* temp;
if(tree!=NULL){
temp=tree->left;
tree->left=tree->right;
tree->right=temp;
mirror(tree->left);
mirror(tree->right);
}
}
};
int main()
{
int ch;
BST b;
node* temp;
while (1)
{
cout << "....operations....\n";
cout << "[Link]\n";
cout << "[Link]\n";
cout << "[Link] path\n";
cout << "[Link] & maximum value\n";
cout << "[Link] a element\n";
cout<<"[Link]\n";
cout << "Enter your choice: ";
cin >> ch;
switch (ch)
{
case 1:
temp = new node();
cout << "\nEnter number to be inserted: ";
cin >> temp->info;
[Link]([Link], temp);
break;
case 2:
cout << "Tree is: \n";
[Link]([Link], 1);
cout << endl;
break;
case 3:
cout << "Longest Path (Height) is: " << [Link]([Link]) << endl;
break;
case 4:
cout << "Minimum value in the tree is: " << [Link]([Link]) << endl;
cout << "Maximum value in the tree is: " << [Link]([Link]) << endl;
break;
case 5:
{
int searchVal;
cout << "Enter value to search: ";
cin >> searchVal;
if ([Link]([Link], searchVal))
cout << "Value " << searchVal << " found in the tree.\n";
else
cout << "Value " << searchVal << " not found in the tree.\n";
}
break;
case 6:
[Link]([Link]);
[Link]([Link],1);
cout<<endl;
break;
default:
cout << "Invalid choice!\n";
}
}
return 0;
}
Output: