Last active
January 24, 2019 17:37
-
-
Save pepasflo/6ea0a713bb63a2f89b79772794d5b3cd to your computer and use it in GitHub Desktop.
Interviewcake problem: is a tree "superbalanced"? https://www.interviewcake.com/question/python/balanced-binary-tree
This file contains hidden or 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
| #!/usr/bin/env python | |
| class BinaryTreeNode(object): | |
| def __init__(self, value): | |
| self.value = value | |
| self.left = None | |
| self.right = None | |
| def insert_left(self, value): | |
| self.left = BinaryTreeNode(value) | |
| return self.left | |
| def insert_right(self, value): | |
| self.right = BinaryTreeNode(value) | |
| return self.right | |
| def difference(a, b): | |
| return abs(a - b) | |
| def depths(tree, depth=1): | |
| if tree.left and tree.right: | |
| # we have both right and left children | |
| (lmin, lmax) = depths(tree.left, depth+1) | |
| (rmin, rmax) = depths(tree.right, depth+1) | |
| return (min(lmin, rmin), max(lmax, rmax)) | |
| elif tree.left: | |
| # we only have a left child | |
| return depths(tree.left, depth+1) | |
| elif tree.right: | |
| # we only have a right child | |
| return depths(tree.right, depth+1) | |
| else: | |
| # left and right are both None | |
| return (depth, depth) | |
| def is_superbalanced(tree): | |
| (min_depth, max_depth) = depths(tree) | |
| return difference(min_depth, max_depth) <= 1 | |
| def test1(): | |
| # tree (is superbalanced): | |
| # o | |
| # | |
| root = BinaryTreeNode(None) | |
| assert is_superbalanced(root) == True | |
| def test2(): | |
| # tree (is superbalanced): | |
| # o | |
| # / | |
| # o | |
| root = BinaryTreeNode(None) | |
| root.insert_left(None) | |
| assert is_superbalanced(root) == True | |
| def test3(): | |
| # tree (is superbalanced): | |
| # o | |
| # / \ | |
| # o o | |
| root = BinaryTreeNode(None) | |
| root.insert_left(None) | |
| root.insert_right(None) | |
| assert is_superbalanced(root) == True | |
| def test4(): | |
| # tree (is superbalanced): | |
| # o | |
| # / | |
| # o | |
| # / | |
| # o | |
| root = BinaryTreeNode(None) | |
| child = root.insert_left(None) | |
| child = child.insert_left(None) | |
| assert is_superbalanced(root) == True | |
| def test5(): | |
| # tree (is superbalanced): | |
| # o | |
| # / \ | |
| # o o | |
| # / | |
| # o | |
| root = BinaryTreeNode(None) | |
| child = root.insert_left(None) | |
| child = child.insert_left(None) | |
| child = root.insert_right(None) | |
| assert is_superbalanced(root) == True | |
| def test6(): | |
| # tree (is NOT superbalanced): | |
| # o | |
| # / \ | |
| # o o | |
| # / | |
| # o | |
| # / | |
| # o | |
| root = BinaryTreeNode(None) | |
| child = root.insert_left(None) | |
| child = child.insert_left(None) | |
| child = child.insert_left(None) | |
| child = root.insert_right(None) | |
| assert is_superbalanced(root) == False | |
| if __name__ == "__main__": | |
| test1() | |
| test2() | |
| test3() | |
| test4() | |
| test5() | |
| test6() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment