Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

weiss 4.53 Generate an index for a book. The input file consists of a set of ind

ID: 3654818 • Letter: W

Question

weiss 4.53 Generate an index for a book. The input file consists of a set of index entries. Each line consists of the string IX:, followed by an index entry name enclosed in braces, followed by a page number that is enclosed in braces. Each ! in an index entry name represents a sub-level. A (|represents the start of a range, and a |) represents the end of the range. Occasionally, this range will be the same page. In that case, output only a single page number. Otherwise, do not collapse or expand ranges on your own. As an example, Figure 4.75 shows sample input, and Figure 4.76 shows the corresponding output.

Explanation / Answer

Each unique index entry will be represented as an IndexEntry. These IndexEntry will be stored in a binary search tree. An inorder traversal of the binary search tree will output the index in alphabetical order. The IndexEntry contains a string and a pointer to a queue that stores information about the page numbers. This problem is a slightly more involved variation of the cross-reference generator discussed in Section 12.2 of the text. Here, each entry in the queue is not an int, but rather is an entity that stores a page number and an indication of whether the page number starts a range, finishes a range, or is on its own. Also, the comparison functions that are defined by IndexEntry should ignore case distinctions so that the sort is case-independent. You may assume that every line of the input is properly formatted. You must verify that |( and |) are properly matched, and print a warning message if they are not. Any strange-looking characters on the line can be accepted as part of the index entry (they are likely text-formatting commands). The code for the cross-reference generator uses an Inorder tree iterator to print the index in sorted order. Simplify the routine by making the IndexEntry aware of how to perform its own output. In other words, write the following function, and have it called as each entry in the search tree is visited by the iterator: // Print an index entry: name, followed by line numbers ostream & operator