Question

In: Computer Science

Tree Reconstruction - Pre+In => Post (C++) Give you pre-order traversal and in-order traversal, please output...

  • Tree Reconstruction - Pre+In => Post (C++)

Give you pre-order traversal and in-order traversal, please output post-order traversal.

Input

Two lines. Pre-order traversal and in-order traversal.

Output

Post-order traversal.

Sample Input

GDAFEMHZ

ADEFGHMZ

Sample Output

AEFDHZMG

Solutions

Expert Solution

Program:

Sample output:

Rawcode:

#include<iostream>       //cpp program for post order travaersal by taking inoder and preorder as inputs
using namespace std;
int search(char a[], char var, int n)        //Delceration search() function is used to search the var value in corresponding array
{
    for (int i = 0; i < n; i++)
        if (a[i] == var) //if finds
            return i;   // then it will return its inderx value
}
void Post(char inorder[], char preorder[], int n)       //decleration of post() funtion which takes inorder array and preorder array as inputs and prints post order
{
    int r ;       //initializing r variable which for assigning root element

    r=search(inorder,preorder[0],n) ;       //searching for 1st element of preorder in inorder array and assigns it root varible r because first element in preorder is alway root element for finding left and right sub arrays
    if(r!=0)           //if root not equals to 0 which implies left sub tree is not empty
        Post(inorder,preorder+1,r) ;       //callign post() to print total left sub tree by calling recursively
    if(r!=n-1)       //checks for right sub tree non empty
        Post(inorder+r+1,preorder+r+1,n-r-1) ;   //if non empty then post() function print total right sub tree by calling recursively
     
    cout<<preorder[0]<<" " ;   //printing first element of postorder

}
int main()        //main method
{
    char in_order[] = {'A','D','E','F','G','H','M','Z'};    //decleration Inorder array
    char pre_order[] = {'G','D','A','F','E','M','H','Z'};    //decleration of preorder array
    int n = sizeof(in_order) / sizeof(in_order[0]);       //finds size of array either pre or inorder and assigns to n
    cout<<"INPUT\nPre order is "<<endl;
    for(int i=0;i<n;i++)       //for loop printing preorder elements
    {
       cout<<pre_order[i]<<" ";
   }
    cout<<"\nIn order is "<<endl;
    for(int i=0;i<n;i++)       //for loop printing inorder elements
    {
       cout<<in_order[i]<<" ";
   }
   
    cout << "\nOUTPUT\nPostorder traversal " << endl;
    Post(in_order, pre_order, n);        //calling post() function
    return 0;
}


------------------------------------------

Hope you understand.Upvote me.

Thank you.


Related Solutions

P1. Construct the tree with post-order traversal string = i b d a h g c...
P1. Construct the tree with post-order traversal string = i b d a h g c f, and in-order traversal string = b i a d f g h c. P2. Construct the tree with pre-order traversal string = 10, 20, 40, 50, 80, 60, 70, 90, and in-order traversal string = 40, 50, 20, 10, 80, 70, 90, 60.
in C programming Write the code to traverse the binary tree using in-order, post-order and pre-order...
in C programming Write the code to traverse the binary tree using in-order, post-order and pre-order methods. Make sure to validate that your output is correct. Review the terminology for tree structures and list structures. What is the root of the tree, what’s a parent, what’s a child, what’s an ancestor, what is an external node, what is an internal node, what is the head of the list, what is the tail of the list, etc. Traverse the binary tree...
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]
Traversal tree program in c/c++
Traversal tree program in c/c++
C program that demonstrate tree traversal. see for the sample output. Sample Output: How many node...
C program that demonstrate tree traversal. see for the sample output. Sample Output: How many node do you have in your tree : 5 Enter root: 20 if you want to add to the left press 0 otherwise press 1 answer: 0 Enter next node: 10 if you want to add to the left press 0 otherwise press 1 answer: 0 Enter next node: 8 if you want to add to the left press 0 otherwise press 1 answer: 0...
Design a nonrecursive post order traversal algorithm. 1.Use the linked representation for the binary tree to...
Design a nonrecursive post order traversal algorithm. 1.Use the linked representation for the binary tree to be traversed. 2.Except left, right, and data fields, there are no other fields in a node. 3.After the traversal the tree should remain intact. 4.Use only one stack. 5.You can’t push anything other than nodes into the stack.
Please ready a program should output Inorder Tree Traversal without recursion and without stack!, output in...
Please ready a program should output Inorder Tree Traversal without recursion and without stack!, output in ALGOL W programming only. Thumbs down for wrong answers Make a program to perform Heap Sort, must run in Alice programming only. Only correct answers needed should be in given language
Modifying the tree traversal C program shown below to do reversed tree traversal as defined below....
Modifying the tree traversal C program shown below to do reversed tree traversal as defined below. Reversed PreOrder: Root, Right, Left. Reversed InOrder:     Right, Root, Left. Reversed PostOrder: Right, Left, Root. The tree is: Root = 1, Left(1) = -2, Right(1) = -3; Left(-2) = 4, Right(-2) = 5; Left(-3) = 6, Right(-3)= 7; Left(5) = -8, Right(5)= -9; Left(7) = 10, Right(7) = 11; Left(11) = -12, Right(11) = -13; Left(-13) = 14. Your program should output the printed...
Use C programming to Implement the breadth-first tree traversal. The tasks are: a) Build the tree....
Use C programming to Implement the breadth-first tree traversal. The tasks are: a) Build the tree. b) Perform breadth first traversal on the tree that you have built at step a). Note: "Please comment the program"
Write a C program that demonstrate tree traversal. A. first is to ask the user to...
Write a C program that demonstrate tree traversal. A. first is to ask the user to enter data B. ask how many nodes THIS IS JUST A SAMPLE. PLEASE ANSWER IT IN COMPLETE CODE. See for the sample output. Sample Output: How many node do you have in your tree : 5 Enter root: 20 if you want to add to the left press 0 otherwise press 1 answer: 0 Enter next node: 10 if you want to add to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT