I wanted to get the shortest route to leaf node height on BST using Python 3. So, I write the code like this.
class Node:
def __init__(self, key, value, left=None, right=None):
self.key = key
self.value = value
self.left = left
self.right = right
class BST:
def __init__(self):
self.root = None
def get(self, key):
self.get_item(self.root, key)
def get_item(self, n, k):
if n == None:
return None
if n.key > k:
return self.get_item(n.left, k)
elif n.key < k:
return self.get_item(n.right, k)
else:
return n.value
def put(self, key, value):
self.root = self.put_item(self.root, key, value)
def put_item(self, n, key, value):
if n == None:
return Node(key, value)
if n.key > key:
n.left = self.put_item(n.left, key, value)
elif n.key < key:
n.right = self.put_item(n.right, key, value)
else:
n.value = value
return n
def print_height(self):
h = self.min_height(self.root)
print(h)
def short_height(self, root):
if root == None:
return 0
return min(self.short_height(root.left), self.short_height(root.right)) +1
if __name__ == '__main__':
t = BST()
t.put(60, 'a')
t.put(50, 'b')
t.put(70, 'c')
t.put(20, 'd')
t.put(10, 'e')
t.put(45, 'f')
t.put(35, 'g')
t.put(25, 'h')
t.put(40, 'i')
t.put(30, 'j')
t.put(80, 'z')
t.print_height
Right result of print_height is '3', but I got the worng result '2'. (When I did't put the node(key:80, value:z), right result is '2', but this time I got '2')
To fix this problem, first, I changed the method 'short_height' like this. (To make sure that the code before +1 is well written)
def short_height(self, root):
if root == None:
return 0
return min(self.short_height(root.left), self.short_height(root.right)) +3
If it is normal, you should do +3 at height(not include root node) 2 of 80 and get 5 as a result, but since 6 came out, I confirmed that the code before +1 that was wrong.
I don't know how to fix this problem.