Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Accounting And Auditing Research And Databases Practitioner's Desk Reference

Authors: Thomas R. Weirich, Natalie Tatiana Churyk, Thomas C. Pearson

1st Edition

1118334426, 978-1118334423

More Books

Students also viewed these Databases questions

Question

3. Would you say that effective teamwork saved their lives?

Answered: 1 week ago