I understand the idea behind the tree traversal as well as implementations, but here is the question. Why do we need them all?
Right now I only know that preorder traversal is used when parsing math expressions. From the Wikipedia I also read than:
But these examples are rather vague. Can anyone describe this in more depth. Especially with examples.
Consider the problem of doing some file operation on a directory tree. When the operation is removal of files, then you need to empty each directory before removing the directory itself, so you need a post-order traversal. By contrast, when copying the hierarchy, you need to copy the directories first, so then you need a pre-order traversal.
I honestly don't see what's vague about the BST in-order traversal. When you want to display the contents of a BST on-screen in a user interface, you want the keys to show up sorted, don't you? (If you didn't, then using a BST would probably be a bad idea since a hash table is often faster.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With