0% found this document useful (0 votes)
40 views4 pages

#Include Using Namespace Struct Int Struct Struct

This C++ program defines a binary tree structure with nodes containing an integer roll number. It includes functions to insert nodes into the tree, check if the tree or nodes are empty, and compare the roll numbers of nodes. The main function allows the user to insert nodes by roll number or display the tree using an LVR (left-right-root) traversal, and exits when the user chooses.

Uploaded by

Hamza Ali
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)
40 views4 pages

#Include Using Namespace Struct Int Struct Struct

This C++ program defines a binary tree structure with nodes containing an integer roll number. It includes functions to insert nodes into the tree, check if the tree or nodes are empty, and compare the roll numbers of nodes. The main function allows the user to insert nodes by roll number or display the tree using an LVR (left-right-root) traversal, and exits when the user chooses.

Uploaded by

Hamza Ali
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/ 4

#include<iostream>

using namespace std;

struct tree
{
int roll;
struct tree * left;
struct tree * right;
};

struct tree * root=NULL;

void read(struct tree* temp)


{
cin>>temp->roll;
}

int is_empty1(struct tree * temp)


{
if(temp==NULL)
{
return 1;
}
else
{
return 0;
}
}

int is_smaller(struct tree* temp1,struct tree* temp2)


{
if(temp1->roll<temp2->roll)
{
return 1;
}
else
{
return 0;
}
}

int is_greater(struct tree* temp1,struct tree* temp2)


{
if(temp1->roll>temp2->roll)
{
return 1;
}
else
{
return 0;
}
}

int is_left_empty(struct tree * temp)


{
if(temp->left==NULL)
{
return 1;
}
else
{
return 0;
}
}

int is_right_empty(struct tree * temp)


{
if(temp->right==NULL)
{
return 1;
}
else
{
return 0;
}
}

void insert(struct tree *cur)


{
if(is_empty1(root))
{
root=cur;
cur->left=NULL;
cur->right=NULL;
}
else
{
struct tree * temp;
temp=root;
while(temp!=NULL)
{
if(is_smaller(cur,temp))
{
if(is_left_empty(temp))
{
temp->left=cur;
cur->left=NULL;
cur->right=NULL;
break;
}
else
{
temp=temp->left;
}
}
else if(is_greater(cur,temp))
{
if(is_right_empty(temp))
{
temp->right=cur;
cur->left=NULL;
cur->right=NULL;
break;

}
else
{
temp=temp->right;
}

}
}
}
}
void display(struct tree* temp)
{
cout<<temp->roll<<endl;
}

void LVR(struct tree* temp)


{

if(temp==NULL)
{
;
}
else

{
LVR(temp->left);
display(temp);
LVR(temp->right);
}

}
void main()
{
int choice;
while(1)
{
cout<<"1 - Insert "<<endl;
cout<<"2 - Display "<<endl;
cout<<"3 - Exit "<<endl;
cout<<"\t \t ENTER YOUR CHOICE :";
cin>>choice;

if(choice==1)
{
struct tree *cur;
cur = new tree();
cout<<"\t \t Enter Roll Number :";
read(cur);
insert(cur);
}
else if(choice==2)
{
LVR(root);
}
else if(choice==3)
{
break;
}

}
}

You might also like