The link below is the photo of the problem. Please create a code without using def
Determine in order, post order and pre order
https://www.bartleby.com/questions-and-answers/52-37-64-24-39-59-73-19-29-56-in-order-pre-order-post-order/100dd94b-74dc-49e0-a700-97b1a7f6afbd
Algorithm Inorder(tree)
1. Traverse the left subtree, i.e., call Inorder(left-subtree)
2. Visit the root.
3. Traverse the right subtree, i.e., call Inorder(right-subtree)
Algorithm Preorder(tree)
1. Visit the root.
2. Traverse the left subtree, i.e., call Preorder(left-subtree)
3. Traverse the right subtree, i.e., call Preorder(right-subtree)
Algorithm Postorder(tree)
1. Traverse the left subtree, i.e., call Postorder(left-subtree)
2. Traverse the right subtree, i.e., call Postorder(right-subtree)
3. Visit the root.
class Node:
def __init__(self, key):
self.left = None
self.right = None
self.val = key
def printInorder(root):
if root:
printInorder(root.left)
print(root.val, end=' ')
printInorder(root.right)
def printPostorder(root):
if root:
printPostorder(root.left)
printPostorder(root.right)
print(root.val, end=' ')
def printPreorder(root):
if root:
print(root.val, end=' ')
printPreorder(root.left)
printPreorder(root.right)
root = Node(52)
root.left = Node(37)
root.right = Node(64)
root.left.left = Node(24)
root.left.right = Node(39)
root.right.left = Node(59)
root.right.right = Node(73)
root.left.left.left = Node(19)
root.left.left.right = Node(29)
root.right.left.left = Node(56)
print("\nPreorder traversal of binary tree is")
printPreorder(root)
print("\nInorder traversal of binary tree is")
printInorder(root)
print("\nPostorder traversal of binary tree is")
printPostorder(root)
Comments
Leave a comment