Trees
Trees
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;
};
stack<node*> s;
struct node* current = root;
current = s.top();
s.pop();
cout << current->data << " ";
current = current->right;
}
cout << endl;
}
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;
}
}
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