11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

430 Chap. 13 Advanced Tree <strong>Structures</strong>with keys in the upper half of the key range will be stored in the right subtree.While such a decomposition rule will not necessarily result in a balanced tree (thetree will be unbalanced if the records are not well distributed within the key range),<strong>at</strong> least the shape of the tree will not depend on the order of key insertion. Furthermore,the depth of the tree will be limited by the resolution of the key range; th<strong>at</strong>is, the depth of the tree can never be gre<strong>at</strong>er than the number of bits required tostore a key value. For example, if the keys are integers in the range 0 to 1023, thenthe resolution for the key is ten bits. Thus, two keys can be identical only until thetenth bit. In the worst case, two keys will follow the same p<strong>at</strong>h in the tree only untilthe tenth branch. As a result, the tree will never be more than ten levels deep. Incontrast, a BST containing n records could be as much as n levels deep.Splitting based on predetermined subdivisions of the key range is called keyspace decomposition. In computer graphics, the technique is known as imagespace decomposition, <strong>and</strong> this term is sometimes used to describe the process ford<strong>at</strong>a structures as well. A d<strong>at</strong>a structure based on key space decomposition is calleda trie. Folklore has it th<strong>at</strong> “trie” comes from “retrieval.” Unfortun<strong>at</strong>ely, th<strong>at</strong> wouldimply th<strong>at</strong> the word is pronounced “tree,” which would lead to confusion with regularuse of the word “tree.” “Trie” is actually pronounced as “try.”Like the B + -tree, a trie stores d<strong>at</strong>a records only in leaf nodes. Internal nodesserve as placeholders to direct the search process. but since the split points are predetermined,internal nodes need not store “traffic-directing” key values. Figure 13.1illustr<strong>at</strong>es the trie concept. Upper <strong>and</strong> lower bounds must be imposed on the keyvalues so th<strong>at</strong> we can compute the middle of the key range. Because the largestvalue inserted in this example is 120, a range from 0 to 127 is assumed, as 128 isthe smallest power of two gre<strong>at</strong>er than 120. The binary value of the key determineswhether to select the left or right branch <strong>at</strong> any given point during the search. Themost significant bit determines the branch direction <strong>at</strong> the root. Figure 13.1 showsa binary trie, so called because in this example the trie structure is based on thevalue of the key interpreted as a binary number, which results in a binary tree.The Huffman coding tree of Section 5.6 is another example of a binary trie. Alld<strong>at</strong>a values in the Huffman tree are <strong>at</strong> the leaves, <strong>and</strong> each branch splits the rangeof possible letter codes in half. The Huffman codes are actually reconstructed fromthe letter positions within the trie.These are examples of binary tries, but tries can be built with any branchingfactor. Normally the branching factor is determined by the alphabet used. Forbinary numbers, the alphabet is {0, 1} <strong>and</strong> a binary trie results. Other alphabetslead to other branching factors.One applic<strong>at</strong>ion for tries is to store a dictionary of words. Such a trie will bereferred to as an alphabet trie. For simplicity, our examples will ignore case inletters. We add a special character ($) to the 26 st<strong>and</strong>ard English letters. The $character is used to represent the end of a string. Thus, the branching factor for

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

Saved successfully!

Ooh no, something went wrong!