-
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?
Conversation
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.
This is a great first attempt Theresa, but it looks like you may have strayed from the original problem a bit. I left some comments below to try and help redirect you. Please reach out (even after graduation) if you have questions or would like to go over this further.
|
||
class BinarySearch_tree: |
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 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.
self.root = None | ||
|
||
def insert(self, value): |
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.
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!
if self.root == None: | ||
self.root = TreeNode(arr[mid_point]) |
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.
Nice! Yes, the root should be the mid_point
of the array.
self.insertNode(value, current_node.right) | ||
|
||
|
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 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.
No description provided.