Browse Source

Made height a node method too

master
Raphael Roberts 6 years ago
parent
commit
f9b1f89c5b
  1. 2
      .gitignore
  2. 17
      BST.py

2
.gitignore

@ -1,2 +1,4 @@
*.tar.gz *.tar.gz
/9780596000851-master/ /9780596000851-master/
.dir-locals.el

17
BST.py

@ -4,6 +4,15 @@ class BSTNode:
self.left = None self.left = None
self.right = 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: class BST:
def __init__(self): def __init__(self):
@ -49,7 +58,7 @@ class BST:
return tree return tree
def height(self): def height(self):
return rec_height(self.root)
return self.root.height()
def inOrderRec(top, ret): def inOrderRec(top, ret):
@ -62,12 +71,6 @@ def inOrderRec(top, ret):
inOrderRec(top.right, 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): def add_rec(start, end, list_param, tree: BST):
if end - start >= 0: if end - start >= 0:
mid = (start + end) // 2 mid = (start + end) // 2

Loading…
Cancel
Save