52.7k views
4 votes
Examine the following Print function and determine which traversal it is:

void Print(TreeNode* tree) {
if (tree == NULL) {
return;
}
Print(tree->right);
tree->info.PrintItem();
Print(tree->left);
}

A) Inorder
B) Preorder
C) Postorder
D) By level

1 Answer

5 votes

Final answer:

The Print function provided is performing a traversal in the order of right, node, left, which does not match any standard traversal but is a reverse of an A. inorder traversal.

Step-by-step explanation:

The Print function shown in the question is a type of tree traversal function. The order in which the nodes of the tree are accessed and printed determines the type of traversal. In this case, the function first calls itself to traverse the right subtree, then it processes the current node (the 'info.PrintItem()' part), and finally, it calls itself to traverse the left subtree. This pattern indicates that the traversal is in reverse of the typical inorder traversal, which is normally left, node, right. Instead, it is right, node, left.

The given Print function is an example of inorder traversal because it traverses the left subtree, processes the current node, and then traverses the right subtree. In the given function, the recursive calls to Print for the left child and right child are made before and after printing the current node's information, respectively.

User Hooman
by
8.6k points