//Watch Video @ youtube https://youtu.be/ZFAWcg3yVuo
#include<bits/stdc++.h>
using namespace std;
struct node
{
node *left;
node *right;
int data;
};
node *insertNode(node *root,int info)
{
if(root==NULL)
{
root=new node;
root->left=NULL;
root->right=NULL;
root->data=info;
return(root);
}
else
{
if(root->data<info)
root->right=insertNode(root->right,info);
else
root->left=insertNode(root->left,info);
}
return(root);
}
/*
void inOrder(node *root)
{
if(root!=NULL)
{
inOrder(root->left);
cout<<" "<<root->data;
inOrder(root->right);
}
}
*/
void preOrder(node *root)
{
if(root!=NULL)
{
cout<<" "<<root->data;
preOrder(root->left);
preOrder(root->right);
}
}
/*
void postOrder(node *root)
{
if(root!=NULL)
{
postOrder(root->left);
postOrder(root->right);
cout<<" "<<root->data;
}
}
*/
main()
{
node *root=NULL;
root=insertNode(root,10);
root=insertNode(root,30);
root=insertNode(root,20);
root=insertNode(root,70);
root=insertNode(root,40);
root=insertNode(root,60);
root=insertNode(root,50);
cout<<"INORDER\n";
inOrder(root);
cout<<"\nPOSTORDER\n";
postOrder(root);
cout<<"\nPREORDER\n";
preOrder(root);
}
#include<bits/stdc++.h>
using namespace std;
struct node
{
node *left;
node *right;
int data;
};
node *insertNode(node *root,int info)
{
if(root==NULL)
{
root=new node;
root->left=NULL;
root->right=NULL;
root->data=info;
return(root);
}
else
{
if(root->data<info)
root->right=insertNode(root->right,info);
else
root->left=insertNode(root->left,info);
}
return(root);
}
/*
void inOrder(node *root)
{
if(root!=NULL)
{
inOrder(root->left);
cout<<" "<<root->data;
inOrder(root->right);
}
}
*/
void preOrder(node *root)
{
if(root!=NULL)
{
cout<<" "<<root->data;
preOrder(root->left);
preOrder(root->right);
}
}
/*
void postOrder(node *root)
{
if(root!=NULL)
{
postOrder(root->left);
postOrder(root->right);
cout<<" "<<root->data;
}
}
*/
main()
{
node *root=NULL;
root=insertNode(root,10);
root=insertNode(root,30);
root=insertNode(root,20);
root=insertNode(root,70);
root=insertNode(root,40);
root=insertNode(root,60);
root=insertNode(root,50);
cout<<"INORDER\n";
inOrder(root);
cout<<"\nPOSTORDER\n";
postOrder(root);
cout<<"\nPREORDER\n";
preOrder(root);
}
No comments:
Post a Comment