0% found this document useful (0 votes)
30 views5 pages

Adsl 3

A3

Uploaded by

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

Adsl 3

A3

Uploaded by

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

#include <iostream>

#include<queue>

using namespace std;

class node

public:

int data;

node *left,*right;

node(int x)

data=x;

left=right=nullptr;

};

node *insert(int x, node*root)

if(root==nullptr)

root=new node(x);

else if(x<root->data)

root->left=insert(x,root->left);

else if(x>root->data)

root->right=insert(x,root->right);

return root;

node *createBST()
{

node *root=nullptr;

int n,value;

cout<<"Enter the number of nodes in the BST:"<<endl;

cin>>n;

cout<<"Enter the values of the nodes:"<<endl;

for(int i=0;i<n;i++)

cout<<"Node"<<i+1<<":";

cin>>value;

root=insert(value,root);

return root;

void inorder(node *temp)

if(temp!=nullptr)

inorder(temp->left);

cout<<"\t"<<temp->data;

inorder(temp->right);

void printLevelOrder(node *root)

if(root==nullptr)

return;

queue<node*>q;

q.push(root);

while(!q.empty())
{

int levelSize=q.size();

for(int i=0;i<levelSize;++i)

node *current=q.front();

q.pop();

cout<<current->data<<"";

if(current->left!=nullptr)

q.push(current->left);

if(current->right!=nullptr)

q.push(current->right);

cout<<endl;

void printLeafNodes(node *root)

if(root==nullptr)

return;

if(root->left==nullptr && root->right==nullptr)

cout<<root->data<<"";

return;

if(root->left!=nullptr)

printLeafNodes(root->left);

if(root->right!=nullptr)

printLeafNodes(root->right);

int height(node *root)

{
if(root==nullptr)

return 0;

int leftHeight=height(root->left);

int rightHeight=height(root->right);

return max(leftHeight,rightHeight)+1;

node *mirror(node *root)

if(root==nullptr)

return nullptr;

swap(root->left,root->right);

mirror(root->left);

mirror(root->right);

return root;

int main()

node *root=createBST();

cout<<"Inorder Traversal of Original BST:";

inorder(root);

cout<<endl;

cout<<"\n Level Order Traversal of Original BST:\n";

printLevelOrder(root);

cout<<"\n Height of Original BST:"<<height(root)<<endl;

cout<<"Leaf Nodes of Original BST:";

printLeafNodes(root);

cout<<endl;

node *mirroredRoot=mirror(root);

cout<<"\n Inorder Traversal of Mirrored BST:";

inorder(mirroredRoot);
cout<<endl;

cout<<"Level Order Traversal of Mirrored BST:\n";

printLevelOrder(mirroredRoot);

cout<<"Height of Mirrored BST:"<<height(mirroredRoot)<<endl;

cout<<"Leaf Nodes of Mirrored BST:";

printLeafNodes(mirroredRoot);

cout<<endl;

return 0;

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