Question: 2 4 6 == 1. Consider the following code fragment that allows you to visit every node in a binary tree using an iterative routine.

 2 4 6 == 1. Consider the following code fragment that

2 4 6 == 1. Consider the following code fragment that allows you to visit every node in a binary tree using an iterative routine. 1 void traversal (Tnode *node) { 3 Lnode *stack - NULL; // empty stack do { 5 while (node != NULL) { Push (&stack, node); 7 node = node ->left; 8 } 9 while ((! Stack_empty (stack)) && (node (Stack_top(stack )) ->right)) { 10 node = Pop (&stack); 11 } 12 if (! Stack_empty (stack)) { 13 node = (Stack_top(stack)) ->right; 14 } 15 } while (!Stack_empty (stack)); 16 } Here, we assume that a tree node (Tnode)has three fields, left, right, and key. The key field stores an int, and the left and right fields store the addresses of the left and right child nodes. We use stack to maintain a stack of tree nodes. In this piece of code, we use a linked list (Lnode) to implement a stack. Push adds (Pop removes) a tree node to (from) the stack. Empty-stack returns TRUE if the stack is empty; FALSE otherwise. Stack_top returns the top most tree node on the stack without modifying the stack. (a) Without deleting any statements from the preceeding code fragment, insert necessary state- ment(s) so that you can perform a preorder printing of all nodes in the binary tree. For example, your answer can be in the form of "insert between line 3 and line 4 the statement fprintf(stdout, "%d ", node->key);" (b) Without deleting any statements from the preceeding code fragment, insert necessary state- ment(s) so that you can perform a postorder printing of all nodes in the binary tree. (e) Without deleting any statements from the preceeding code fragment, insert necessary state- ment(s) so that you can perform a inorder printing of all nodes in the binary tree. 2 2 4 6 == 1. Consider the following code fragment that allows you to visit every node in a binary tree using an iterative routine. 1 void traversal (Tnode *node) { 3 Lnode *stack - NULL; // empty stack do { 5 while (node != NULL) { Push (&stack, node); 7 node = node ->left; 8 } 9 while ((! Stack_empty (stack)) && (node (Stack_top(stack )) ->right)) { 10 node = Pop (&stack); 11 } 12 if (! Stack_empty (stack)) { 13 node = (Stack_top(stack)) ->right; 14 } 15 } while (!Stack_empty (stack)); 16 } Here, we assume that a tree node (Tnode)has three fields, left, right, and key. The key field stores an int, and the left and right fields store the addresses of the left and right child nodes. We use stack to maintain a stack of tree nodes. In this piece of code, we use a linked list (Lnode) to implement a stack. Push adds (Pop removes) a tree node to (from) the stack. Empty-stack returns TRUE if the stack is empty; FALSE otherwise. Stack_top returns the top most tree node on the stack without modifying the stack. (a) Without deleting any statements from the preceeding code fragment, insert necessary state- ment(s) so that you can perform a preorder printing of all nodes in the binary tree. For example, your answer can be in the form of "insert between line 3 and line 4 the statement fprintf(stdout, "%d ", node->key);" (b) Without deleting any statements from the preceeding code fragment, insert necessary state- ment(s) so that you can perform a postorder printing of all nodes in the binary tree. (e) Without deleting any statements from the preceeding code fragment, insert necessary state- ment(s) so that you can perform a inorder printing of all nodes in the binary tree. 2

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!