Hello i have overriden toString() method in my own class, but somehow the output is not exactly the one I wanted. Sorry for the newbie question but i cant figure out where the problem is, any hint / help is mostly appreciated. Thank you.
Myclass:
public class Country implements Comparable<Country>{
private String name;
private String capital;
private int area;
public Country(String a, String b, int c) {
this.name = a;
this.capital = b;
this.area =c;
}
@Override
public String toString(){
return(this.name + " "+ this.capital+" " + this.area);
}
}
DS:
private void preorder(BinaryNode <type> a){
if (a != null){
System.out.println(a.toString());
preorder(a.left);
preorder(a.right );
}
}
App:
BinarySearchTree <Country> db = new BinarySearchTree<Country>();
Country ob = new Country("Romania", "Buc", 123);
db.addNewElement(ob);
ob = new Country("Hungaria", "Bud", 50);
db.addNewElement(ob);
ob = new Country("Vatican", "Vat", 1);
db.addNewElement(ob);
db.printAll();
output:
adt.BinaryNode@1e5e2c3
adt.BinaryNode@18a992f
adt.BinaryNode@4f1d0d
EDIT: fix after “chaitanya10” hint for msitake
DS:
private void preorder(BinaryNode <type> a){
if (a != null){
System.out.println(a.elm.toString()); // ACCES the data in node not the hole node.
preorder(a.left);
preorder(a.right );
}
}
your method takes
BinaryNode<type>as anargument, you are callingtoStringonbrinaryNode<type>notCOuntry. you haveoverriden toString()inCountrynotBinaryTree.change it to
OR
override toString() in BinaryNode.