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.

hasRight(v):<br />

Test whether v has a right child.<br />

Just as <strong>in</strong> Section 7.1.2 for the tree ADT, we do not def<strong>in</strong>e specialized update<br />

methods for b<strong>in</strong>ary trees here. Instead, we will consider some possible update<br />

methods when we describe specific implementations <strong>and</strong> applications of b<strong>in</strong>ary<br />

trees.<br />

7.3.2 A B<strong>in</strong>ary Tree Interface <strong>in</strong> <strong>Java</strong><br />

We model a b<strong>in</strong>ary tree as an abstract data type that extends the tree ADT <strong>and</strong> adds<br />

the three specialized methods for a b<strong>in</strong>ary tree. In Code Fragment 7.14, we show the<br />

simple <strong>Java</strong> <strong>in</strong>terface we can def<strong>in</strong>e us<strong>in</strong>g this approach. By the way, s<strong>in</strong>ce b<strong>in</strong>ary<br />

trees are ordered trees, the iterable collection returned by method children(v)<br />

(<strong>in</strong>herited from the Tree <strong>in</strong>terface) stores the left child of v before the right child of<br />

v.<br />

Code Fragment 7.14:<br />

for the b<strong>in</strong>ary tree ADT. Interface B<strong>in</strong>ary Tree<br />

extends <strong>in</strong>terface Tree (Code Fragment 7.1).<br />

<strong>Java</strong> <strong>in</strong>terface B<strong>in</strong>ary Tree<br />

7.3.3 Properties of B<strong>in</strong>ary Trees<br />

B<strong>in</strong>ary trees have several <strong>in</strong>terest<strong>in</strong>g properties deal<strong>in</strong>g with relationships between<br />

their heights <strong>and</strong> number of nodes. We denote the set of all nodes of a tree T at the<br />

398

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

Saved successfully!

Ooh no, something went wrong!