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.

Figure 7.13: Operation that removes an external<br />

node <strong>and</strong> its parent node, used <strong>in</strong> the justification of<br />

Proposition 7.11.<br />

Note that the above relationship does not hold, <strong>in</strong> general, for improper b<strong>in</strong>ary<br />

trees <strong>and</strong> nonb<strong>in</strong>ary trees, although there are other <strong>in</strong>terest<strong>in</strong>g relationships that<br />

can hold, as we explore <strong>in</strong> an exercise (C-7.7).<br />

7.3.4 A L<strong>in</strong>ked Structure for B<strong>in</strong>ary<br />

Trees<br />

As with a general tree, a natural way to realize a b<strong>in</strong>ary tree T is to use a l<strong>in</strong>ked<br />

structure, where we represent each node v of T by a position object (see Figure<br />

7.14a) with fields provid<strong>in</strong>g references to the element stored at v <strong>and</strong> to the position<br />

objects associated with the children <strong>and</strong> parent of v. If v is the root of T, then the<br />

parent field of v is null. If v has no left child, then the left field of v is null. If v has<br />

no right child, then the right field of v is null. Also, we store the number of nodes of<br />

T <strong>in</strong> a variable, called size. We show the l<strong>in</strong>ked structure representation of a b<strong>in</strong>ary<br />

tree <strong>in</strong> Figure 7.14b.<br />

Figure 7.14: A node (a) <strong>and</strong> a l<strong>in</strong>ked structure (b) for<br />

represent<strong>in</strong>g a b<strong>in</strong>ary tree.<br />

401

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

Saved successfully!

Ooh no, something went wrong!