class Node:
def __init__(self,value):
self.left = None
self.data = value
self.right = None
class Tree:
def createNode(self,data):
return Node(data)
def insert(self,node,data):
if node is None:
return self.createNode(data)
if data < node.data:
node.left = self.insert(node.left,data)
elif data > node.data:
node.right = self.insert(node.right,data)
return node
def traversePreorder(self,root):
if root is not None:
print root.data
self.traversePreorder(root.left)
self.traversePreorder(root.right)
def main():
root = None
tree = Tree()
root = tree.insert(root,10)
print root
tree.insert(root,20)
tree.insert(root,30)
tree.insert(root,40)
tree.insert(root,70)
print "Traverse Inorder"
tree.traversePreorder(root)
if __name__ == "__main__":
main()
Comments
Post a Comment