Java
Java copied to clipboard
Preorder traversal in RedBlackBST is incorrect
In the class com.thealgorithms.datastructures.trees.RedBlackBST, the preorder traversal is
public void printTreepre(Node node) {
if (node == nil) {
return;
}
System.out.print(
((node.color == R) ? " R " : " B ") + "Key: " + node.key + " Parent: " + node.p.key + "\n");
printTree(node.left);
printTree(node.right);
}
Should the last two lines call printTreepre rather than printTree?
printTree is an inorder traversal method.
Instead of printTreepre use printTree the error occured you assigned the wrong thing
I think it should call printTreepre at the end of the function
I think it should call printTreepre at the end of the function
Yes, that is what I mean.
public void printTreepre(Node node) { if (node == nil) { return; } System.out.print( ((node.color == R) ? " R " : " B ") + "Key: " + node.key + " Parent: " + node.p.key + "\n"); return printTreepre(node.left)+ printTreepree(node.right);
}
@misgald please make a pull request
@misgald return type of method is void so just calling the printTreepre() method with left and right nodes will do the work.
excellent
Hey, Can I work on this?
Hey, Can I work on this?
@natashasrivastava This has already been solved
Closing as solved