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

Mariela Cruz - Scissors #35

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
124 changes: 104 additions & 20 deletions binary_search_tree/tree.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
class TreeNode:
# each tree node will have a key: maintin the order by ex. student id
# value ex. student object
# we sorting by keys
# value is the data we are storing
def __init__(self, key, val = None):
if val == None:
val = key
Expand All @@ -14,40 +18,120 @@ class Tree:
def __init__(self):
self.root = None

# Time Complexity:
# Space Complexity:
def add(self, key, value = None):
pass
def add_helper(self, current_node, key, value):
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:

# Time Complexity: O(log n) - balanced tree
# Space Complexity: O (log n) - recursive approach
def add(self, key, value = None):
Comment on lines +31 to +33

Choose a reason for hiding this comment

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

👍

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

# iterative solution
#if self.root == None:
# self.root = TreeNode(key,value)
#else:
# parent = None
# current = self.root
# while current != None:
# parent = current
# if current.key > key:
# current = current.left
# else:
# current = current.right
# if parent.key > key:
# parent.left = TreeNode(key,value)
# else:
# parent.right = TreeNode(key, value)
# Time Complexity: O(log n)
# Space Complexity:
def find(self, key):

Choose a reason for hiding this comment

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

👍 Nice iterative solution

pass
if self.root == None:
return None
else:
current = self.root
while current != None:
if current.key == key:
return current.value
elif current.key > key:
current = current.left
else:
current = current.right

def inorder_helper(self, current, inorder_list):

Choose a reason for hiding this comment

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

👍

if current != None:
self.inorder_helper(current.left, inorder_list)
inorder_list.append({"key": current.key,"value":current.value})
self.inorder_helper(current.right, inorder_list)


# Time Complexity:
# Space Complexity:

# Time Complexity: O(logn)
# Space Complexity: O (1) - iterative approach
# you get all the elements in order
def inorder(self):
pass
inorder_list = []
self.inorder_helper(self.root, inorder_list)
return inorder_list

# helper function for pre order
def preorder_helper(self, current, traversal_list):
Comment on lines +86 to +87

Choose a reason for hiding this comment

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

👍

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(log n)
# Space Complexity:
# used if you are saving a tree to an array or a file. you canread the array and get back the same
# root, left, right
def preorder(self):
pass
traversal_list = []
self.preorder_helper(self.root, traversal_list)
return traversal_list

def postorder_helper(self, current, postorder_list):
if current == None:
return postorder_list
elif current != None:
self.preorder_helper(current.left, postorder_list)
self.preorder_helper(current.right, postorder_list)
postorder_list.append({"key": current.key,"value":current.value})
Comment on lines +106 to +108

Choose a reason for hiding this comment

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

Suggested change
self.preorder_helper(current.left, postorder_list)
self.preorder_helper(current.right, postorder_list)
postorder_list.append({"key": current.key,"value":current.value})
postorder_list.append({"key": current.key,"value":current.value})
self.postorder_helper(current.left, postorder_list)
self.postorder_helper(current.right, postorder_list)


# Time Complexity:
# Space Complexity:
# Time Complexity: O(logn)
# Space Complexity: O (1)
# easiest way to delete all the nodes at one time
def postorder(self):
pass
postorder_list = []
self.postorder_helper(self.root, postorder_list)
return postorder_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.

👍

if current == None:
return 0
left_count = self.height_helper(current.left)
right_count = self.height_helper(current.right)

# Time Complexity:
# Space Complexity:
return max(left_count, right_count) + 1

# Time Complexity: O(logn)
# Space Complexity: O (1)
def height(self):
Comment on lines +126 to 128

Choose a reason for hiding this comment

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

👍 Since the helper is recursive, we have O(log n) space complexity for a balanced tree.

pass
return self.height_helper(self.root)


# # Optional Method
# # Time Complexity:
# # Space Complexity:
# # Time Complexity: O(logn)
# # Space Complexity: O (n)
def bfs(self):
pass

Expand Down