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.

A compressed trie is similar to a st<strong>and</strong>ard trie but it ensures that each <strong>in</strong>ternal node<br />

<strong>in</strong> the trie has at least two children. It enforces this rule by compress<strong>in</strong>g cha<strong>in</strong>s of<br />

s<strong>in</strong>gle-child nodes <strong>in</strong>to <strong>in</strong>dividual edges. (See Figure 12.8.) Let T be a st<strong>and</strong>ard trie.<br />

We say that an <strong>in</strong>ternal node v of T is redundant if v has one child <strong>and</strong> is not the<br />

root. For example, the trie of Figure 12.6 has eight redundant nodes. Let us also say<br />

that a cha<strong>in</strong> of k ≥ 2 edges,<br />

(v 0 ,v 1 )(v 1 ,v 2 )…(v k−1 ,v k ),<br />

is redundant if:<br />

• v i is redundant for i = 1, …, k−1 1.<br />

• v 0 <strong>and</strong> v k are not redundant.<br />

We can transform T <strong>in</strong>to a compressed trie by replac<strong>in</strong>g each redundant cha<strong>in</strong><br />

(v 0 ,v 1 ) … (v k−1 ,v k ) of k ≥ 2 edges <strong>in</strong>to a s<strong>in</strong>gle edge (v 0 , v k ), relabel<strong>in</strong>g v k with the<br />

concatenation of the labels of nodes v 1 ,…, v k .<br />

Figure 12.8: Compressed trie for the str<strong>in</strong>gs bear,<br />

bell, bid, bull, buy, sell, stock, stop. Compare this with<br />

the st<strong>and</strong>ard trie shown <strong>in</strong> Figure 12.6.<br />

Thus, nodes <strong>in</strong> a compressed trie are labeled with str<strong>in</strong>gs, which are substr<strong>in</strong>gs of<br />

str<strong>in</strong>gs <strong>in</strong> the collection, rather than with <strong>in</strong>dividual characters. The advantage of a<br />

compressed trie over a st<strong>and</strong>ard trie is that the number of nodes of the compressed<br />

trie is proportional to the number of str<strong>in</strong>gs <strong>and</strong> not to their total length, as shown <strong>in</strong><br />

the follow<strong>in</strong>g proposition (compare with Proposition 12.6).<br />

Proposition 12.7: A compressed trie stor<strong>in</strong>g a collection S of s str<strong>in</strong>gs from<br />

an alphabet of size d has the follow<strong>in</strong>g properties:<br />

• Every <strong>in</strong>ternal node of T has at least two children <strong>and</strong> most d children.<br />

767

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

Saved successfully!

Ooh no, something went wrong!