Java Reference
In-Depth Information
Controller
BSTAnimation
View
Model
BST
BTView
F IGURE 25.18
The controller obtains data and stores it in a model. The view displays the
data stored in the model.
The MVC architecture separates data storage and handling from the visual representation
of the data. It has two major benefits:
It makes multiple views possible so that data can be shared through the same model.
For example, you can create a new view that displays the tree with the root on the left
and tree grows horizontally to the right (see Programming Exercise 25.11).
It simplifies the task of writing complex applications and makes the components
scalable and easy to maintain. Changes can be made to the view without affecting the
model, and vice versa.
25.10
How many times will the displayTree method be invoked if the tree is empty? How
many times will the displayTree method be invoked if the tree has 100 nodes?
Check
Point
25.11
In what order are the nodes in the tree visited by the displayTree method: inorder,
preorder, or postorder?
25.12
What would happen if the code in lines 47-52 in BTView.java is moved to line 33?
25.13
What is MVC? What are the benefits of the MVC?
25.5 Iterators
BST is iterable because it is defined as a subtype of the java.lang.Iterable interface.
Key
Point
The methods inorder() , preorder() , and postorder() display the elements in inorder ,
preorder , and postorder in a binary tree. These methods are limited to displaying the ele-
ments in a tree. If you wish to process the elements in a binary tree rather than display them,
these methods cannot be used. Recall that an iterator is provided for traversing the elements
in a set or list. You can apply the same approach in a binary tree to provide a uniform way of
traversing the elements in a binary tree.
The java.lang.Iterable interface defines the iterator method, which returns an
instance of the java.util.Iterator interface. The java.util.Iterator interface (see
Figure 25.19) defines the common features of iterators.
iterator
«interface»
java.util.Iterator<E>
+ hasNext(): boolean
+ next(): E
+ remove(): void
Returns true if the iterator has more elements.
Returns the next element in the iterator.
Removes from the underlying container the last element
returned by the iterator (optional operation).
F IGURE 25.19
The Iterator interface defines a uniform way of traversing the elements in
a container.
 
 
 
Search WWH ::




Custom Search