To implement the preorder method of the AbstractTree class, we relied on the convenience of creating a
Question:
To implement the preorder method of the AbstractTree class, we relied on the convenience of creating a snapshot. Reimplement a preorder method that creates a lazy iterator. (See Section 7.4.2 for discussion of iterators.)
Section 7.4.2
?
Transcribed Image Text:
/- /** * A (nonstatic) inner class. Note well that each instance contains an implicit * reference to the containing list, allowing it to access the list's members. nested Arraylterator class 3. 4 private class Arraylterator implements Iterator
/- /** * A (nonstatic) inner class. Note well that each instance contains an implicit * reference to the containing list, allowing it to access the list's members. nested Arraylterator class 3. 4 private class Arraylterator implements Iterator { private int j = 0; private boolean removable = false; // can remove be called at this time? 6 // index of the next element to report 7 8. /** Tests whether the iterator has a next object. * @return true if there are further objects, false otherwise */ public boolean hasNext() { return j < size; } // size is field of outer instance 10 11 12 13 14 15 /** * Returns the next object in the iterator. 16 17 18 * @return next object * @throws NoSuchElementException if there are no further elements */ public E next() throws NoSuchElementException { if (j == size) throw new NoSuchElementException("No next element"); removable = true; // this element can subsequently be removed return datalj++]; / post-increment j, so it is ready for future call to next } 19 20 21 22 23 24 25 26 27 /** * Removes the element returned by most recent call to next. * @throws IllegalStateException if next has not yet been called * Othrows IllegalStateException if remove was already called since recent next 28 29 30 31 32 public void remove(() throws IllegalStateException { if (Iremovable) throw new IllegalStateException("nothing to remove"); Array List.this.remove(j-1); // that was the last one returned j--; removable = false; } } /- 33 34 35 // next element has shifted one cell to the left // do not allow remove again until next is called 36 37 38 39 - end of nested Arraylterator class 40 /** Returns an iterator of the elements stored in the list. */ public Iterator iterator() { return new Arraylterator(); } 41 42 43 // create a new instance of the inner class 44
Fantastic news! We've Found the answer you've been seeking!
Step by Step Answer:
Answer rating: 40% (5 reviews)
To implement the preorder method of the AbstractTree class using a lazy iterator we can create a private inner class called PreorderIterator that impl...View the full answer
Answered By
Brian Kiprono
Taking part in public speeches, blogging, writing essays
0.00
0 Reviews
10+ Question Solved
Related Book For
Data Structures and Algorithms in Java
ISBN: 978-1118771334
6th edition
Authors: Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser
Question Posted:
Students also viewed these Computer science questions
-
Implement the preorder method in BST using a stack instead of recursion. Write a test program that prompts the user to enter 10 integers, stores them in a BST, and invokes the preorder method to...
-
Add the following method in the BST class that returns an iterator for traversing the elements in a BST in preorder. /** Returns an iterator for traversing the elements in preorder */...
-
Give a concrete implementation of the retainAll method for the set ADT, using only the other fundamental methods of the set. You are to assume that the underlying set implementation uses fail-fast...
-
Create a 3-panel informational brochure on threats to government computer systems and the potentials effects of these threats. To create your brochure, you can use brochure builder or another program...
-
Carl owns 100% of the issued shares of Compunet. He incorporated Compunet earlier this year to provide computer consulting services to Vitamins Inc., a retailer of energy-producing vitamins. Prior to...
-
An S corporation has the following information: Compute the S$ corporation's taxable income for the year. Sales Dividend income Tax-exempt interest Long-term capital gain Short-term capital gain Cost...
-
Aaron Reed, a photographer, was in a dispute with Ezelle Investment Properties, Inc., over Ezelle allegedly using one of Reeds photographs without permission. Reed sent Ezelle a cease-and-desist...
-
Eastbay Hospital has an auxiliary generator that is used when power failures occur. The generator is worn out and must be either overhauled or replaced with a new generator. The hospital has...
-
Show that the two drawings represent the same graph by labeling the vertices and edges of the right-hand drawing to correspond to those of the left-hand drawing. 5 el 06 Veq 5 e2 V2 ez 3 U5 (a) es 1...
-
Assume that on January 1, 2014, a Sunrise Bakery restaurant purchased a building, paying $53,000 cash and signing a $103,000 note payable. The restaurant paid another $66,000 to remodel the building....
-
Describe how to clone a LinkedBinaryTree instance representing a (not necessarily proper) binary tree, with use of the addLeft and addRight methods.
-
Algorithm preorderDraw draws a binary tree T by assigning x- and y-coordinates to each position p such that x(p) is the number of nodes preceding p in the preorder traversal of T and y(p) is the...
-
Select the compound in each of the following pairs that will be converted to the corresponding alkyl bromide more rapidly on being treated with hydrogen bromide. Explain the reason for your choice....
-
Of the 500 inpatient service days in the above example, 386 were Dr. Smith's patients. What percentage of inpatient service days did Dr. Smith have in the Neurosurgery Unit? Round to one decimal...
-
To show that you are actively listening during a job interview, you should nod strategically and maintain eye contact. O take selective notes. O ask related questions. O do all of these.
-
When delivering bad news, what should you do? O O O a. Show empathy b. All the mentioned c. Convey the bad news as soon as possible d. Give a complete explanation of the problem
-
12. Which of the statements is incorrect? bom sish(U) (A)A contention-based access method is a deterministic method (B)A fixed configuration Ethernet switch can be stackable. Jason W (C)The number of...
-
Elements of an address can be referred to in many ways ( ( Street could be listed as Street, St , , Str ) ) in a data warehouse. Ensuring it is referenced the same way is an example of Group of...
-
This year Colleen transferred $100,000 to an irrevocable trust that pays equal shares of income annually to three cousins (or their estates) for the next eight years. At that time, the trust is...
-
Review Exhibit 11.4. Analyze each product on the graph according to the characteristics that influence the rate of adoption. For example, what can you conclude from the data about the relative...
-
Repeat Problem P4-3 for the MLT-3 scheme, but use the following data streams. a. 00000000 b. 11111111 c. 01010101 d. 00011000 Problem 4-3 Draw the graph of the NRZ-L scheme using each of the...
-
Repeat Problem P4-3 for the 2B1Q scheme, but use the following data streams. a. 0000000000000000 b. 1111111111111111 c. 0101010101010101 d. 0011001100110011 Problem 4-3 Draw the graph of the NRZ-L...
-
Define the characteristics of a self-synchronizing signal.
-
exploration of the history of street art and its controversies of ppt/quicktime video.
-
Solve the following problems. Reduce answers to their lowest terms. Box your answers. 1 5 1.+ -= 4 12 2 2.- + 3 9 = 11 Convert improper fractions to mixed numbers. 2 11 3. + == 5 12 4 4 4. 12 7 5.35...
-
When average consumer income increased by 6%, BakeCo oven cleaner sales increased from 21,000 units to 22,500 units. Given this information,what is income elasticity for BakeCo oven cleaner ?
Study smarter with the SolutionInn App