Can anyone help me with this private method, I dont know which part is wrong. pu
ID: 3757466 • Letter: C
Question
Can anyone help me with this private method, I dont know which part is wrong.
public class BinaryTree {
//Implements a Binary Tree of Strings
private class Node {
private Node left;
private String data;
private Node right;
private Node parent; // reference to the parent node
// the parent is null for the root node
private Node(Node L, String d, Node r, Node p) {
left = L;
data = d;
right = r;
parent = p;
}
}
private Node root;
public String toString() {
// returns the string representation of the tree using the in order format
// discussed in class. If the tree was created from a string use the
// the values of open, close and empty given to the constructor otherwise
// use (, ) and ! for open, close and empty respectively
// most of the work should be done in a recursive private method.
StringBuffer buffer = new StringBuffer();
return toString(buffer, root);
}
private String toString(StringBuffer buffer, Node r) {
if (r == null) {
buffer.append("!");
return toString(buffer, r);
}
buffer.append("(");
if (r != null) {
buffer.append("(");
toString(buffer, r.left);
buffer.append(r.data);
toString(buffer, r.right);
buffer.append(")");
}
buffer.append(")");
return buffer.toString();
}
}
Explanation / Answer
You may use the above methods, it should work fine now. As i do not have the code to test, i can not give you a running screenshot, but it should work fine.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.