Preorder: / * + a b – c d + e f
Inorder: a + b * c – d / e + f
Postorder: a b + c d - * e f + /
Say we have this tree. I already got the code for printing out the orders; Preorder, Inorder and Postorder. Now whats left is the problem, the user is asked to input Preorder next to what character?, lets say the user entered the letter a, so the output should be
Preorder next to what character?: a
Preordernext(a) = b
and so on to Inorder and Postorder, now the problem is i cant seem to locate the next node from a specific node.
System.out.print("Preorder next of what character: ");
char q = scn.next().charAt(0);
System.out.print("PreorderNext("+q+")| ");
binaryTree.searchPostOrder(q);
So far this is what i came up with and I received no result aside from blank...
public void searchPostOrder(char kwanix) {
searchPostorder(root, kwanix);
}
private char searchPostorder(Node node, char kwanix) {
if (node.getData() == kwanix) {
if (node.getLeft() != null) {
if (node.getLeft().equals(kwanix)) {
kwanix = node.getData();
}
}
if (node.getRight() != null) {
if (node.getRight().equals(kwanix)) {
kwanix = node.getData();
}
}
}
return kwanix;
}
This is the result after running the program
Post Order| d c b a
Pre order| a c d b
In order| c d a b
Preorder next of what character: a
PreorderNext(a)|