Skip to main content

Program In C++ to find out the preorder,postorder,inorder traversal of tree


Program :-
WAP in C++ to find out the preorder,postorder as well as inorder traversal of the binary tree.

Solution :-

// Name :- Bakul Gupta
// Email-id :- bakulgupta11@gmail.com

#include<bits/stdc++.h>
using namespace std;
struct Node
{
int key;
Node *left,*right;
Node(int x)
{
left=NULL;
right=NULL;
key=x;
}
};

void inorder(Node *root)
{
if(root)
{
inorder(root->left);
cout<<root->key<<' ';
inorder(root->right);
}
}

void preorder(Node *root)
{
if(root)
{
cout<<root->key<<' ';
preorder(root->left);
preorder(root->right);
}
}

void postorder(Node *root)
{
if(root)
{
postorder(root->left);
postorder(root->right);
cout<<root->key<<" ";
}
}


int main()
{
Node *root=new Node(10);
root->left=new Node(20);
root->right=new Node(30);
root->left->left=new Node(40);
root->left->right=new Node(50);
cout<<"Inorder traversal is :-";
inorder(root);
cout<<"\n Post order Traversal is :-";
postorder(root);
cout<<"\n Pre Order traversal is :-";
preorder(root);
return 0;

}

Comments

Popular posts from this blog

INTRODUCTION TO BITCOIN

Q. What is Bit-coin ? 1. Bit-coin is formally a virtual form of currency,by virtual form:- we mean that bit-coin is only available in electronic (i.e digitalized) form.Hence there is no physical presence of bitcoin just like as INR(Indian Rupees), USD(Dollars), Yen,Euro etc. 2.It is a digital payment system which is supposed to be implemented by an unknown programmer or group of programmers under the name of Satoshi Nakamoto. 3.Initially the first presence of Bit-coin was felt in the year 2009.   Advantages of Using Bit-coin:- 1. Bit-coin uses peer-to-peer (P2P) technology i.e the bitcoin r transferred     from one person to another person e-wallet without the involvement of any     intermediary. 2. It supports the world-wide connectivity it means that the bitcoin can be  transferred to any part of the world in real world.   3. Low-processing fees is another feature regarding the bitcoin transactions...

AWS | CloudFront

CloudFront Service used for Content Delivery Network Collection of distributed servers , that are used to give access to the contents to users on the basis of geographic locations. Parameters : Edge Location: Location where the data will be cached Origin: Origin of the files that the CDN will distribute. Origin may include : s3 buckets , elastic laod balancer or route53 Terminilogy : 1. Web Distribution: Used for websites 2. RTMP: Used for media streaming Creating a CloudFront Distribution   STEP 1:- Select the CloudFront service from the AWS services menu STEP 2:- Click on the select distribution option, and choose the type of distribution according to the use case   2.1 Web Distribution   2.2  RTMP Distribution

AWS | IAM

IAM Identity & Access Management AWS service used for the purpose of access controls to the various services and policies. Main components of IAM : 1. Users 2. Groups 3. Roles 4. Policies 1. Users It is an individual entity , that have specific set of policies on the basis of the various roles specified. 2. Groups Combinations of the users, that shares same set of policies. 3. Roles 4. Policies