Problem

(Level-Order Binary Tree Traversal) The program of Figs.19.20–19.22 illustrated three recu...

(Level-Order Binary Tree Traversal) The program of Figs.19.20–19.22 illustrated three recursive methods of traversing a binary tree—inorder, preorder and postorder traversals. This exercise presents the level-order traversal of a binary tree, in which the node values are printed level by level, starting at the root node level. The nodes on each level are printed from left to right. The level-order traversal is not a recursive algorithm. It uses a queue object to control the output of the nodes. The algorithm is as follows:

1) Insert the root node in the queue


2) While there are nodes left in the queue,


Get the next node in the queue

Print the node’s value

If the pointer to the left child of the node is not nullptr

Insert the left child node in the queue

If the pointer to the right child of the node is not nullptr

Insert the right child node in the queue.

Write member function levelOrder to perform a level-order traversal of a binary tree object. Modify the program of Figs. 19.20–19.22 to use this function. [Note: You’ll also need to modify and incorporate the queue-processing functions of Fig.19.16 in this program.]

Fig. 19.20 Creating and traversing a binary tree. (Part 1 of 3.)

Fig. 19.20 Creating and traversing a binary tree. (Part 2 of 3.)

Fig. 19.20 Creating and traversing a binary tree. (Part 3 of 3.)

Fig.19.21 TreeNode class-template definition. (Part 1 of 2.)

Fig. 19.21 TreeNode class-template definition. (Part 2 of 2.)

Fig. 19.22 Tree class-template definition. (Part 1 of 3.)

Fig. 19.22 Tree class-template definition. (Part 2 of 3.)

Fig. 19.22 Tree class-template definition. (Part 3 of 3.)

Fig.19.16 Queue class-template definition. (Part 1 of 2.)

Fig. 19.16 Queue class-template definition. (Part 2 of 2.)

Step-by-Step Solution

Request Professional Solution

Request Solution!

We need at least 10 more requests to produce the solution.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the solution will be notified once they are available.
Add your Solution
Textbook Solutions and Answers Search
Solutions For Problems in Chapter 20
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT