#Adding to a BST
1 messages ยท Page 1 of 1 (latest)
First of all it's a bit weird that addHelper is a function in your BinarySearchTree class. It would be much more ideological to have another add function in Node that would basically do what addHelper does, but cleaner.
yeah i was thinking the same thing
but i think he uses an autograder for these
so i dont want to mess with things outside the problem description
But yeah, since it's an assignment you don't have a choice there I guess
the logic seems sound though, right?
theres a second part where we write a main method for it
i did this
Detected code, here are some useful tools:
and it seemed fine
yeah, logic looks "good".
as long as it works im ok with it ๐ญ
Nonetheless, here's how I would write it:
id like to see that yeah
public class BinarySearchTree<T extends Comparable<T>> {
private class Node {
private final T key;
private Node left;
private Node right;
public Node(T key) {
this.key = key;
this.left = null;
this.right = null;
}
public boolean add(T key) {
if (key.compareTo(this.key) == 0) {
// already exists - you don't have to respect that case
return false;
}
if (key.compareTo(this.key) < 0) {
// insert into the left
if (left == null) { left = new Node(key); return true; }
return left.add(key);
} else {
// insertion into the right subtree
if (right == null) { right = new Node(key); return true; }
return right.add(key);
}
}
}
public BinarySearchTree() {
}
private Node root;
public boolean add(T key) {
if (root == null) {
root = new Node(key);
return true;
}
return root.add(key);
}
}
Detected code, here are some useful tools:
@buoyant tulip I switched a few things around, like e.g. now the function returns boolean instead of Node (which is a private class which means you probably wouldn't want to expose it). Also you just don't need to return the node, because you don't need it for your logic and it would always just return the same root because you never care about rebalancing either.
Besides that I really didn't switch a whole lot, tried to keep it as close to the original as possible (also there's just not that much you you can do with binary trees).
@buoyant tulip
Your question has been closed due to inactivity.
If it was not resolved yet, feel free to just post a message below
to reopen it, or create a new thread.
Note that usually the reason for nobody calling back is that your
question may have been not well asked and hence no one felt confident
enough answering.
When you reopen the thread, try to use your time to improve the quality
of the question by elaborating, providing details, context, all relevant code
snippets, any errors you are getting, concrete examples and perhaps also some
screenshots. Share your attempt, explain the expected results and compare
them to the current results.
Also try to make the information easily accessible by sharing code
or assignment descriptions directly on Discord, not behind a link or
PDF-file; provide some guidance for long code snippets and ensure
the code is well formatted and has syntax highlighting. Kindly read through
https://stackoverflow.com/help/how-to-ask for more.
With enough info, someone knows the answer for sure ๐
oh I see I see
yeah that makes sense
thank you! ๐
@buoyant tulip
Your question has been closed due to inactivity.
If it was not resolved yet, feel free to just post a message below
to reopen it, or create a new thread.
Note that usually the reason for nobody calling back is that your
question may have been not well asked and hence no one felt confident
enough answering.
When you reopen the thread, try to use your time to improve the quality
of the question by elaborating, providing details, context, all relevant code
snippets, any errors you are getting, concrete examples and perhaps also some
screenshots. Share your attempt, explain the expected results and compare
them to the current results.
Also try to make the information easily accessible by sharing code
or assignment descriptions directly on Discord, not behind a link or
PDF-file; provide some guidance for long code snippets and ensure
the code is well formatted and has syntax highlighting. Kindly read through
https://stackoverflow.com/help/how-to-ask for more.
With enough info, someone knows the answer for sure ๐