Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 95 additions & 26 deletions binary_search_tree/tree.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,115 @@
class TreeNode:
def __init__(self, key, val = None):
if val == None:
val = key

class TreeNode:
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



class Tree:
def __init__(self):
self.root = None

def add_helper(self, current, key, value):
if not current:
return TreeNode(key,value)
if key <= current.key:
current.left = self.add_helper(current.left,key,value)
else:
current.right = self.add_helper(current.right,key,value)
return current

# Time Complexity:
# Space Complexity:

# Time Complexity: O(log n)
# Space Complexity: O(log n)
def add(self, key, value = None):
Comment on lines +26 to 28

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Nice recursive solution

pass
if self.root == None:
self.root = TreeNode(key,value)
else:
self.add_helper(self.root, key,value)

# Time Complexity:
# Space Complexity:

# Time Complexity: O(n)
# Space Complexity: O(n)
def find(self, key):
Comment on lines +35 to 37

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 However the space complexity is O(1) because it's an iterative solution (no call stack).

The time complexity is O(log n) if the tree is balanced and O(n) worst-case.

pass

# Time Complexity:
# Space Complexity:
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: O(n)
# Space Complexity: O(n)
def inorder(self):
Comment on lines +48 to 50

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

pass
tree_nodes = []
return self.inorder_helper(self.root, tree_nodes)

def inorder_helper(self, root, tree_nodes):
if root:
self.inorder_helper(root.left, tree_nodes)
tree_nodes.append({
"key":root.key,
"value":root.value
})
self.inorder_helper(root.right, tree_nodes)
return tree_nodes


# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(n)
def preorder(self):
Comment on lines +65 to 67

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

pass
tree_nodes = []
return self.preorder_helper(self.root, tree_nodes)

def preorder_helper(self, root, tree_nodes):
if root:
tree_nodes.append({
"key":root.key,
"value":root.value
})
self.preorder_helper(root.left, tree_nodes)
self.preorder_helper(root.right, tree_nodes)
return tree_nodes


# Time Complexity:
# Space Complexity:

# Time Complexity: O(n)
# Space Complexity: O(n)
def postorder(self):
Comment on lines +83 to 85

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

pass
tree_nodes = []
return self.postorder_helper(self.root, tree_nodes)

def postorder_helper(self, root, tree_nodes):
if root:
self.postorder_helper(root.left, tree_nodes)
self.postorder_helper(root.right, tree_nodes)
tree_nodes.append({
"key":root.key,
"value":root.value
})
return tree_nodes

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(h)
def height(self):
Comment on lines +99 to 101

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Nice work on time/space

pass
return self.height_helper(self.root)

def height_helper(self, current):
if not current:
return 0

left = self.height_helper(current.left)
right = self.height_helper(current.right)
return max(left, right)+1




# # Optional Method
Expand All @@ -57,3 +124,5 @@ def bfs(self):
# # Useful for printing
def to_s(self):
return f"{self.inorder()}"