I am trying to print my AVL tree in the following way:
(((10) 20 (30)) 40 (50))
Where 20 is the parent of 10 & 30. 40 is the root and the parent of 20 and 50.
Here are my current methods for printing the tree.
public void printTree(){
if(isEmpty()){
System.out.println("Empty tree");
}else{
printTree(root);
}
}
private void printTree(Node t){
if(t != null){
printTree(t.left);
System.out.println(t.element + " ")
printTree(t.right);
}
}
The tree currently prints as follows:
10 20 30 40 50
As Rohit Jain commented, just add them in the printTree method:
private void printTree(Node t){
if(t != null){
System.out.print("(");
printTree(t.left);
System.out.print(t.element + " ")
printTree(t.right);
System.out.print(")");
}
}
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.