Need Help imlementing these java methods for a BST tree that conisists of all st
ID: 3592680 • Letter: N
Question
Need Help imlementing these java methods for a BST tree that conisists of all strings. Cannot change the Method signatures.
Belows is my Node class for asssistance:
public class Node {
private String value;
private Node left, right;
public Node() {
this.value = null;
this.left = null;
this.right = null;
}
public Node(String s) {
this.value = s;
this.left = null;
this.right = null;
}
public Node(String s, Node l, Node r) {
this.value = s;
this.left = l;
this.right = r;
}
public String getValue() {
return this.value;
}
public void setValue(String s) {
this.value = s;
}
public boolean hasLeft() {
if (left.equals(null))
return false;
else
return true;
}
public boolean hasRight() {
if (right.equals(null))
return false;
else
return true;
}
public Node getLeft() {
return this.left;
}
public String getLeftValue() {
return this.left.getValue();
}
public Node getRight() {
return this.right;
}
public String getRightValue() {
return this.right.getValue();
}
public void setLeft(Node node) {
this.left = node;
}
public void setLeftValue(String s) {
this.left.setValue(s);
}
public void setRight(Node node) {
this.right = node;
}
public void setRightValue(String s) {
this.right.setValue(s);
}
}
Below are the methods I need help with implementing, recursive helper methods may need to be implemented if need be.
/*
* Returns true if s appears in the tree; otherwise returns false.
*/
public boolean search(String s) {
return false;
}
/*
* Returns the number of times s appears in the tree.
*/
public int frequency() {
return 0;
}
/*
* If s appears in the tree, then removes the string s from the tree and returns
* true. If s does not appear in the tree, then returns false. If s appears more than once, then
* removes only one occurrence.
*/
public boolean remove(String s) {
return false;
}
Explanation / Answer
public class Node {
private String value;
private Node left, right;
public Node() {
this.value = null;
this.left = null;
this.right = null;
}
public Node(String s) {
this.value = s;
this.left = null;
this.right = null;
}
public Node(String s, Node l, Node r) {
this.value = s;
this.left = l;
this.right = r;
}
public String getValue() {
return this.value;
}
public void setValue(String s) {
this.value = s;
}
public boolean hasLeft() {
if (left.equals(null))
return false;
else
return true;
}
public boolean hasRight() {
if (right.equals(null))
return false;
else
return true;
}
public Node getLeft() {
return this.left;
}
public String getLeftValue() {
return this.left.getValue();
}
public Node getRight() {
return this.right;
}
public String getRightValue() {
return this.right.getValue();
}
public void setLeft(Node node) {
this.left = node;
}
public void setLeftValue(String s) {
this.left.setValue(s);
}
public void setRight(Node node) {
this.right = node;
}
public void setRightValue(String s) {
this.right.setValue(s);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.