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

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)
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...
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,...
You will be inserting values into a generic tree, then printing the values inorder, as well...
You will be inserting values into a generic tree, then printing the values inorder, as well as printing the minimum and maximum values in the tree. Given main(), write the methods in the 'BSTree' class specified by the // TODO: sections. There are 5 TODOs in all to complete. Ex: If the input is like ferment bought tasty can making apples super improving juice wine -1 the output should be: Enter the words on separate lines to insert into the...
Create a Binary Search Tree for the following data and do In-order, Preorder and Post-order traversal...
Create a Binary Search Tree for the following data and do In-order, Preorder and Post-order traversal of the tree. 50, 60, 25, 40, 30, 70, 35, 10, 55, 65, 5 Write an algorithm to delete a node in Singly Linked List                            [12 Write an algorithm of Binary Search                                                              [10] Write a program in ‘C’ to generate Fibonacci series using recursion            [8]
// (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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT