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...
Develop a Java application to implement a binary tree data structure. A tree data structure starts...
Develop a Java application to implement a binary tree data structure. A tree data structure starts from the top node, called the root. Each node in the tree has a set of children, which are also nodes of the tree and can have children of their own, and so on. This keeps on going till we get to the bottom of the tree, to the “leaf” nodes. Each node in the tree, except for the root, has a parent. A...
use postorder and inorder tree traversals to make preorder traversal. #!/usr/bin/env python3 # encoding: UTF-8 """Turning...
use postorder and inorder tree traversals to make preorder traversal. #!/usr/bin/env python3 # encoding: UTF-8 """Turning in-order and post-order tree traversals into pre-order""" def get_preorder(inorder: str, postorder: str) -> str: """Return pre-order traversal of a tree based on its in-order and post-order traversals""" #Complete the following function #raise NotImplementedError
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,...
JAVA - PLEASE COMMENT CODE - THANK YOU: Implement a program that can input an expression...
JAVA - PLEASE COMMENT CODE - THANK YOU: Implement a program that can input an expression in postfix notation and output its value.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT