Skip to content

completed binary tree task #40

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

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
157 changes: 139 additions & 18 deletions binary_search_tree/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,156 @@ class Tree:
def __init__(self):
self.root = None

# Time Complexity:
# Space Complexity:
# Time Complexity: O(log n)
# Space Complexity: O(1)

def add_helper(self, current_root, new_node):
if current_root is None:
return new_node

if new_node.key < current_root.key:
current_root.left = self.add_helper(current_root.left, new_node)

else:
current_root.right = self.add_helper(current_root.right, new_node)
return current_root

Choose a reason for hiding this comment

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

This implementation of add_helper works, but definitely does a bit of extra work in setting nodes. Essentially, every node on the path to where the new node ends up will have either its left or right node "updated", though most of these will be updated to the exact same node. The only one that gets a meaningful update is whenever the bottom of the tree is reached and gets its left/right node updated to the new node. These updates happen in lines 25 and 28.

This probably will not be too bad of a performance hit, but there is a way that you can do this and just update only the node you need.

But this overall fine enough!


def add(self, key, value = None):
pass

# Time Complexity:
# Space Complexity:
if self.root is None:
self.root = TreeNode(key,value)
return self.root

new_node =TreeNode(key,value)
self.add_helper(self.root, new_node)


# Time Complexity: O(logn)
# Space Complexity: O(1)

def find(self, key):
pass
if self.root is None:
return None
current = self.root

while current:
if key < current.key:
# go left

current = current.left # traversing left
elif key > current.key:

current = current.right # traversing right. key is the integer and value string

else:
return current.value
return None

# Time Complexity: O(n)
# Space Complexity: O(n)

def inorder_helper(self, current_node,inorder_list):

if current_node is not None:

# traversing left
self.inorder_helper(current_node.left, inorder_list)
# add current node to preorder_list
inorder_list.append({"key": current_node.key, "value": current_node.value})
#traversing right
self.inorder_helper(current_node.right, inorder_list)



# Time Complexity:
# Space Complexity:
def inorder(self):
pass
inorder_list = []
if self.root is None:
return inorder_list


self.inorder_helper(self.root, inorder_list)

return inorder_list
# inorder = self.root
# inorder(self.root.left)

# inorder(self.root.right)

Choose a reason for hiding this comment

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

Looks like there's some commented code here. It's not a big deal in this case, but usually as a style thing, I recommend getting rid of commented code just to keep the code cleaner in general.



# Time Complexity: O(n)
# Space Complexity: O(n)

def preorder_helper(self,current_node, preorder_list):

if current_node is not None:

#append current node to preorder_list
preorder_list.append({"key": current_node.key, "value": current_node.value})

#traverse left
self.preorder_helper(current_node.left, preorder_list)


#traverse right
self.preorder_helper(current_node.right, preorder_list)




# Time Complexity:
# Space Complexity:
def preorder(self):
pass
preorder_list = []
if self.root is None:
return preorder_list

self.preorder_helper(self.root, preorder_list)

return preorder_list


#Time Complexity: O(n)
#Space Complexity: O(n)

def postorder_helper(self, current_node, postorder_list):

if current_node is not None:

# traversing left
self.postorder_helper(current_node.left, postorder_list)

#traversing right
self.postorder_helper(current_node.right, postorder_list)

postorder_list.append({"key": current_node.key, "value": current_node.value})


# Time Complexity:
# Space Complexity:
def postorder(self):
pass
postorder_list = []
if self.root is None:
return postorder_list

#call helper method
self.postorder_helper(self.root, postorder_list)

return postorder_list

# Time Complexity: O(n)
# Space Complexity: O(n)
def height_helper(self,current_node):

if current_node is not None:
height_left= self.height_helper(current_node.left)
height_right = self.height_helper(current_node.right)

else:
return 0

return (max(height_left,height_right) +1)


# Time Complexity:
# Space Complexity:
def height(self):
pass
return self.height_helper(self.root)




# # Optional Method
Expand Down