Question

In: Computer Science

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


Solutions

Expert Solution

For the above program, we need to have the following things into consideration.

  1. We need to find the postorder of the above-given tree from the inorder and preorder.
  2. Then we need to print the post order

Following is the python code:

# Printing the post order
post_order = []
def postorder(inorder, preorder, n):
    if preorder[0] in inorder:
        root = inorder.index(preorder[0])

    if root != 0:  # left subtree exists
        postorder(inorder[:root],
                       preorder[1:root + 1],
                       len(inorder[:root]))

    if root != n - 1:  # right subtree exists
        postorder(inorder[root + 1:],
                       preorder[root + 1:],
                       len(inorder[root + 1:]))

    post_order.append(preorder[0])


def get_preorder(inorder, preorder):
    postorder(inorder, preorder, len(inorder))


# Driver Code
inorder = [4, 2, 5, 1, 3, 6];
preorder = [1, 2, 4, 5, 3, 6];
n = len(inorder)
print("Postorder traversal ")
get_preorder(inorder, preorder)
print(post_order)

Following is the snippet of the above program .:


The Output of the above program.

That was a nice question to answer
Friend, If you have any doubts in understanding do let me know in the comment section. I will be happy to help you further.
Please like if you think effort deserves like.
Thanks


Related Solutions

Here is the script for finding the start dates of employees #!/usr/bin/env python3 import csv import...
Here is the script for finding the start dates of employees #!/usr/bin/env python3 import csv import datetime import requests FILE_URL = "https://storage.googleapis.com/gwg-hol-assets/gic215/employees-with$ def get_start_date(): """Interactively get the start date to query for.""" print() print('Getting the first start date to query for.') print() print('The date must be greater than Jan 1st, 2018') year = int(input('Enter a value for the year: ')) month = int(input('Enter a value for the month: ')) day = int(input('Enter a value for the day: ')) print() return...
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]
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{        ...
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)
1. The following XML document describes some employees of a company. <?xml version="1.0" encoding="UTF-8"?> <company> <person...
1. The following XML document describes some employees of a company. <?xml version="1.0" encoding="UTF-8"?> <company> <person age="25" id="p29432" manager="p48293"> <ssn>123456789</ssn> <name> John Davis</name> <office>B432</office> <phone>1234</phone> </person> <person age="27" id="p29333" manager="p48222"> <ssn>987654321</ssn> <name>Jim Handler</name> <office>B123</office> </person> <person age=55 id="p29432" manager="p48221"> <ssn>123456789</ssn> <name> Christo Dichev</name> <office>A4210</office> <phone>2477</phone> </person> <person age="57" id="p29333" manager="p4832"> <ssn>987654321</ssn> <name>Elva Jones</name> <office>A4211</office> </person> </company> 1. Check if the XML document is well-formed. If it is not, change it so that it becomes wellformed, making as little changes as...
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.
3.1. Create an XML schema to validate the following XML file. <?xml version="1.0" encoding="utf-8"?> <root> <whats>Everything</whats>...
3.1. Create an XML schema to validate the following XML file. <?xml version="1.0" encoding="utf-8"?> <root> <whats>Everything</whats> <up>Is</up> <doc>Fine</doc> </root> Schema starter: <xsd:schema xmlns:xsd=""> <xsd:element name="root" type="rootType" /> <xsd:complexType name="rootType">     <xsd:sequence>       <xsd:element name="" type="" minOccurs="1" />       <xsd:element name="” type="" minOccurs="1" />       <xsd:element name="" type="" minOccurs="1" />     </xsd:sequence> </xsd:complexType> </xsd:schema> 3.2. Use your schema to validate the XML file.                     3.2.1. You can use Visual Studio or online utilities to apply the schema to the XML file....
Write a simple Java code to make a Binary Tree for the following tree: Don’t use...
Write a simple Java code to make a Binary Tree for the following tree: Don’t use serializable interface implantation /** Class to encapsulate a tree node. */ protected static class Node implements Serializable {
Make a schema for a memory size of 65536 words per 8 bits. Use 8k  by 8-bit...
Make a schema for a memory size of 65536 words per 8 bits. Use 8k  by 8-bit 2764 memory a) with a single decoder 388 and inverters. b) Use a decoder 2 4 and inverters? Help?   How to implement?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT