Question

PYTHON 3 CODING

Review the following tree and then implement Depth First Traversals. Your program should display the output from Inorder to Preorder and Postorder.

V. 0111

Include the following items in your answer:

  • Write the implementation for Inorder, Preorder, and Postorder
  • Print the output for each of the Inorder, Preorder, and Postorder

Use the following steps to help with your solution:

  • Create the node structure. Each node structure should contain the data, left node and right node.
  • Create a function to add a node.
  • Write the implementation for PrintInOrder.
  • Write the implementation for PrintPreOrder.
  • Write the implementation for PrintPostOrder.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

# class for Node
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None

# adding Node to tree


def addNode(root, node):
if root is None:
root = node
else:
if root.data < node.data:
if root.right is None:
root.right = node
else:
addNode(root.right, node)
else:
if root.left is None:
root.left = node
else:
addNode(root.left, node)


# inOrder tree traversal
def printInOrder(root):

if root:

# First recur on left child
printInOrder(root.left)

# then print the data of node
print(root.data, end=" ")

# now recur on right child
printInOrder(root.right)


# postOrder tree traversal
def printPostOrder(root):

if root:

# First recur on left child
printPostOrder(root.left)

# the recur on right child
printPostOrder(root.right)

# now print the data of node
print(root.data, end=" ")


# preOrder tree traversal
def printPreOrder(root):

if root:

# First print the data of node
print(root.data, end=" ")

# Then recur on left child
printPreOrder(root.left)

# Finally recur on right child
printPreOrder(root.right)


if __name__ == "__main__":
n = int(input("Enter the number of nodes you want to Enter : "))

data = input("Enter the data for Node 1 : ")

root = Node(data)

for i in range(2, n+1):
data = input("Enter the data for Node {} : ".format(i))
node = Node(data)
addNode(root, node)

print("\nPreOrder traversal of binary tree is")
printPreOrder(root)

print("\n\nInOrder traversal of binary tree is")
printInOrder(root)

print("\n\nPostOrder traversal of binary tree is")
printPostOrder(root)

# class for Node class Node: def init_(self, data): self.data = data self.left = None self.right = None # adding Node to tree56 # pre0rder tree traversal def printPre0 rder ( root ) : 57 58 59 if root: 60 61 # First print the data of node print(root.

#please upvote if you like the answer :)

Add a comment
Know the answer?
Add Answer to:
PYTHON 3 CODING Review the following tree and then implement Depth First Traversals. Your program should...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • in python 11.1 Binary Search Tree In this assignment, you will implement a Binary Search Tree...

    in python 11.1 Binary Search Tree In this assignment, you will implement a Binary Search Tree You will also need to implement a Node class. This class will not be tested, but is needed to implement the BST. Your BST must implement the following methods. You are free to implement additional helper methods. It is recommended you create your own helper methods Constructor: Creates an Empty Tree String Method: Returns the string "Empty Tree" for an empty tree. Otherwise, returns...

  • using java to write,show me the output. please write some common. You CAN NOT use inbuild...

    using java to write,show me the output. please write some common. You CAN NOT use inbuild functions for Tree ADT operations. using code below to finsih public class Main {    public static void main(String[] args) {        BinaryTree tree = new BinaryTree(); tree.root = new Node(1); tree.root.left = new Node(2); tree.root.right = new Node(3); tree.root.left.left = new Node(4); tree.root.left.right = new Node(5); tree.root.right.left = new Node(6); tree.root.right.right = new Node(7); tree.root.left.left.left = new Node(8); tree.root.left.left .right= new Node(9);...

  • 1. Write a program in C to show how to traverse a tree or visit each...

    1. Write a program in C to show how to traverse a tree or visit each node in the tree exactly once? Use the following figure to implement the three methods, LVR (inorder), LRV (postorder), VLR (preorder). 17 1819 14 12 8 Figure 5.15: Binary tree with arithmetic expression

  • show all steps-python Review the following BST and create the inorder, preorder and postorder list. Try...

    show all steps-python Review the following BST and create the inorder, preorder and postorder list. Try to delete the value 17 and after deletion create the inorder, preorder and postorder list. Follow both algorithms.  If the node has a left child and a right child, 1) replace the node’s value with the largest value in the left subtree and delete that value’s node from the left subtree. Or 2) replace the node’s value with the smallest value in the right subtree...

  • show all steps-python Review the following BST and create the inorder, preorder and postorder list. Follow...

    show all steps-python Review the following BST and create the inorder, preorder and postorder list. Follow both algorithms.  If the node has a left child and a right child, 1) replace the node’s value with the largest value in the left subtree and delete that value’s node from the left subtree. Or 2) replace the node’s value with the smallest value in the right subtree and delete that value’s node from the right subtree. -What are the two possible values for...

  • Min heap class implementation in Python. Implement a min-using an array. Your min-heap class will have...

    Min heap class implementation in Python. Implement a min-using an array. Your min-heap class will have one private attribute, an array of integers. Implement the following methods for the min-heap class You may not use the built-in min function. init - Constructor makes an empty heap str - Prints the heap out in any way you want for debugging only) makenull(self) - Makes the heap empty insert(self,x) - Insert element x into the heap parent(self,i) - Returns the index of...

  • Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to com...

    Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to completely test every method in the BST class to ensure the class meets all its requirements. You should read the Listing 25.5: TestBST.java for an idea of what your program should look like. Listing 25.4 BST.java public class BST> extends AbstractTree { protected TreeNode...

  • java A University would like to implement its students registery as a binary search tree, calledStudentBST....

    java A University would like to implement its students registery as a binary search tree, calledStudentBST. Write an Student node class, called StudentNode, to hold the following information about an Student: - id (as a int) - gpa (as double) StudentNode should have constructors and methods (getters, setters, and toString()) to manage Write the StudentBST class, which is a binary search tree to hold objects of the class StudentNode. The key in each node is the id. : import.java.ArrayList; public...

  • (Pre-order and post-order traversal). Implement the textbook's algorithm (not the Java implementation) for pre-order and post-order...

    (Pre-order and post-order traversal). Implement the textbook's algorithm (not the Java implementation) for pre-order and post-order traversal. To "process" or "visit" a node means to print the data at that node. Pre-order traversal output: 10 6 4 8 18 15 21 Post-order traversal output: 4 8 6 15 21 18 10 Additionally, create and traverse the following trees: Algorithm preorder(p) perform the "visit" action for position p this happens before any recursion for each child c in children(p) do recursively...

  • [Python] Construct Tree Using Inorder and Preorder Given Preorder and Inorder traversal of a binary tree,...

    [Python] Construct Tree Using Inorder and Preorder Given Preorder and Inorder traversal of a binary tree, create the binary tree associated with the traversals.You just need to construct the tree and return the root. Note: Assume binary tree contains only unique elements. Input format : Line 1 : n (Total number of nodes in binary tree) Line 2 : Pre order traversal Line 3 : Inorder Traversal Output Format : Elements are printed level wise, each level in new line...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT