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

Binary

The document contains code for implementing basic binary tree operations like insertion, finding height, and level order traversal. It includes functions to create nodes, search for a node, find the height of a tree, insert new nodes, and perform level order traversal.

Uploaded by

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

Binary

The document contains code for implementing basic binary tree operations like insertion, finding height, and level order traversal. It includes functions to create nodes, search for a node, find the height of a tree, insert new nodes, and perform level order traversal.

Uploaded by

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

#include<iostream>

using namespace std;


#define size 100

struct node* root;


struct node
{
int data;
struct node* left;
struct node* right;

};
node* craeteNode(int value)
{
node* newNode= new node();
newNode->left=NULL;
newNode->right= NULL;
newNode->data= value;
return newNode;
}

struct node*search(int val,struct node*tree)


{
if (tree==NULL)
{
return NULL;
}
if(tree->data==val)
{
return tree;
}

struct node* rnode= search(val, tree->left);


{
if (rnode==NULL)
{
return search(val, tree->right);
}
else
{
return rnode;
}
}

}
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;
}

void enqueue(node* node) {


if (rear == size)return;
arr[rear++] = node;
}
node* dequeue() {
if (front == rear)
return NULL;
return arr[front++];
}

bool isEmpty() {
return front == rear;
}
};

void levelOrderTraversal(node* root) {


if (root == NULL) return;

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);
}

You might also like