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 12.6: St<strong>and</strong>ard trie for the str<strong>in</strong>gs {bear, bell,<br />

bid, bull, buy, sell, stock, stop}.<br />

an i-character prefix X[0..i − 1] of a str<strong>in</strong>g X of S. In fact, for each character c that<br />

can follow the prefix X[0..i − 1] <strong>in</strong> a str<strong>in</strong>g of the set S, there is a child of v labeled<br />

with character c. In this way, a trie concisely stores the common prefixes that exist<br />

among a set of str<strong>in</strong>gs.<br />

If there are only two characters <strong>in</strong> the alphabet, then the trie is essentially a b<strong>in</strong>ary<br />

tree, with some <strong>in</strong>ternal nodes possibly hav<strong>in</strong>g only one child (that is, it may be an<br />

improper b<strong>in</strong>ary tree). In general, if there are d characters <strong>in</strong> the alphabet, then the<br />

trie will be a multi-way tree where each <strong>in</strong>ternal node has between 1 <strong>and</strong> d children.<br />

In addition, there are likely to be several <strong>in</strong>ternal nodes <strong>in</strong> a st<strong>and</strong>ard trie that have<br />

fewer than d children. For example, the trie shown <strong>in</strong> Figure 12.6 has several<br />

<strong>in</strong>ternal nodes with only one child. We can implement a trie with a tree stor<strong>in</strong>g<br />

characters at its nodes.<br />

The follow<strong>in</strong>g proposition provides some important structural properties of a<br />

st<strong>and</strong>ard trie:<br />

Proposition 12.6: A st<strong>and</strong>ard trie stor<strong>in</strong>g a collection S of s str<strong>in</strong>gs of total<br />

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

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

• T has s external nodes.<br />

• The height of T is equal to the length of the longest str<strong>in</strong>g <strong>in</strong> S.<br />

• The number of nodes of T is O(n).<br />

764

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

Saved successfully!

Ooh no, something went wrong!