Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rock- Briyana Haywood #38

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
125 changes: 100 additions & 25 deletions binary_search_tree/tree.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
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

Expand All @@ -14,36 +14,113 @@ class Tree:
def __init__(self):
self.root = None

# Time Complexity:
# Space Complexity:

def add_helper(self, current_node, key, value):

Choose a reason for hiding this comment

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

Nice recursive helper!

if current_node == None:
return TreeNode(key, value)
if key <= current_node.key:
current_node.left = self.add_helper(current_node.left, key, value)
else:
current_node.right = self.add_helper(current_node.right, key, value)
return current_node


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

Choose a reason for hiding this comment

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

👍 Good to note the balanced requirement

pass
if self.root == None:
self.root = TreeNode(key, value)
else:
self.add_helper(self.root, key, value)
Comment on lines +31 to +34

Choose a reason for hiding this comment

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

Could be rewritten as:

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



def find_helper(self, current, key):

Choose a reason for hiding this comment

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

Another good recursive helper

if current == None:
return None
elif current.key == key:
return current.value
elif key < current.key:
return self.find_helper(current.left, key)
else:
return self.find_helper(current.right, key)


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

Choose a reason for hiding this comment

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

👍

pass
return self.find_helper(self.root, key)


def inorder_helper(self, current, traversal_list):
if current == None:
return None
self.inorder_helper(current.left, traversal_list)
traversal_list.append({
"key": current.key,
"value": current.value
})
self.inorder_helper(current.right, traversal_list)


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

Choose a reason for hiding this comment

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

👍

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


def preorder_helper(self, current, traversal_list):
if current != None:
traversal_list.append({
"key": current.key,
"value": current.value
})
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(n)
def preorder(self):
Comment on lines +83 to 85

Choose a reason for hiding this comment

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

👍

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


def postorder_helper(self, current, traversal_list):
if current == None:
return None
self.postorder_helper(current.left, traversal_list)
self.postorder_helper(current.right, traversal_list)
traversal_list.append({
"key": current.key,
"value": current.value
})

# Time Complexity:
# Space Complexity:

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

Choose a reason for hiding this comment

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

👍

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


def height_helper(self, current):

Choose a reason for hiding this comment

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

nice!

if current == None:
return 0
else:
left = self.height_helper(current.left)
right = self.height_helper(current.right)
return max(left,right) + 1

# Time Complexity:
# Space Complexity:
def height(self):
pass

# Time Complexity: O(n)
# Space Complexity: O(n)
def height(self):
Comment on lines +119 to +121

Choose a reason for hiding this comment

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

👍 , the space complexity is the height of the tree (which worst case is n)

current = self.root
return self.height_helper(current)

# # Optional Method
# # Time Complexity:
Expand All @@ -52,8 +129,6 @@ def bfs(self):
pass




# # Useful for printing
def to_s(self):
return f"{self.inorder()}"