mirror of
https://github.com/jlengrand/Coding4Interviews.git
synced 2026-03-10 08:11:24 +00:00
enhances print node to take care of more special cases
This commit is contained in:
@@ -134,4 +134,13 @@ class BinarySearchNode():
|
||||
"""
|
||||
prints a single tree node.
|
||||
"""
|
||||
return (str(self.left_child.value) + " " + str(self.value) + " " + str(self.right_child.value) )
|
||||
ret = ""
|
||||
if self.has_left_child():
|
||||
ret += str(self.left_child.value) + " "
|
||||
|
||||
ret += str(self.value)
|
||||
|
||||
if self.has_right_child():
|
||||
ret += " " + str(self.right_child.value)
|
||||
|
||||
return ret
|
||||
@@ -208,5 +208,9 @@ class test_binary_search_tree(unittest.TestCase):
|
||||
|
||||
self.assertEqual(bst.__str__(), "4 6 10")
|
||||
|
||||
self.assertEqual(bst.root_node.right_child.__str__(), "10 16")
|
||||
self.assertEqual(bst.root_node.right_child.right_child.__str__(), "16")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user