-
Notifications
You must be signed in to change notification settings - Fork 100
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
def __init__(self) : | ||
self.root = None | ||
|
||
def insert(self, value): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You never call your |
||
# 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! Yes, the root should be the |
||
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) | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
There was a problem hiding this comment.
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 dataroot
with some functions. Theroot
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 variableroot
to represent the tree. Instead of class methods where the tree is passed in viaself
, each of our functions would instead need to have an explicit parameterroot
(or similar) to pass in the tree.