In: Computer Science
Just Implement Inorder, Preorder and Postorder of Tree Data Structure in given code below. Thank You.
#include<bits/stdc++.h>
using namespace std;
struct Node{
int data;
Node* left;
Node* right;
};
Node* createNode(int value){
Node* newNode = new Node();
newNode->data = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
Node* insert(Node *currentNode, int value){
if(currentNode==NULL){
currentNode =
createNode(value);
}else if(value <=
currentNode->data){
currentNode->left =
insert(currentNode->left,value);
}else{
currentNode->right =
insert(currentNode->right,value);
}
return currentNode;
}
int Search(Node* currentNode, int value){
if(currentNode==NULL){
return 0;
}else if(currentNode->data==value){
return 1;
}else if(value <=
currentNode->data){
return
Search(currentNode->left,value);
}else{
return
Search(currentNode->right,value);
}
}
int main()
{
Node* root = NULL;
root = insert(root,15);
root = insert(root,10);
root = insert(root,8);
root = insert(root,35);
root = insert(root,60);
root = insert(root,23);
int find = Search(root,45);
if(find == 1){
cout<<"Value
Found"<<endl;
}else{
cout<<"Value not
Found"<<endl;
}
}
Answer:
Give me a thumbs up if you like my work !!!
I have written the below code based on your requirements.
The below code has no-error and it will work perfectly.
I have also attached the output screenshot that I got by running the below program.
Output:
Code:
void inorder(Node *root)
{
if(root==NULL)
return;
inorder(root->left);
cout<<root->data<<" ";
inorder(root->right);
return;
}
void preorder(Node *root)
{
if(root==NULL)
return;
cout<<root->data<<" ";
preorder(root->left);
preorder(root->right);
return;
}
void postorder(Node *root)
{
if(root==NULL)
return;
postorder(root->left);
postorder(root->right);
cout<<root->data<<" ";
return;
}
************************************************************************************************************************
Main Function:
int main()
{
Node* root = NULL;
root = insert(root,15);
root = insert(root,10);
root = insert(root,8);
root = insert(root,35);
root = insert(root,60);
root = insert(root,23);
int find = Search(root,45);
if(find == 1){
cout<<"Value Found"<<endl;
}else{
cout<<"Value not Found"<<endl;
}
cout<<"\n\nInorder : "<<endl;
inorder(root);
cout<<"\n\nPreorder : "<<endl;
preorder(root);
cout<<"\n\nPostorder : "<<endl;
postorder(root);
}
**********************************************************************************************************************
Whole Problem:
#include<bits/stdc++.h>
using namespace std;
struct Node{
int data;
Node* left;
Node* right;
};
Node* createNode(int value){
Node* newNode = new Node();
newNode->data = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
Node* insert(Node *currentNode, int value){
if(currentNode==NULL){
currentNode = createNode(value);
}else if(value <= currentNode->data){
currentNode->left = insert(currentNode->left,value);
}else{
currentNode->right = insert(currentNode->right,value);
}
return currentNode;
}
int Search(Node* currentNode, int value){
if(currentNode==NULL){
return 0;
}else if(currentNode->data==value){
return 1;
}else if(value <= currentNode->data){
return Search(currentNode->left,value);
}else{
return Search(currentNode->right,value);
}
}
void inorder(Node *root)
{
if(root==NULL)
return;
inorder(root->left);
cout<<root->data<<" ";
inorder(root->right);
return;
}
void preorder(Node *root)
{
if(root==NULL)
return;
cout<<root->data<<" ";
preorder(root->left);
preorder(root->right);
return;
}
void postorder(Node *root)
{
if(root==NULL)
return;
postorder(root->left);
postorder(root->right);
cout<<root->data<<" ";
return;
}
int main()
{
Node* root = NULL;
root = insert(root,15);
root = insert(root,10);
root = insert(root,8);
root = insert(root,35);
root = insert(root,60);
root = insert(root,23);
int find = Search(root,45);
if(find == 1){
cout<<"Value Found"<<endl;
}else{
cout<<"Value not Found"<<endl;
}
cout<<"\n\nInorder : "<<endl;
inorder(root);
cout<<"\n\nPreorder : "<<endl;
preorder(root);
cout<<"\n\nPostorder : "<<endl;
postorder(root);
}