Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
BST treversal
Creates a tree of given values and gives inorder, preorder and postorder of a tree.
  • Loading branch information
Kshitiz-Jain authored Oct 2, 2019
commit 2bad5c1ab32872100dba0bf5a4534ec96601928d
62 changes: 62 additions & 0 deletions TreeTravesal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@


class Node(object):
"""docstring for Node"""
def __init__(self, val):
self.key = val
self.left=None
self.right=None


class Tree:
"""docstring for Tree"""
def __init__(self,val):
self.root = Node(val)

def insertNode(root,val):
if(root==None):
root=Node(val)
elif(root.key<val):
root.right=Tree.insertNode(root.right,val)
else:
root.left=Tree.insertNode(root.left,val)
return root

def inorder(root):
if(root==None):
return
else:
Tree.inorder(root.left)
print(root.key)
Tree.inorder(root.right)

def preorder(root):
if(root==None):
return
else:
print(root.key)
Tree.preorder(root.left)
Tree.preorder(root.right)

def postorder(root):
if(root==None):
return
else:
Tree.postorder(root.left)
Tree.postorder(root.right)
print(root.key)



array=[1,22,3,44,32,35]
treeRoot=Node(array[0])
for i in range(1,len(array)):
treeRoot=Tree.insertNode(treeRoot,array[i])

Tree.inorder(treeRoot)
Tree.preorder(treeRoot)
Tree.postorder(treeRoot)