Skip to content

Instantly share code, notes, and snippets.

@alysbrooks
Last active August 29, 2015 13:58
Show Gist options
  • Save alysbrooks/10218553 to your computer and use it in GitHub Desktop.
Save alysbrooks/10218553 to your computer and use it in GitHub Desktop.
Implementation of trees in Python
class Node:
def __init__(self, left, right):
self.left = left
self.right = right
def n(left=None, right=None):
"""Shortcut method for making a tree."""
return Node(left, right)
test = Node(n(), n())
def height(node):
if node is None:
return -1
elif node.left is None and node.right is None:
return 0
else:
return max(height(node.left), height(node.right)) + 1
def isAVLTree(node):
if node is None:
return True
elif node.left is None and node.right is None:
return True
elif (isAVLTree(node.left) and isAVLTree(node.right)) and \
abs(height(node.left) - height(node.right)) <= 1:
return True
else:
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment