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

Trees

The document contains C++ code for binary tree operations including insertion, searching, and various tree traversals (preorder, inorder, postorder) using both recursion and stack. It also implements deletion of nodes from the binary tree with special handling for nodes with two children. The main function demonstrates these functionalities by creating a binary tree, performing traversals, and deleting specified nodes based on user input.

Uploaded by

khan10naveed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Trees

The document contains C++ code for binary tree operations including insertion, searching, and various tree traversals (preorder, inorder, postorder) using both recursion and stack. It also implements deletion of nodes from the binary tree with special handling for nodes with two children. The main function demonstrates these functionalities by creating a binary tree, performing traversals, and deleting specified nodes based on user input.

Uploaded by

khan10naveed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

#include<bits/stdc++.

h>
using namespace std;
struct node
{
int data;
struct node *left;
struct node *right;
};
void preorder(struct node *root)
{
if(root==NULL)
return;
cout<<root->data<<" ";
preorder(root->left);
preorder(root->right);
}
int main()
{
struct node *p1=(struct node*)malloc(sizeof(struct node));
struct node *p2=(struct node*)malloc(sizeof(struct node));
struct node *p3=(struct node*)malloc(sizeof(struct node));
struct node *p4=(struct node*)malloc(sizeof(struct node));
struct node *p5=(struct node*)malloc(sizeof(struct node));
p1->data=5;
p2->data=10;
p3->data=11;
p4->data=3;
p5->data=15;
p1->left=p2;
p1->right=p3;
p2->left=p4;
p2->right=p5;
p3->left=NULL;
p3->right=NULL;
p4->left=NULL,p4->right=NULL;
p5->left=NULL,p5->right=NULL;
preorder(p1);
}
Output:
5 10 3 15 11
#include<bits/stdc++.h>
using namespace std;
struct node
{
int data;
struct node *left;
struct node *right;
};

void preorder(struct node *root)


{
if(root==NULL)
return;
cout<<root->data<<" ";
preorder(root->left);
preorder(root->right);
}

void inorder(struct node *root)


{
if(root==NULL)
return;
inorder(root->left);
cout<<root->data<<" ";
inorder(root->right);
}
void postorder(struct node *root)
{
if(root==NULL)
return;
postorder(root->left);
postorder(root->right);
cout<<root->data<<" ";
}

void preorderSTACK(struct node * root)


{
if(root==NULL)
{
cout<<"Tree is empty"<<endl;
return;
}
stack<node*> s;
s.push(root);
while(!s.empty())
{
node* p=s.top();
cout<<p->data<<" ";
s.pop();
if(p->right!=NULL) s.push(p->right);
if(p->left!=NULL) s.push(p->left);
}
cout<<endl;
}

void inorderSTACK(struct node* root) {


if (root == NULL) return;

stack<node*> s;
struct node* current = root;

while (current != NULL || !s.empty()) {


while (current != nullptr) {
s.push(current);
current = current->left;
}

current = s.top();
s.pop();
cout << current->data << " ";

current = current->right;
}
cout << endl;
}

void postorderSTACK(node* root) {


if (root == NULL) return;

stack<node*> s1, s2;


s1.push(root);

while (!s1.empty()) {
node* node= s1.top();
s1.pop();
s2.push(node);

if (node->left) s1.push(node->left);
if (node->right) s1.push(node->right);
}

while (!s2.empty()) {
cout << s2.top()->data << " ";
s2.pop();
}
cout << endl;
}
int main()
{
struct node *p1=(struct node*)malloc(sizeof(struct node));
struct node *p2=(struct node*)malloc(sizeof(struct node));
struct node *p3=(struct node*)malloc(sizeof(struct node));
struct node *p4=(struct node*)malloc(sizeof(struct node));
struct node *p5=(struct node*)malloc(sizeof(struct node));
p1->data=5;
p2->data=10;
p3->data=11;
p4->data=3;
p5->data=15;
p1->left=p2;
p1->right=p3;
p2->left=p4;
p2->right=p5;
p3->left=NULL;
p3->right=NULL;
p4->left=NULL,p4->right=NULL;
p5->left=NULL,p5->right=NULL;
cout<<"Preorder traversal using recursion"<<endl;
preorder(p1);
cout<<endl;
cout<<"Inorder traversal using recursion"<<endl;
inorder(p1);
cout<<endl;
cout<<"Postorder traversal using recursion"<<endl;
postorder(p1);
cout<<endl;
cout<<"Postorder traversal using stack"<<endl;
preorderSTACK(p1);
cout<<"Inorder traversal using stack"<<endl;
inorderSTACK(p1);
cout<<"Postorder traversal using stack"<<endl;
postorderSTACK(p1);

}
Output:
Preorder traversal using recursion
5 10 3 15 11
Inorder traversal using recursion
3 10 15 5 11
Postorder traversal using recursion
3 15 10 11 5
Postorder traversal using stack
5 10 3 15 11
Inorder traversal using stack
3 10 15 5 11
Postorder traversal using stack
3 15 10 11 5
#include<bits/stdc++.h>
using namespace std;
struct node
{
int data;
struct node *left;
struct node *right;
};
bool ok=true;
struct node* find(struct node *root,int item)
{
ok=true;
if(root==NULL)
{
return NULL;
}

struct node *save=new node;


struct node *p=new node;
save=NULL;
p=root;
while(p!=NULL)
{
if(p->data==item)
{
ok=false;
return NULL;
}
else if(item>p->data)
{
save=p;
p=p->right;
}
else
{
save=p;
p=p->left;
}
}
return save;
}

struct node *insert(struct node* root,int item)


{
struct node *par=find(root,item);
if(par==NULL && ok)
{
struct node *p=new node;
p->data=item;
p->left=NULL;
p->right=NULL;
return p;
}
else
{
if(item>par->data)
{
struct node *p=new node;
p->data=item;
p->left=NULL;
p->right=NULL;
par->right=p;
return root;
}
else
{
struct node *p=new node;
p->data=item;
p->left=NULL;
p->right=NULL;
par->left=p;
return root;
}
}
}

void search(struct node *root,int item)


{
struct node *p=new node;
p=root;
while(p!=NULL)
{
if(p->data==item)
{
cout<<"Item found"<<endl;
return;
}
else if(item>p->data)
p=p->right;
else
p=p->left;
}
cout<<"Item not found"<<endl;
return;
}
void preorder(struct node *root)
{
if(root==NULL)
return;
cout<<root->data<<" ";
preorder(root->left);
preorder(root->right);
}
int main()
{
int n;
cin>>n;
node *p=new node;
p=NULL;
for(int i=0;i<n;i++)
{
int d;
cin>>d;
p=insert(p,d);
}
preorder(p);
cout<<endl;
int d;
cin>>d;
search(p,d);
}
Output:
5
12345
12345
#include<bits/stdc++.h>
using namespace std;
struct node
{
int data;
struct node *left;
struct node *right;
};
bool ok=true;
void preorder(struct node *root)
{
if(root==NULL)
return;
cout<<root->data<<" ";
preorder(root->left);
preorder(root->right);
}
struct node* find(struct node *root,int item)
{
ok=true;
if(root==NULL)
{
return NULL;
}

struct node *save=new node;


struct node *p=new node;
save=NULL;
p=root;
while(p!=NULL)
{
if(p->data==item)
{
ok=false;
return save;
}
else if(item>p->data)
{
save=p;
p=p->right;
}
else
{
save=p;
p=p->left;
}
}
return save;
}
struct node *insert(struct node* root,int item)
{
struct node *par=find(root,item);
if(par==NULL && ok)
{
struct node *p=new node;
p->data=item;
p->left=NULL;
p->right=NULL;
return p;
}
else
{
if(item>par->data)
{
struct node *p=new node;
p->data=item;
p->left=NULL;
p->right=NULL;
par->right=p;
return root;
}
else
{
struct node *p=new node;
p->data=item;
p->left=NULL;
p->right=NULL;
par->left=p;
return root;
}
}
}
struct node* search(struct node *root,int item)
{
struct node *p=new node;
p=root;
while(p!=NULL)
{
if(p->data==item)
{
return p;
}
else if(item>p->data)
p=p->right;
else
p=p->left;
}
return NULL;
}
struct node *del1(struct node *root,struct node *loc)
{
struct node* par=find(root,loc->data);
if(par==NULL)
{
if(loc->left==NULL && loc->right==NULL)
return NULL;
else if(loc->left!=NULL)
{
return loc->left;
}
else
{
return loc->right;
}
}
else
{
if(loc->left==NULL && loc->right==NULL)
{
if(par->left==loc)
{
par->left=NULL;
delete(loc);
return root;
}
else
{
par->right=NULL;
delete(loc);
return root;
}
}
else if(loc->left!=NULL)
{
if(par->left==loc)
{
par->left=loc->left;
delete(loc);
return root;
}
else
{
par->right=loc->left;
delete(loc);
return root;
}
}
else
{
if(par->left==loc)
{
par->left=loc->right;
delete(loc);
return root;
}
else
{
par->right=loc->right;
delete(loc);
return root;
}
}
}
}
struct node *del2(struct node *root,struct node *loc)
{
struct node* par=find(root,loc->data);
struct node *suc=loc->right;
struct node *parsuc=loc;
while(suc->left!=NULL)
{
parsuc=suc;
suc=suc->left;
}
int t=suc->data;
root=del1(root,suc);
if(par==NULL)
{
node *temp=new node;
temp->left=loc->left;
temp->right=loc->right;
temp->data=t;
delete(loc);
return temp;
}
else
{
if(par->left==loc)
{
node *temp=new node;
temp->data=t;
par->left=temp;
temp->left=loc->left;
temp->right=loc->right;
delete(loc);
return root;
}
else
{
node *temp=new node;
temp->data=t;
par->right=temp;
temp->left=loc->left;
temp->right=loc->right;
delete(loc);
return root;
}
}

}
int main()
{
int n;
cin>>n;
node *p=new node;
p=NULL;
for(int i=0;i<n;i++)
{
int d;
cin>>d;
p=insert(p,d);
}
preorder(p);
int d;
cin>>d;
struct node *loc=search(p,d);
if(loc==NULL)
{
cout<<"item not in tree"<<endl;
}
else if(loc->left!=NULL && loc->right!=NULL)
{
p=del2(p,loc);
}
else
{
p=del1(p,loc);
} preorder(p); } Output:
31524
3 1 2 5 4 Insert the item you want to delete
5
3124

You might also like