From c0a2c71c71cdbb39967fa9fee14e92e97916104f Mon Sep 17 00:00:00 2001 From: Priscille Gwet Date: Sun, 2 Jan 2022 23:55:06 -0600 Subject: [PATCH 1/2] code additions/edits --- binary_search_tree/tree.py | 87 +++++++++++++++++++--- tests/__pycache__/__init__.cpython-39.pyc | Bin 152 -> 149 bytes tests/test_binary_search_tree.py | 5 +- 3 files changed, 78 insertions(+), 14 deletions(-) diff --git a/binary_search_tree/tree.py b/binary_search_tree/tree.py index cdd5abc..6a04d38 100644 --- a/binary_search_tree/tree.py +++ b/binary_search_tree/tree.py @@ -7,6 +7,9 @@ def __init__(self, key, val = None): self.value = val self.left = None self.right = None + + def dict(self): + return {"key": self.key, "value": self.value} @@ -17,32 +20,96 @@ def __init__(self): # Time Complexity: # Space Complexity: def add(self, key, value = None): - pass + tree = TreeNode(key, value) + + if self.root == None: + self.root = tree + else: + current = self.root + + while current != None: + if key == current.key: + return None + if key < current.key: + if current.left == None: + current.left = tree + current = current.left + elif key > current.key: + if current.right == None: + current.right = tree + current = current.right # Time Complexity: # Space Complexity: def find(self, key): - pass + if self.root == None: + return None + current = self.root + while current != None: + if key == current.key: + return current.value + elif key > current.key: + current = current.right + else: + current = current.left + return None + + def inorder_helper(self, current, traversal_list): + if current == None: + return + + self.inorder_helper(current.left, traversal_list) + traversal_list.append(current.dict()) + self.inorder_helper(current.right, traversal_list) # Time Complexity: # Space Complexity: def inorder(self): - pass + traversal_list = [] + self.inorder_helper(self.root, traversal_list) + return traversal_list + + def preorder_helper(self, current, traversal_list): + if current == None: + return + + traversal_list.append(current.dict()) + self.preorder_helper(current.left, traversal_list) + self.preorder_helper(current.right, traversal_list) # Time Complexity: # Space Complexity: def preorder(self): - pass + traversal_list = [] + self.preorder_helper(self.root, traversal_list) + return traversal_list + + def postorder_helper(self, current, traversal_list): + if current == None: + return + + self.postorder_helper(current.left, traversal_list) + self.postorder_helper(current.right, traversal_list) + traversal_list.append(current.dict()) # Time Complexity: - # Space Complexity: + # Space Complexity: def postorder(self): - pass + traversal_list = [] + self.postorder_helper(self.root, traversal_list) + return traversal_list + + def height_helper(self, current): + if current == None: + return 0 + left_height = self.height_helper(current.left) + right_height = self.height_helper(current.right) + return 1 +max(left_height, right_height) # Time Complexity: # Space Complexity: def height(self): - pass + return self.height_helper(self.root) # # Optional Method @@ -50,10 +117,6 @@ def height(self): # # Space Complexity: def bfs(self): pass - - - - -# # Useful for printing +# Useful for printing def to_s(self): return f"{self.inorder()}" diff --git a/tests/__pycache__/__init__.cpython-39.pyc b/tests/__pycache__/__init__.cpython-39.pyc index aa55c2ed2909da5566d50f92972b26b188bfaed0..719ac0b0ca9c2536ed7b3f63b24c6bf7f5ba7de1 100644 GIT binary patch delta 52 zcmbQiIF*q%k(ZZ?0SNlv`6Mz<W3Dm78UC!XB1@?=O!oSCFbN*>L;co>X)SE J7EDYs1^|KM5N-ef diff --git a/tests/test_binary_search_tree.py b/tests/test_binary_search_tree.py index c2339e2..b7d642f 100644 --- a/tests/test_binary_search_tree.py +++ b/tests/test_binary_search_tree.py @@ -144,10 +144,11 @@ def test_height_of_many_node_tree(tree_with_nodes): tree_with_nodes.add(2, "pasta") tree_with_nodes.add(2.5, "bread") assert tree_with_nodes.height() == 5 - + +@pytest.mark.skip(reason="optional") def test_bfs_with_empty_tree(empty_tree): assert empty_tree.bfs() == [] - +@pytest.mark.skip(reason="optional") def test_bfs_with_tree_with_nodes(tree_with_nodes): expected_answer = [ { From ef39f19b3026898b580c4e5baa00f91eb577f450 Mon Sep 17 00:00:00 2001 From: Priscille Gwet Date: Sun, 2 Jan 2022 23:59:55 -0600 Subject: [PATCH 2/2] update file --- binary_search_tree/tree.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/binary_search_tree/tree.py b/binary_search_tree/tree.py index 6a04d38..1eade14 100644 --- a/binary_search_tree/tree.py +++ b/binary_search_tree/tree.py @@ -17,8 +17,8 @@ class Tree: def __init__(self): self.root = None - # Time Complexity: - # Space Complexity: + # Time Complexity: O(n) + # Space Complexity: O(1) def add(self, key, value = None): tree = TreeNode(key, value) @@ -39,8 +39,8 @@ def add(self, key, value = None): current.right = tree current = current.right - # Time Complexity: - # Space Complexity: + # Time Complexity: O(n) + # Space Complexity: O(1) def find(self, key): if self.root == None: return None @@ -62,8 +62,8 @@ def inorder_helper(self, current, traversal_list): traversal_list.append(current.dict()) self.inorder_helper(current.right, traversal_list) - # Time Complexity: - # Space Complexity: + # Time Complexity: O(n) + # Space Complexity: O(1) def inorder(self): traversal_list = [] self.inorder_helper(self.root, traversal_list) @@ -77,8 +77,8 @@ def preorder_helper(self, current, traversal_list): self.preorder_helper(current.left, traversal_list) self.preorder_helper(current.right, traversal_list) - # Time Complexity: - # Space Complexity: + # Time Complexity: O(n) + # Space Complexity: O(1) def preorder(self): traversal_list = [] self.preorder_helper(self.root, traversal_list) @@ -92,8 +92,8 @@ def postorder_helper(self, current, traversal_list): self.postorder_helper(current.right, traversal_list) traversal_list.append(current.dict()) - # Time Complexity: - # Space Complexity: + # Time Complexity: O(n) + # Space Complexity: O(1) def postorder(self): traversal_list = [] self.postorder_helper(self.root, traversal_list) @@ -106,8 +106,8 @@ def height_helper(self, current): right_height = self.height_helper(current.right) return 1 +max(left_height, right_height) - # Time Complexity: - # Space Complexity: + # Time Complexity: O(n) + # Space Complexity: O(1) def height(self): return self.height_helper(self.root)