Binary
Binary
};
node* craeteNode(int value)
{
node* newNode= new node();
newNode->left=NULL;
newNode->right= NULL;
newNode->data= value;
return newNode;
}
}
int height(struct node*root)
{
if(root==NULL)
{
return 0;
}
if(root->left==NULL && root->right==NULL)
{
return 1;
}
int lh=height(root->left);
int rh=height(root->right);
if(lh>rh)
{
return lh+1;
}
else
{
return rh+1;
}
}
void Insert()
{
int val;
cout<<"Enter the value to Inset: ";
cin>>val;
struct node*n1=craeteNode(val);
if(root==NULL)
{
cout<<"Tree is empty"<<endl;
root=n1;
cout<<val<<" is inserted as root"<<endl;
}
else
{
int Pval;
int ch;
cout<<"Enter Parent value: ";
cin>>Pval;
cout<<"1.left"<<endl<<"2.Right"<<endl;
cin>>ch;
struct node*pnode=search(Pval,root);
if(ch==1)
{
pnode->left=n1;
}
else
{
pnode->right=n1;
}
class queue {
private:
node* arr[size];
int front, rear;
public:
queue()
{
front=0, rear=0;
}
bool isEmpty() {
return front == rear;
}
};
queue q;
q.enqueue(root);
while (!q.isEmpty()) {
node* current = q.dequeue();
cout << current->data << " ";
if (current->left)
q.enqueue(current->left);
if (current->right)
q.enqueue(current->right);
}
}
int main()
{
root==NULL;
int choice;
do
{
cout<<"Enter Your choice: "<<endl;
cout<<"1.Insert"<<endl<<"2.Height"<<endl<<"3.LOT"<<endl<<"-
1.exit"<<endl;
cin>>choice;
switch(choice)
{
case 1:
Insert();
break;
case 2:
{
cout<<"height is: "<<height(root)<<endl;
break;
}
case 3:
{
levelOrderTraversal(root);
break;
}
case -1:
cout<<"Successfully exit!!!";
break;
}
}while(choice!=-1);
}