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

Pine ~ Alma Becerril Salas #43

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
139 changes: 112 additions & 27 deletions binary_search_tree/tree.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
from logging import root
from multiprocessing.sharedctypes import Value
from unittest import result

Choose a reason for hiding this comment

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

It looks like none of these import statements got used. My guess is that they were added by your IDE when something was autocompleted, but it's good practice to remove them if you don't end up using them to just keep your code clean.



class TreeNode:
def __init__(self, key, val = None):
if val == None:
Expand All @@ -14,46 +19,126 @@ class Tree:
def __init__(self):
self.root = None

# Time Complexity:
# Space Complexity:
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

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 28 and 29.

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!


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

Choose a reason for hiding this comment

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

We don't need to go through every node in the tree as would be implied by O(n), but instead follow the tree down however many levels it has. In the case of a balanced tree (a reasonable assumption), we will have to only go down ceiling(log_2(n)) levels, thus having O(log(n)) for both space and time complexity.

Of course, if the tree is not balanced, your calculation is correct, as the worst case, every item is on its own level and thus O(n).

def add(self, key, value = None):
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)

Choose a reason for hiding this comment

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

Much like as discussed for adding nodes, we don't have to go through each node to find one, but only O(log(n)) nodes. Your answer of O(n) does apply in the worst-case unbalanced tree, though.

As far as space complexity, as no new space is allocated, this is O(1).

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

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

# Time Complexity:
# Space Complexity:

# Time Complexity: O(n)
# Space Complexity: O(n)
def inorder(self):
pass
if self.root == None:
return []
arr = []
self.inorder_helper(self.root, arr)
return arr

def inorder_helper(self, node, arr):
if node is not None:
self.inorder_helper(node.left, arr)
arr.append({"key": node.key, "value": node.value})
self.inorder_helper(node.right, arr)


# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(n)
def preorder(self):
pass
result = []
def preorder_helper(current_node):
if current_node == None:
return result
result.append({"key": current_node.key, "value": current_node.value})
preorder_helper(current_node.left)
preorder_helper(current_node.right)
preorder_helper(self.root)
return result

# Time Complexity:
# Space Complexity:

# Time Complexity: O(n)
# Space Complexity: O(n)
def postorder(self):
pass
result = []
def postorder_helper(current_node):
if current_node == None:
return result
postorder_helper(current_node.left)
postorder_helper(current_node.right)
result.append({"key": current_node.key, "value": current_node.value})
postorder_helper(self.root)
return result

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(n)
def height(self):
pass
def height_helper(current_node):
if current_node == None:
return 0
left_height = height_helper(current_node.left)
right_height = height_helper(current_node.right)
return max(left_height, right_height) + 1
return height_helper(self.root)


# # Optional Method
# # Time Complexity:
# # Space Complexity:
def bfs(self):
pass
# Optional Method
# Time Complexity:
# Space Complexity:
# def bfs(self):
# result = []
# queue = []
# queue.append(self.root)
# while len(queue) > 0:
# current = queue.pop(0)
# result.append(current.key)
# if current.left != None:
# queue.append(current.left)
# if current.right != None:
# queue.append(current.right)
# return result


# *********notes from the video*********
# if you have a very unbalanced tree, the time complexity is going to be O(n)
# if you have a balanced tree, the time complexity is going to be O(log n). much better.
# red-black tree is a balanced tree.
# a traversal is when you visit every node in a tree.
# depth first traversals are: pre-order, in-order, post-order
# pre-order: visit the root, then the left subtree, then the right subtree. if you need to save a tree data structure to disk, of sent it across the network and maintain the structure, pre-order traversals can be useful.
# in-order: visit the left subtree, then the root, then the right subtree. if you need to sort a list, in-order traversals are useful.
# post-order: visit the left subtree, then the right subtree, then the root. if you need to delete a node from a tree, post-order traversals are useful.
# binary search tree is an ordered data structure in which the every node in the left subtree is "less" than or equal to the current nocde and every node in the right subtree is greater than the current node.
# a balanced binary search tree provides O(log n) time complexity for adding, removing and finding values in the tree.
# an unbalanced tree more resenbles a linked list in time complexity for find and arbitraty insertions and deletions O(n)
# a traversal is an algorithm which visits every node in a tree.
# examples include: breadth first search, inorder, postorder, preorder.
# ****************************************


# # Useful for printing
def to_s(self):
return f"{self.inorder()}"
68 changes: 34 additions & 34 deletions tests/test_binary_search_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,37 +203,37 @@ def test_will_report_height_of_unbalanced_tree():
assert unbalanced_tree.height() == 5


def test_bfs_with_empty_tree(empty_tree):
assert empty_tree.bfs() == []


def test_bfs_with_tree_with_nodes(tree_with_nodes):
expected_answer = [
{
"key": 5,
"value": "Peter"
},
{
"key": 3,
"value": "Paul"
},
{
"key": 10,
"value": "Karla"
},
{
"key": 1,
"value": "Mary"
},
{
"key": 15,
"value": "Ada"
},
{
"key": 25,
"value": "Kari"
}
]

answer = tree_with_nodes.bfs()
assert answer == expected_answer
# def test_bfs_with_empty_tree(empty_tree):
# assert empty_tree.bfs() == []


# def test_bfs_with_tree_with_nodes(tree_with_nodes):
# expected_answer = [
# {
# "key": 5,
# "value": "Peter"
# },
# {
# "key": 3,
# "value": "Paul"
# },
# {
# "key": 10,
# "value": "Karla"
# },
# {
# "key": 1,
# "value": "Mary"
# },
# {
# "key": 15,
# "value": "Ada"
# },
# {
# "key": 25,
# "value": "Kari"
# }
# ]

# answer = tree_with_nodes.bfs()
# assert answer == expected_answer