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

Emily Saeli - C-17 Whales #86

Open
wants to merge 1 commit into
base: main
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
44 changes: 43 additions & 1 deletion binary_search_trees/array_to_bst.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,51 @@ def __init__(self, value, left = None, right = None):
self.right = right



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

# def add_helper(self, current_node, new_node):
# if new_node.val < current_node.val:
# if not current_node.left:
# current_node.left = new_node
# return
# else:
# self.add_helper(current_node.left, new_node)
# elif new_node.val >= current_node.val:
# if not current_node.right:
# current_node.right = new_node
# return
# else:
# self.add_helper(current_node.right, new_node)

# def add(self, val):
# new_node = TreeNode(val)
# if not self.root:
# self.root = new_node
# else:
# self.add_helper(self.root, new_node)

def arr_to_bst(arr):
""" Given a sorted array, write a function to create a
Balanced Binary Search Tree using the elements in the array.
Return the root of the Binary Search Tree.
"""
pass
if not arr:
return None
elif len(arr) == 1:
return TreeNode(arr[0])
else:
if len(arr)% 2 == 0:
median_index = int(len(arr)/2)
median = arr[median_index]
else:
median_index = int((len(arr)-1)/2)
median = arr[median_index]
arr_left = arr[0:median_index]
arr_right = arr[median_index+1:]
median_node = TreeNode(median)
median_node.left = arr_to_bst(arr_left)
median_node.right = arr_to_bst(arr_right)
return median_node
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ packaging==20.8
pluggy==0.13.1
py==1.10.0
pyparsing==2.4.7
pytest==6.2.1
pytest==7.2.0
toml==0.10.2
Loading