From 2eb73618710da16a6102b05cb0420177ffc7df34 Mon Sep 17 00:00:00 2001 From: Araceli Date: Sun, 23 Jan 2022 18:06:58 -0800 Subject: [PATCH] mandatory tests passing --- binary_search_tree/tree.py | 153 +++++++++++++++++++++++++++++++------ 1 file changed, 129 insertions(+), 24 deletions(-) diff --git a/binary_search_tree/tree.py b/binary_search_tree/tree.py index cdd5abc..ac7b252 100644 --- a/binary_search_tree/tree.py +++ b/binary_search_tree/tree.py @@ -1,10 +1,12 @@ + + class TreeNode: - def __init__(self, key, val = None): - if val == None: - val = key + def __init__(self, key, value = None): + if value == None: + value = key self.key = key - self.value = val + self.value = value self.left = None self.right = None @@ -14,36 +16,141 @@ 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): - 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: + # Time Complexity: O(n) + # Space Complexity: O(1) def find(self, key): - pass + + if self.root == None: + return None + + current = self.root + + while current != None: + if current.key == key: + return current.value + elif key < current.key: + current = current.left + else: + current = current.right + + return None - # Time Complexity: - # Space Complexity: + # Time Complexity: O(n) + # Space Complexity: O(1) def inorder(self): - pass + tree_nodes = [] + + if self.root == None: + return tree_nodes + else: + return self.inorder_helper(self.root, tree_nodes) + + def inorder_helper(self, current, tree_nodes): + if current == None: + return None + + self.inorder_helper(current.left, tree_nodes) + + tree_nodes.append( + { + "key": current.key, + "value": current.value + } + ) - # Time Complexity: - # Space Complexity: + self.inorder_helper(current.right, tree_nodes) + + return tree_nodes + + # Time Complexity: O(n) + # Space Complexity: O(1) def preorder(self): - pass + tree_nodes = [] + + if self.root == None: + return tree_nodes + else: + return self.preorder_helper(self.root, tree_nodes) - # Time Complexity: - # Space Complexity: + def preorder_helper(self, current, tree_nodes): + if current == None: + return None + + tree_nodes.append( + { + "key": current.key, + "value": current.value + } + ) + + self.preorder_helper(current.left, tree_nodes) + + self.preorder_helper(current.right, tree_nodes) + + return tree_nodes + + # Time Complexity: O(n) + # Space Complexity: O(1) def postorder(self): - pass + tree_nodes = [] + + + return self.postorder_helper(self.root, tree_nodes) + + def postorder_helper(self, current, tree_nodes): + if current == None: + return tree_nodes + + self.postorder_helper(current.left, tree_nodes) + + self.postorder_helper(current.right, tree_nodes) - # Time Complexity: - # Space Complexity: + tree_nodes.append( + { + "key": current.key, + "value": current.value + } + ) + + return tree_nodes + + # Time Complexity: O(n) + # Space Complexity: O(1) def height(self): - pass + return self.height_helper(self.root) + + 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 max(left_height, right_height) + 1 # # Optional Method # # Time Complexity: @@ -51,8 +158,6 @@ def height(self): def bfs(self): pass - - # # Useful for printing def to_s(self):