As per Wikipedia, Algorithm for In-order Traversal of Binary Tree
- If the current node is empty/NULL/None return nothing.
- Traverse the left subtree by recursively calling the in-order function.
- Display the data part of the root (or current node).
- Traverse the right subtree by recursively calling the in-order function.
I was interested in Algorithm for In-order Traversal of Ternary Tree. Upon referring Professor Robert Sedgewick's Lecture of Ternary Search Tries, I found that if I do the In-order traversal on the Ternary Search Tree (a type of Trie data structure) for a particular searched string then visiting order of nodes should be
- Check if the current node is empty or null.
- Traverse the left subtree recursively calling in-order function.
- Display the data part of the current node.
- Traverse the middle subtree by recursively calling the in-order function.
- Traverse the right subtree by recursively calling the in-order function.
But I got result different from claimed in one Assignment Problem, and in one Competitive Exam Problem.
Problem 1 : Find the In-order Traversal of the following tree
- Mine Answer :
AKBJCLIEDHFG
- Given Answer :
AKBJCLIDEHFG
Problem 2 : Consider the rooted tree with the vertex labelled P
as Root. Find the order in which nodes are visited during an in-order traversal of the tree.
- Mine Answer :
QSPTRUWV
- Given Answer :
SQPTRWUV
Please verify whether my answers are correct or not and If I am wrong somewhere then please correct me.