Skip to content

Instantly share code, notes, and snippets.

@ishankhare07
Last active September 9, 2015 12:11
Show Gist options
  • Save ishankhare07/711fd0c0cf8f4e3c699c to your computer and use it in GitHub Desktop.
Save ishankhare07/711fd0c0cf8f4e3c699c to your computer and use it in GitHub Desktop.
avl tree implementation
class Node:
def __init__(self, num):
self.data = num
self.left = None
self.right = None
root = None
def add_to_tree(element, root):
if root == None:
root = Node(element)
elif element > root.data:
root.right = add_to_tree(element, root.right)
elif element < root.data:
root.left = add_to_tree(element, root.left)
return root
def treeprint(root, indent=0):
if root:
treeprint(root.left, indent + 1)
print(" " * indent, root.data)
treeprint(root.right, indent + 1)
while True:
try:
c = input()
except EOFError as EOF:
break
root = add_to_tree(int(c), root)
treeprint(root)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment