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

Binary Search Tree Exercise #89

Open
wants to merge 2 commits into
base: main
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
60 changes: 59 additions & 1 deletion binary_search_trees/array_to_bst.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,62 @@ def arr_to_bst(arr):
Balanced Binary Search Tree using the elements in the array.
Return the root of the Binary Search Tree.
"""
pass

class BinarySearch_tree:

Choose a reason for hiding this comment

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

It's a bit funky to define a class within a function. I'm not sure that it's not allowed but I believe it does create some weird situations with Python's rules about variable/object scope. We can actually create a binary search tree even without creating a BinarySearchTree class.

Notice from the Learn lessons (and your implementation!) that the BinarySearchTree class simply bundles together a single piece of data root with some functions. The root actually is the tree, in that it's the topmost node in the tree and via the child pointers we can reach all the other nodes in the tree.

We can uncouple the root and class methods, and simply use a variable root to represent the tree. Instead of class methods where the tree is passed in via self, each of our functions would instead need to have an explicit parameter root (or similar) to pass in the tree.

def __init__(self) :
self.root = None

def insert(self, value):

Choose a reason for hiding this comment

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

You never call your insert function anywhere which is part of why many of the tests are failing. You have the logic written out, but it never gets used!

# check for duplicates
if value == self.root.value:
return
# If root is empty
mid_point = len(arr)//2
if self.root == None:
self.root = TreeNode(arr[mid_point])
Comment on lines +23 to +25

Choose a reason for hiding this comment

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

Nice! Yes, the root should be the mid_point of the array.

else:
self.insertNode(value, self.root)

# recursive function
def insertNode(self, value, current_node):
if value < current_node.value:
if current_node.left == None:
current_node.left == TreeNode(value)
# else set left to current_node
else:
self.insertNode(value, current_node.left)
# else check the right side
else:
if current_node.right == None:
current_node.right = TreeNode(value)
else:
self.insertNode(value, current_node.right)


Choose a reason for hiding this comment

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

It looks like you wrote out the logic to insert a node into a binary search tree which is great, but you're missing the logic to actually convert the array to the binary search tree.











# arr = [5, 10, 15, 20, 25, 30, 35, 40, 45]
# Finding the midpoint
# Step one -find mid-point in sorted arrray
# assign mid-point to root
# find length and integer diviide by 2
# if the % of length
# mid-point == len(arr)//2

# if len(arr)%2 == 0:
# x = len(arr) //2
# else
# y = len(arr)

# Duplicate values check
# compare value being passed in to value in current node. If equals the value in current node, return

# if tree is empty assign value to root