Java icon indicating copy to clipboard operation
Java copied to clipboard

Preorder traversal in RedBlackBST is incorrect

Open Golden-Pigeon opened this issue 3 years ago • 7 comments
trafficstars

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.

Golden-Pigeon avatar May 05 '22 08:05 Golden-Pigeon

Instead of printTreepre use printTree the error occured you assigned the wrong thing

Commanderadi avatar May 06 '22 09:05 Commanderadi

I think it should call printTreepre at the end of the function

siriak avatar May 06 '22 17:05 siriak

I think it should call printTreepre at the end of the function

Yes, that is what I mean.

Golden-Pigeon avatar May 07 '22 15:05 Golden-Pigeon

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 avatar Jul 29 '22 03:07 misgald

@misgald please make a pull request

siriak avatar Jul 29 '22 12:07 siriak

@misgald return type of method is void so just calling the printTreepre() method with left and right nodes will do the work.

Zohaib-Sathio avatar Aug 14 '22 19:08 Zohaib-Sathio

excellent

DeanV-Huang avatar Aug 25 '22 02:08 DeanV-Huang

Hey, Can I work on this?

natashasrivastava avatar Oct 09 '22 19:10 natashasrivastava

Hey, Can I work on this?

@natashasrivastava This has already been solved

Zohaib-Sathio avatar Oct 09 '22 19:10 Zohaib-Sathio

Closing as solved

siriak avatar Jan 01 '23 20:01 siriak