From 8a7c92659dcbc57965fbc36a07d6fcf41a945c60 Mon Sep 17 00:00:00 2001 From: Lilly Date: Sun, 10 Jul 2022 08:32:35 -0700 Subject: [PATCH] binary tree project --- binary_search_tree/tree.py | 125 +++++++++++++++++++++++++++++++------ 1 file changed, 106 insertions(+), 19 deletions(-) diff --git a/binary_search_tree/tree.py b/binary_search_tree/tree.py index cdd5abc..7ba19b0 100644 --- a/binary_search_tree/tree.py +++ b/binary_search_tree/tree.py @@ -14,35 +14,122 @@ class Tree: def __init__(self): self.root = None - # Time Complexity: - # Space Complexity: + # Time Complexity: O(log(n)) + # Space Complexity: O(n) def add(self, key, value = None): - pass - - # Time Complexity: - # Space Complexity: + if self.root == None: + self.root = TreeNode(key, value) + return + + current = self.root + while current != None: + if current.key > key: + if current.left == None: + current.left = TreeNode(key, value) + return + else: + current = current.left + else: + if current.right == None: + current.right = TreeNode(key, value) + return + else: + current = current.right + + # Time Complexity: o(1) + # Space Complexity: O(n) + # search for a node in the tree def find(self, key): - pass + current = self.root + while current != None: + if current.key == key: + return current.value + elif current.key > key: + current = current.left + else: + current = current.right + return None + + - # Time Complexity: - # Space Complexity: + # Time Complexity: O(n) + # Space Complexity: O(n) + def inorder_helper(self, current, result): + if current == None: + return result + else: + result = self.inorder_helper(current.left, result) + result.append({"key":current.key, "value":current.value}) + result = self.inorder_helper(current.right, result) + return result + def inorder(self): - pass + result = [] + current = self.root + if current == None: + return result + else: + result = self.inorder_helper(current, result) + return result - # Time Complexity: - # Space Complexity: + # Time Complexity: O(n) + # Space Complexity: O(n) + def preorder_helper(self, current, result): + if current == None: + return result + else: + result.append({"key":current.key, "value":current.value}) + result = self.preorder_helper(current.left, result) + result = self.preorder_helper(current.right, result) + return result def preorder(self): - pass + result = [] + current = self.root + if current == None: + return result + else: + result = self.preorder_helper(current, result) + return result + + # Time Complexity: O(n) + # Space Complexity: O(n) + def postorder_helper(self, current, result): + if current == None: + return result + else: + result = self.postorder_helper(current.left, result) + result = self.postorder_helper(current.right, result) + result.append({"key":current.key, "value":current.value}) + return result - # Time Complexity: - # Space Complexity: def postorder(self): - pass + result = [] + current = self.root + if current == None: + return result + else: + result = self.postorder_helper(current, result) + return result + + # Time Complexity: O(n) + # Space Complexity: O(n) + def height_helper(self, current): + if current == None: + return 0 + else: + left_height = self.height_helper(current.left) + right_height = self.height_helper(current.right) + if left_height > right_height: + return left_height + 1 + else: + return right_height + 1 - # Time Complexity: - # Space Complexity: def height(self): - pass + current = self.root + if current == None: + return 0 + else: + return self.height_helper(current) # # Optional Method