initial commit
commit
49186d2b98
@ -0,0 +1 @@
|
||||
.DS_STORE
|
@ -0,0 +1,69 @@
|
||||
# Node
|
||||
class Node:
|
||||
def __init__(self, key):
|
||||
self.left = None
|
||||
self.right = None
|
||||
self.val = key
|
||||
|
||||
# A function to do inorder tree traversal
|
||||
def printInorder(root):
|
||||
if root:
|
||||
# First recur on left child
|
||||
printInorder(root.left)
|
||||
# then print the data of node
|
||||
print(root.val),
|
||||
# now recur on right child
|
||||
printInorder(root.right)
|
||||
|
||||
# A function to do 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.val),
|
||||
|
||||
# A function to do preorder tree traversal
|
||||
def printPreorder(root):
|
||||
if root:
|
||||
# First print the data of node
|
||||
print(root.val),
|
||||
# Then recur on left child
|
||||
printPreorder(root.left)
|
||||
# Finally recur on right child
|
||||
printPreorder(root.right)
|
||||
|
||||
def addData(root, value):
|
||||
if value > root.val:
|
||||
if root.right == None:
|
||||
root.right = Node(value)
|
||||
else:
|
||||
addData(root.right, value)
|
||||
else:
|
||||
if root.left == None:
|
||||
root.left = Node(value)
|
||||
else:
|
||||
addData(root.left, value)
|
||||
|
||||
# Driver code
|
||||
root = Node(5)
|
||||
root.left = Node(3)
|
||||
root.right = Node(6)
|
||||
root.left.left = Node(2)
|
||||
root.left.right = Node(4)
|
||||
addData(root, 799)
|
||||
addData(root,300)
|
||||
|
||||
print("Preorder 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)
|
||||
|
||||
print("\nPreorder traversal of binary tree is")
|
||||
printPreorder(root)
|
Loading…
Reference in New Issue