Question

In: Computer Science

Just Implement Inorder, Preorder and Postorder of Tree Data Structure in given code below. Thank You....

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;
    }
  
}

Solutions

Expert Solution

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);
}


Related Solutions

a java code In this lab you will implement an inorder traversal, and a search for...
a java code In this lab you will implement an inorder traversal, and a search for a 2-3-4 tree. The inorder traversal will print the contents (integers) of the tree, and the search method will return a boolean, indicating whether a given key was found in the tree. Examine the provided template. The main method is provided for you, as well as a template of the Node and 2-3-4 classes. The main method will read either "inorder" or an integer...
C++ code (just fuction) Binary Search Tree (BTS) 1) Algorithm for all traversals (inorder, preorder, postorder,...
C++ code (just fuction) Binary Search Tree (BTS) 1) Algorithm for all traversals (inorder, preorder, postorder, level order), done nonrecursively 2)Ability to provide the result of traversing a tree in all traversals (inorder, preorder, postorder, level order)
Implement a Composite Design Pattern for the code below that creates a family tree MAIN: public...
Implement a Composite Design Pattern for the code below that creates a family tree MAIN: public class Main { public static void main(String[] args) { /* Let's create a family tree (for instance like one used on genealogy sites). For the sake of simplicity, assume an individual can have at most two children. If an individual has 1-2 children, they are considered a "tree". If an individual does not have children, they are considered a "person". With that in mind,...
// (A) Implement the code below. For a given graph, return a Set of type T...
// (A) Implement the code below. For a given graph, return a Set of type T of all of the strongly connected nodes of the given key. public Set<T> stronglyConnectedComponent(T key) { return null; } // (B) Implement the code below. Optimally find the shortest path between the given start and end node. public List<T> shortestPath(T startLabel, T endLabel) {        return null; }
Code using C++ A binary search tree is a data structure designed for efficient item insertion,...
Code using C++ A binary search tree is a data structure designed for efficient item insertion, deletion, and retrieval. These 3 operations share an average run time complexity of O(log(n)). Such time complexities are guaranteed whenever you are working with a balanced binary search tree. However, if you have a tree that begins leaning heavily to either its left or right side, then you can expect the performances of the insertion, deletion, and retrieval operations to degrade. As an example,...
How do you implement stack by using linked list? No code just explain it.
How do you implement stack by using linked list? No code just explain it.
[50%] Code Snippet Given snippet code below that you are required to complete. You are not...
[50%] Code Snippet Given snippet code below that you are required to complete. You are not allowed to make a new function or change any given code. Please complete each section that are marked with the notation “INSERT YOUR CODE HERE”. Once you complete the snippet below, your output should have the same result with the given output below. Descriptions: [15%] isValid() This function is for checking that there is no duplicated employee data in the linked list. This function...
I just want the code blanking led for PIC microprocessor in MBLAB X Thank you
I just want the code blanking led for PIC microprocessor in MBLAB X Thank you
----- Please solve the questions with the code below. Thank you. ----- Exercise Overview Refactor your...
----- Please solve the questions with the code below. Thank you. ----- Exercise Overview Refactor your code to enhance the user experience and to use objects and classes. All functional requirements in Project 1 remain, except where enhancing the system replaces specific functions. Functional Requirements The console entry point for the user inputs is on the same line as the prompt. (new) User enters name at the beginning of a session. System covers four math operations – addition, subtraction, multiplication,...
Please do this code with python. Thank you! struct Node {     int data;     struct...
Please do this code with python. Thank you! struct Node {     int data;     struct Node* left;     struct Node* right; }; // Node creation struct Node* newNode(int data) {     struct Node* nn         = new Node;     nn->data = data;     nn->left = NULL;     nn->right = NULL;     return nn; } // Function to insert data in BST struct Node* insert(struct Node* root, int data) {   if (root == NULL)         return newNode(data);     else {...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT