Problem

Writing programs that solve the Programming Projects helps to solidify your under­standing...

Writing programs that solve the Programming Projects helps to solidify your under­standing of the material and demonstrates how the chapter’s concepts are applied. (As noted in the Introduction, qualified instructors may obtain completed solutions to the Programming Projects on the publisher’s Web site.)

In Chapter 8, “Binary Trees,” we’ll look at binary trees, where every branch has (potentially) exactly two sub-branches. If we draw a binary tree on the screen using characters, we might have 1 branch on the top row, 2 on the next row, then 4, 8, 16, and so on. Here’s what that looks like for a tree 16 characters wide:

(Note that the bottom line should be shifted a half character-width right, but there’s nothing we can do about that with character-mode graphics.) You can draw this tree using a recursive makeBranches () method with arguments left and right, which are the endpoints of a horizontal range. When you first enter the routine, left is 0 and right is the number of characters (including dashes) in all the lines, minus 1. You draw an X in the center of this range. Then the method calls itself twice: once for the left half of the range and once for the right half. Return when the range gets too small. You will probably want to put all the dashes and Xs into an array and display the array all at once, perhaps with a display () method. Write a main () program to draw the tree by calling makeBranches () and display (). Allow main () to determine the line length of the display (32, 64, or whatever). Ensure that the array that holds the characters for display is no larger than it needs to be. What is the relationship of the number of lines (five in the picture here) to the line width?

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 6
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