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.

With this partition data structure, operation union(A,B) is called with position<br />

arguments p <strong>and</strong> q that respectively represent the sets A <strong>and</strong> B (that is, A = p <strong>and</strong> B<br />

= q). We perform this operation by mak<strong>in</strong>g one of the trees a subtree of the other<br />

(Figure 11.18b), which can be done <strong>in</strong> O(1) time by sett<strong>in</strong>g the set reference of the<br />

root of one tree to po<strong>in</strong>t to the root of the other tree. Operation f<strong>in</strong>d for a position p<br />

is performed by walk<strong>in</strong>g up to the root of the tree conta<strong>in</strong><strong>in</strong>g the position p (Figure<br />

11.18a), which takes O(n) time <strong>in</strong> the worst case.<br />

Note that this representation of a tree is a specialized data structure used to<br />

implement a partition, <strong>and</strong> is not meant to be a realization of the tree abstract data<br />

type (Section 7.1). Indeed, the representation has only "upward" l<strong>in</strong>ks, <strong>and</strong> does not<br />

provide a way to access the children of a given node.<br />

Figure 11.18: Tree-based implementation of a<br />

partition: (a) operation union(A,B); (b) operation<br />

f<strong>in</strong>d(p), where p denotes the position object for<br />

element 12.<br />

723

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

Saved successfully!

Ooh no, something went wrong!