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...
-
Find the derivative of the trigonometric function. f(t) = t 2 sin t
-
Using your analysis from P11-8A, prepare the stockholders' equity section of the Black Corporation's balance sheet. Problem P11-8A The stockholders' equity of Black Corporation at January 1 follows:...
-
The Kyle Sports Medicine facility is a not-for-profit health care facility that is trying to determine what transaction information should be included in its calculation of the performance indicator...
-
Why is Patriotism important in today's America? WoodmenLife is a patriotic fraternal organization.
-
Muskoka Boys Shaving Co. ("MBSC") is a GST/HST registrant operating an online store in Ontario's cottage country region. The company specializes in selling fine grooming products for men. It is June...
-
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...
-
Based on the sample results in Exercise 10.69, construct and interpret the 95% confidence interval for the population proportion. Is the hypothesized proportion (0.62) within the interval? Given the...
-
What are the implications of negative interest rates on financial markets and the broader economy, and how do central banks justify this unconventional monetary policy approach ?
-
How do you align the HR plan to adequately reflect the organizational mission? Strategic planning is essential for successful health care organizations. Alignment between organizational strategic...
-
Review the scenario information and the Strategic Plan to identify all the actions that will need to be completed to achieve the College's objectives and be documented in an Operational Plan. Ensure...
-
Lakshmi Company produces two products from stainless steel: dinner plates and flatware sets. Lakshmi uses a Product Costing System. Lakshmi has two support departments: Accounting (S1) and General...
-
Olive Company makes silver belt buckles. The company's master budget appears in the first column of the table. Required: Complete the table by preparing Olive's flexible budget for 5,900, 7,900, and...
-
Write about the following: 1. What are some companies show success with BYOD? 2. What are some benefits these companies get from BYOD? 3. What are some polices did these companies use to show success...
-
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.
-
Disk City, Inc. is a retailer for digital video disks. The projected net income for the current year is $1,820,000 based on a sales volume of 230,000 video disks. Disk City has been selling the disks...
-
You have a bag of fourteen candies; 2 each of purple, green and yellow, and 4 each of pink and red. What is the probability of choosing, without replacement, a purple, then a green, then a yellow,...
-
Please complete the following: Problem P3-18 3-18 Common-size statement analysis A common-size income statement for Creek Enterprises' 2021 operations follows. Using the firm's 2022 income statement...
Study smarter with the SolutionInn App