Last active
August 29, 2015 13:58
-
-
Save alysbrooks/10218553 to your computer and use it in GitHub Desktop.
Implementation of trees in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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