mirror of
https://github.com/jlengrand/Coding4Interviews.git
synced 2026-03-10 08:11:24 +00:00
Implement min, that find the lowest node value of a tree
This commit is contained in:
@@ -4,6 +4,7 @@ Binary Search Tree Implementation
|
||||
2013/07
|
||||
"""
|
||||
|
||||
|
||||
class BinarySearchTree():
|
||||
"""
|
||||
Defines a complete binary search Tree.
|
||||
@@ -24,17 +25,17 @@ class BinarySearchTree():
|
||||
the node.
|
||||
"""
|
||||
if self.size == 0:
|
||||
self.root_node = BinarySearchNode(value, None) # root node has no parent
|
||||
self.root_node = BinarySearchNode(value, None) # root node has no parent
|
||||
|
||||
else:
|
||||
node = self.root_node
|
||||
is_left = None
|
||||
while(node != None):
|
||||
ptr_node = node # keep parent in memory
|
||||
while(node is not None):
|
||||
ptr_node = node # keep parent in memory
|
||||
if node.value > value: # we go to the left child
|
||||
node = node.left_child
|
||||
is_left = True
|
||||
else: # we move to the right child. This means = case too
|
||||
else: # we move to the right child. This means = case too
|
||||
node = node.right_child
|
||||
is_left = False
|
||||
|
||||
@@ -52,10 +53,9 @@ class BinarySearchTree():
|
||||
Returns the highest node value of the tree.
|
||||
Otherwise, returns None
|
||||
"""
|
||||
|
||||
if self.size == 0:
|
||||
return None
|
||||
else: # we have values in the node
|
||||
else: # we have nodes in the tree
|
||||
node = self.root_node
|
||||
while(node.has_right_child()):
|
||||
node = node.right_child
|
||||
@@ -63,6 +63,22 @@ class BinarySearchTree():
|
||||
# we have the leaf
|
||||
return node.value
|
||||
|
||||
def min(self):
|
||||
"""
|
||||
Returns the lowest node value of the tree.
|
||||
Otherwise returns None
|
||||
"""
|
||||
if self.size == 0:
|
||||
return None
|
||||
else: # we have nodes in the tree
|
||||
node = self.root_node
|
||||
while(node.has_left_child()):
|
||||
node = node.left_child
|
||||
|
||||
# we have the leaf
|
||||
return node.value
|
||||
|
||||
|
||||
class BinarySearchNode():
|
||||
"""
|
||||
Defines any node of the Binary Search Tree.
|
||||
@@ -81,4 +97,3 @@ class BinarySearchNode():
|
||||
|
||||
def has_right_child(self):
|
||||
return self.right_child is not None
|
||||
|
||||
|
||||
@@ -21,11 +21,11 @@ class test_binary_search_tree(unittest.TestCase):
|
||||
|
||||
self.assertEqual(bst.size, 1)
|
||||
|
||||
node_val2= 4
|
||||
node_val2 = 4
|
||||
bst.add(node_val2)
|
||||
node_val3= 10
|
||||
node_val3 = 10
|
||||
bst.add(node_val3)
|
||||
node_val4= 16
|
||||
node_val4 = 16
|
||||
bst.add(node_val4)
|
||||
self.assertEqual(bst.size, 4)
|
||||
|
||||
@@ -40,11 +40,11 @@ class test_binary_search_tree(unittest.TestCase):
|
||||
bst = BinarySearchTree()
|
||||
node_val1 = 6
|
||||
bst.add(node_val1)
|
||||
node_val2= 4
|
||||
node_val2 = 4
|
||||
bst.add(node_val2)
|
||||
node_val3= 10
|
||||
node_val3 = 10
|
||||
bst.add(node_val3)
|
||||
node_val4= 16
|
||||
node_val4 = 16
|
||||
bst.add(node_val4)
|
||||
self.assertEqual(bst.size, 4)
|
||||
|
||||
@@ -55,11 +55,10 @@ class test_binary_search_tree(unittest.TestCase):
|
||||
self.assertTrue(node.has_right_child())
|
||||
self.assertEqual(node.right_child.value, node_val3)
|
||||
|
||||
node = node.left_child # we have 4 now
|
||||
node = node.left_child # we have 4 now
|
||||
self.assertFalse(node.has_left_child())
|
||||
self.assertFalse(node.has_right_child())
|
||||
|
||||
|
||||
def test_max(self):
|
||||
|
||||
# 6
|
||||
@@ -74,15 +73,39 @@ class test_binary_search_tree(unittest.TestCase):
|
||||
|
||||
node_val1 = 6
|
||||
bst.add(node_val1)
|
||||
node_val2= 4
|
||||
node_val2 = 4
|
||||
bst.add(node_val2)
|
||||
node_val3= 10
|
||||
node_val3 = 10
|
||||
bst.add(node_val3)
|
||||
node_val4= 16
|
||||
node_val4 = 16
|
||||
bst.add(node_val4)
|
||||
self.assertEqual(bst.size, 4)
|
||||
|
||||
self.assertEqual(bst.max(), node_val4)
|
||||
|
||||
def test_min(self):
|
||||
|
||||
# 6
|
||||
# / \
|
||||
# 4 10
|
||||
# \
|
||||
# 16
|
||||
|
||||
bst = BinarySearchTree()
|
||||
|
||||
self.assertEqual(bst.min(), None)
|
||||
|
||||
node_val1 = 6
|
||||
bst.add(node_val1)
|
||||
node_val2 = 4
|
||||
bst.add(node_val2)
|
||||
node_val3 = 10
|
||||
bst.add(node_val3)
|
||||
node_val4 = 16
|
||||
bst.add(node_val4)
|
||||
self.assertEqual(bst.size, 4)
|
||||
|
||||
self.assertEqual(bst.min(), node_val2)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user