23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

There are also a number of generic methods a tree should probably support that are<br />

not necessarily related to its tree structure, <strong>in</strong>clud<strong>in</strong>g the follow<strong>in</strong>g:<br />

size():<br />

return the number of nodes <strong>in</strong> the tree.<br />

isEmpty():<br />

Test whether the tree has any nodes or not.<br />

iterator():<br />

return an iterator of all the elements stored at nodes of the tree.<br />

positions():<br />

return an iterable collection of all the nodes of the tree.<br />

replace(v,e):<br />

Replace with e <strong>and</strong> return the element stored at node v.<br />

Any method that takes a position as an argument should generate an error condition<br />

if that position is <strong>in</strong>valid. We do not def<strong>in</strong>e any specialized update methods for trees<br />

here. Instead, we prefer to describe different tree update methods <strong>in</strong> conjunction<br />

with specific applications of trees <strong>in</strong> subsequent chapters. In fact, we can imag<strong>in</strong>e<br />

several k<strong>in</strong>ds of tree update operations beyond those given <strong>in</strong> this book.<br />

7.1.3 Implement<strong>in</strong>g a Tree<br />

The <strong>Java</strong> <strong>in</strong>terface shown <strong>in</strong> Code Fragment 7.1 represents the tree ADT. Error<br />

conditions are h<strong>and</strong>led as follows: Each method that can take a position as an<br />

argument, may throw an InvalidPositionException, to <strong>in</strong>dicate that the<br />

position is <strong>in</strong>valid. Method parent throws a BoundaryViolationException<br />

if it is called on the root. Method root throws an EmptyTreeException if it is<br />

called on an empty tree.<br />

Code Fragment 7.1: <strong>Java</strong> <strong>in</strong>terface Tree<br />

represent<strong>in</strong>g the tree ADT. Additional update methods<br />

may be added, depend<strong>in</strong>g on the application. We do<br />

not <strong>in</strong>clude such methods <strong>in</strong> the <strong>in</strong>terface, however.<br />

381

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!