I have a question for my binary tree code. First of all I've got 3 variables:
int node
for the number
boolean lhs = null
for the left tree
boolean rhs = null
for the right tree
I want to print out my binary tree but my code doesn't work correctly:
public String toString () {
if (lhs == null) {
} else if (rhs == null) {
return "Baum ist leer";
}
if (lhs != null) {
}
else if (rhs != null){
return rhs.toString();
}
}
This is my code, but the only output I get in my console is:
Baum ist leer
...and I don't know why, as my tree isn't empty. I can't find my mistake.
This is the code for inserting into my tree:
public void add(int insert) {
if (node > insert) {
if (lhs == null) {
lhs = new Tree(insert);
} else {
lhs.add(insert);
}
} else if (node < insert) {
if (rhs == null) {
rhs = new Tree(insert);
} else {
rhs.add(insert);
}
}
}
Your first condition isn't right correctly. What this code menans is:
if (lhs == null) {
//Code here will run if lhs equals to null, regardless the other variables values.
} else if (rhs == null) {
//Code here will run if lhs isn't null, but rhs equals null.
return "Baum ist leer";
}
According to your return, I think you try make your else if
run if both of them are null, which this code does:
if(lhs == null){
if(rhs == null){
//Code here will run only if both lhs and rhs are null.
return "Baum ist leer";
}
}
Or, more clean code:
if(lhs == null && rhs == null){
//Code here will run only if both lhs and rhs are null.
return "Baum ist leer";
}
If I misunderstood your purpose, live comment with it below.
The reason you only get "Baum ist leer" as return value, is because your function does not ever return anything else. The only time it returns a literal string is here:
return "Baum ist leer";
The only other time it returns something, it is:
return rhs.toString();
..which is a recursive call that can only result in either another recursive call or some return
that does not make a recursive call. And as you only have one such return
that does not make a recursive call, there is no way you would get any other string than "Baum ist leer".
Secondly, the function should guarantee to always return a string, so there should be no empty if
blocks.
Finally, there is no code in your toString
method that reads the data of a node, so it is impossible that this function would return a string with node data.
A side note: it is confusing that you have named the data of a tree node node
. It is most common to refer to the object as the node, and to its data as either data
or value
, or something the like. But naming it node
is confusing.
Here is an implementation of toString
that returns a multiline string, where each line represents one node, and the indentation of the node's number is an indication where it is in the tree structure. The root will have no indentation and so appears at the far left side, while the deepest leaves of the tree will appear at the far right.
// Helper
private String indentedString(Tree tree, String indent) {
if (tree == null) {
return "";
}
return indentedString(tree.rhs, indent + " ")
+ indent + tree.node + "\n"
+ indentedString(tree.lhs, indent + " ");
}
public String toString () {
return indentedString(this, "");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.