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

BTS 16/18 #62

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
113 changes: 100 additions & 13 deletions binary_search_tree/tree.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# from recursive_helpers import *

class TreeNode:
def __init__(self, key, val = None):
def __init__(self, key, val=None):
if val == None:
val = key

Expand All @@ -9,40 +11,125 @@ def __init__(self, key, val = None):
self.right = None



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

# Time Complexity:
# Space Complexity:

Choose a reason for hiding this comment

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

⏱🪐 Time and space complexity?

def add(self, key, value = None):
pass
# Space Complexity:
# iterative approach
def add(self, key, value=None):
node = TreeNode(key, value)
if self.root is None:
self.root = node
return
current = self.root

while current:
if current.key > key:
# ##go left
if current.left is None:
# ##Add node to the left
current.left = node
return
else:
current = current.left
else:
# ##go right
if current.right is None:
# ##Add the node on the right
current.right = node
return
else:
current = current.right

# Time Complexity:
# Space Complexity:
Comment on lines -22 to -23

Choose a reason for hiding this comment

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

⏱🪐 Time and 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.

pass
result = self.find_helper(key, self.root)
# catch edge case to avoid value error
if result is not None:
return result.value
return result

def find_helper(self, key, current_node):
"""

:return:
"""
if current_node is None or current_node.key == key:
return current_node
if key > current_node.key:
return self.find_helper(key, current_node.right)
return self.find_helper(key, current_node.left)

# Time Complexity:
# Space Complexity:
# Space Complexity:
Comment on lines -28 to +65

Choose a reason for hiding this comment

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

⏱🪐 Time and space complexity?

def inorder(self):

Choose a reason for hiding this comment

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

pass
"""
input: tree class
:return: list of key value tuples from traversing tree inorder
"""
inorder_result = []
# call recursive function with current node and result_list
self.inorder_helper(self.root, inorder_result)
return inorder_result

def inorder_helper(self, current_node, inorder_result):
"""
:param current_node: self.root
:param inorder_result: list
:return: populates input dictionary with key and node values from BST
"""
if current_node is None:
return

# traverse left side until current_node is none
# pass in left child node and list
self.inorder_helper(current_node.left, inorder_result)
# append key value pairs as backtrack up the stack
inorder_result.append({'key': current_node.key, 'value': current_node.value})
# traverse right
self.inorder_helper(current_node.right, inorder_result)

# Time Complexity:
# Space Complexity:
Comment on lines 93 to 94

Choose a reason for hiding this comment

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

⏱🪐 Time and space complexity?

def preorder(self):

Choose a reason for hiding this comment

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

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

def preorder_helper(self,curr_node,preorder_result):
if curr_node is None:
return
preorder_result.append({'key': curr_node.key, 'value': curr_node.value})
self.preorder_helper(curr_node.left, preorder_result)
self.preorder_helper(curr_node.right, preorder_result)


# Time Complexity:
# Space Complexity:
Comment on lines 108 to 109

Choose a reason for hiding this comment

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

⏱🪐 Time and space complexity?

def postorder(self):

Choose a reason for hiding this comment

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

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

def postorder_helper(self,curr_node,postorder_result):
if curr_node is None:
return
self.postorder_helper(curr_node.left, postorder_result)
self.postorder_helper(curr_node.right, postorder_result)
postorder_result.append({'key': curr_node.key, 'value': curr_node.value})


def height_helper(self, curr_node):
if curr_node is None:
return 0
return 1 + max(self.height_helper(curr_node.left), self.height_helper(curr_node.right))


# Time Complexity:
# Space Complexity:
Comment on lines 129 to 130

Choose a reason for hiding this comment

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

⏱🪐 Time and space complexity?

def height(self):

Choose a reason for hiding this comment

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

pass
return self.height_helper(self.root)


# # Optional Method
Expand Down
6 changes: 3 additions & 3 deletions tests/test_binary_search_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def tree_with_nodes(empty_tree) -> Tree():


def test_find_returns_none_for_empty_tree(empty_tree):
assert empty_tree.find(5) == None
assert empty_tree.find(5) is None


def test_can_find_single_root_node(empty_tree):
Expand All @@ -42,12 +42,12 @@ def test_can_find_leaf_nodes(tree_with_nodes):


def test_find_returns_none_for_values_not_in_tree(tree_with_nodes):
assert tree_with_nodes.find(6) == None
assert tree_with_nodes.find(6) is None


def test_inorder_with_empty_tree(empty_tree):
answer = empty_tree.inorder()
assert empty_tree.inorder() == []
assert answer == []


def test_inorder_with_nodes(tree_with_nodes):
Expand Down