0% found this document useful (0 votes)
32 views

Assignment 4

The document describes creating and traversing a binary search tree (BST). It includes functions to insert nodes, find the minimum/maximum values, determine the longest path, search for a value, and swap left/right pointers. The code provided builds a sample BST with 5 nodes and demonstrates several of the functions.

Uploaded by

Vaishnavi Kaware
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Assignment 4

The document describes creating and traversing a binary search tree (BST). It includes functions to insert nodes, find the minimum/maximum values, determine the longest path, search for a value, and swap left/right pointers. The code provided builds a sample BST with 5 nodes and demonstrates several of the functions.

Uploaded by

Vaishnavi Kaware
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

“Group B

Assignment no.2
Beginning with an empty binary search tree, Construct binary search tree by inserting the
values in the order given. After constructing a binary tree -
i. Insert new node
ii. Find number of nodes in longest path from root
iii. Minimum data value found in the tree
iv. Change a tree so that the roles of the left and right pointers are swapped at every node
v. Search a value.”

#include <iostream.h>
class node
{
public:
int data;
node *left;
node *right;
};
class bst
{
node *root;
public:
bst()
{
root=NULL;
}
node *create();
void insert(node *,node *);
void inorder(node *);
void smallest_value();
void largest_value();
1
void search(node *,int);
void swap(node *);
int longest_path(node *);
};
node *bst::create()
{
int n,value;
cout<<"\nEnter no. of nodes for BST: ";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"\nEnter value for node: ";
cin>>value;
node *p=new node;
p->data=value;
p->left=NULL;
p->right=NULL;
insert(root,p);
}
return root;
}

void bst::insert(node *x,node *p)


{
if(root==NULL)
{
root=p;
cout<<"\nRoot is added..!!";
}
else

2
{
if(x->data<p->data)
{
if(x->right==NULL)
x->right=p;
else
insert(x->right,p);
}
else
{
if(x->left==NULL)
x->left=p;
else
insert(x->left,p);
}
}
}
void bst::inorder(node *x)
{
if(x!=NULL)
{ inorder(x->left);
cout<<x->data<<" ";
inorder(x->right);
}
}
void bst::search(node *x,int key)
{ if(x==NULL)
cout<<"\nKey Not Found..!!";
else
{

3
if(key<x->data)
search(x->left,key);
else
{
if(key>x->data)
search(x->right,key);
else
cout<<"\nKey Found";
}
}
}
void bst::largest_value()
{
if(root==NULL)
{
cout<<"\nTree is empty";
}
else
{
node *temp=root;
while(temp->right!=NULL)
{
temp=temp->right;
}
cout<<"\nLargest value is= "<<temp->data;
}
}

void bst::smallest_value()
{

4
if(root==NULL)
{
cout<<"\nTree is empty..!!";
}
else
{
node *temp=root;
while(temp->left!=NULL)
{
temp=temp->left;
}
cout<<"\nSmallest value is= "<<temp->data;
}
}

int bst::longest_path(node *x)


{
int height_left,height_right;
if(x==NULL)
return 0;
if(x->left==NULL && x->right==NULL)
return 1;
height_left=longest_path(x->left);
height_right=longest_path(x->right);
if(height_left>height_right)
{
return (height_left+1);
}
else
{

5
return(height_right+1);
}
}

void bst::swap(node *x)


{
if(x==NULL)
{
cout<<"\nTree is empty..!!";
return;
}
else
{
node *temp=x;
temp=x->left;
x->left=x->right;
x->right=temp;
}
inorder(x);
}

void main()
{
bst b1;
int choice;
int height;
node *x;
cout<<"..**Binary Search Tree**..";
do
{

6
cout<<"\n1. Create BST: ";
cout<<"\n2. Traverse BST: ";
cout<<"\n3. Largest value from the tree: ";
cout<<"\n4. Minimum data fron the tree: ";
cout<<"\n5. Longest path from the root: ";
cout<<"\n6. Search Function: ";
cout<<"\n7. Swap Function: ";
cout<<"\n8. End of program: ";
cout<<"\nEnter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
x=b1.create();
break;
case 2:
b1.inorder(x);
break;
case 3:
b1.largest_value();
break;
case 4:
b1.smallest_value();
break;
case 5:
height=b1.longest_path(x);
cout<<"\nLongest Path: "<<height;
break;
case 6:
int key;

7
cout<<"\nEnter value to be searched: ";
cin>>key;
b1.search(x,key);
break;
case 7:
b1.swap(x);
b1.inorder(x);
break;
case 8:
cout<<"\n End of the Program. .. !!";
break;
}
}while(choice!=8);
}

Output:
..**Binary Search Tree**..
1. Create BST:
2. Traverse BST:
3. Largest value from the tree:
4. Minimum data fron the tree:
5. Longest path from the root:
6. Search Function:
7. Swap Function:
8. End of program:
Enter your choice: 1

Enter no. of nodes for BST: 5

Enter value for node: 2

8
Root is added..!!
Enter value for node: 3

Enter value for node: 6

Enter value for node: 7

Enter value for node: 8

1. Create BST:
2. Traverse BST:
3. Largest value from the tree:
4. Minimum data fron the tree:
5. Longest path from the root:
6. Search Function:
7. Swap Function:
8. End of program:
Enter your choice: 2
23678
1. Create BST:
2. Traverse BST:
3. Largest value from the tree:
4. Minimum data fron the tree:
5. Longest path from the root:
6. Search Function:
7. Swap Function:
8. End of program:
Enter your choice: 3

9
Largest value is= 8
1. Create BST:
2. Traverse BST:
3. Largest value from the tree:
4. Minimum data fron the tree:
5. Longest path from the root:
6. Search Function:
7. Swap Function:
8. End of program:
Enter your choice: 4

Smallest value is= 2


1. Create BST:
2. Traverse BST:
3. Largest value from the tree:
4. Minimum data fron the tree:
5. Longest path from the root:
6. Search Function:
7. Swap Function:
8. End of program:
Enter your choice: 5

Longest Path: 5
1. Create BST:
2. Traverse BST:
3. Largest value from the tree:
4. Minimum data fron the tree:
5. Longest path from the root:
6. Search Function:
7. Swap Function:

10
Sanika Sachin Ekshette
Roll No: - SCC56
8. End of program:
Enter your choice: 6

Enter value to be searched: 7

Key Found
1. Create BST:
2. Traverse BST:
3. Largest value from the tree:
4. Minimum data fron the tree:
5. Longest path from the root:
6. Search Function:
7. Swap Function:
8. End of program:
Enter your choice: 8

End of the Program. .. !!

11

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy