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);...

  • Code in C++ Instructions Write a program which... Prompt the user to enter the number of...

    Code in C++ Instructions Write a program which... Prompt the user to enter the number of values that they would like to insert into a binary search tree. Prompt the user to insert, one-at-a-time, a value into the binary search tree. Print the preorder, postorder, and inorder traversal on the tree. Binary Search Tree The methods preorder, postorder, inorder, preorderR, postorderR, and inorderR will all contains the necessary screen output statements to verify that the traversal is happening correctly (see...

  • 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...

  • C++ ONLY Threaded Binary Search Tree Since a binary search tree with N nodes has N...

    C++ ONLY Threaded Binary Search Tree Since a binary search tree with N nodes has N + 1 NULL pointers, half the space allocated in a binary search tree for pointer information is wasted. Suppose that if a node has a NULL left child, we make its left child pointer link to its inorder predecessor, and if a node has a NULL right child, we make its right child pointer link to its inorder successor. This is known as a...

  • 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...

  • Write an implementation of a generic java BinaryTree class that fits any primitive data type. your...

    Write an implementation of a generic java BinaryTree class that fits any primitive data type. your class at minimum should have the following. Node class with a minimum of three fields and 3 constructor. BinaryTree class should include the following at least 3 Fields, at least 2 constructors, add, remove, find, getSize method (which returns the amount nods in the tree), getHeight (should return the height), countLeftNode (Should return the mount of left node found in the tree), countRightNode (Should...

  • 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...

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