diff --git a/.gitignore b/.gitignore index 953cd6f..4156916 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ *.tar.gz /9780596000851-master/ + +.dir-locals.el \ No newline at end of file diff --git a/BST.py b/BST.py index d1ded9a..c9b1e8a 100644 --- a/BST.py +++ b/BST.py @@ -4,6 +4,15 @@ class BSTNode: self.left = None self.right = None + def height(self): + lh = 0 + rh = 0 + if self.left is not None: + lh = self.left.height() + if self.right is not None: + rh = self.right.height() + return 1 + max(lh, rh) + class BST: def __init__(self): @@ -49,7 +58,7 @@ class BST: return tree def height(self): - return rec_height(self.root) + return self.root.height() def inOrderRec(top, ret): @@ -62,12 +71,6 @@ def inOrderRec(top, ret): inOrderRec(top.right, ret) -def rec_height(node: BSTNode): - if node is None: - return 0 - return 1 + max(map(rec_height, (node.left, node.right))) - - def add_rec(start, end, list_param, tree: BST): if end - start >= 0: mid = (start + end) // 2