The program of Figs. 21.15 and 21.16 illustrated three recursive methods of traversing a binary treeinorder, preorder

Question:

The program of Figs. 21.15 and 21.16 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 shown in Fig. 21.22. Write method levelOrder to perform a level-order traversal of a binary tree object. Modify the program of Figs. 21.15 and 21.16 to use this method.

Fig. 21.22

I 2 3 4 5 6 7 8 9 10 II Insert the root node in the queue. While there are nodes left in the queue, do the

Fig. 21.15I // Fig. 21.15: Tree.java 2 // TreeNode and Tree class declarations for a binary search tree. 3 package

Fig. 21.16

I // Fig. 21.16: TreeTest.java 2 // Binary tree test program. 3 import java.security.SecureRandom; import

Fantastic news! We've Found the answer you've been seeking!

Step by Step Answer:

Related Book For  book-img-for-question

Java How To Program Late Objects Version

ISBN: 9780136123712

8th Edition

Authors: Paul Deitel, Deitel & Associates

Question Posted: