For my project at university, I am implementing an AVL tree class in C++ with characters for values. We have an autograder which points out our errors with random tests, if present. I ran it multiple times, quite a lot, and got no errors, which means it should have OK logic. However, sometimes I got an error stating that my preorder and postorder traversals are wrong. This is one of the actual errors I got:
YOUR PREORDER: "IECABDGFHPLKOUSRYX", YOUR POSTORDER: "BADCFHGEKOLRSXYUPI"
CORRECT PREORDER: "ECABDOIGFHKLURPSYX", CORRECT POSTORDER: "BADCFHGLKIPSRXYUOE"
Yes, these trees are still balanced, but somehow incorrectly put together. How should I go about debugging this? I have a function which can reconstruct trees using preorder and postorder, but I think the order of putting variables in matters. Otherwise, I have no way of knowing the order of node input and where my algorithm went wrong.
Consider writing your own test engine, that picks a random string, runs it through your algorithm and gets the preorder it spits out, sorts the random string in preorder and compares the two.
if they don't match, it prints the random string and the two orders. That should help you find failing preorder test cases.
then add post order to the random tester, and run it some more. You might be able to reuse this for future projects.
Google Fuzzer for other ideas :)