Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Tries When searching for the name “Smith” in a phone book, we first locate the group of names starting with “S”, then within those we search for “m”,

Similar presentations


Presentation on theme: "1 Tries When searching for the name “Smith” in a phone book, we first locate the group of names starting with “S”, then within those we search for “m”,"— Presentation transcript:

1 1 Tries When searching for the name “Smith” in a phone book, we first locate the group of names starting with “S”, then within those we search for “m”, etc. Idea: Perform a search based on a prefix of the key, rather than a comparison of the whole key. –The branching is determined by a partial key value. –Each node branches out to as many nodes as there are characters.

2 2 Tries A Trie is a multi-way tree for storing strings in which –there is one node for every common prefix –the strings are stored in the leaves The order of the trie is m, where m is the size of the alphabet.

3 3 Tries Example: The set of strings {"mat", "mad, "am", "bad"} stored in a BST and in a trie mat ammad bad a m b a m a dt am matmad d bad

4 4 Tries Advantage: –The height of the tree depends on the length of the keys –A trie can be used to store very large sets but the height (and therefore the search time) is very short. Applications: –spell checker –web search engine (search by index word) –network router (search by IP address)

5 5 Alphabet : {A, P, S, T} Words: {A, APT, AT, PAT, PASS, PAST, PS, SAP, SAT, TAP} Example (slightly compressed...)

6 6 Tries Observation: Many nodes have few non-null pointers. –We would like to save space –Idea #1: Briandais tree Store only the pointers that are used Maintain all siblings in a linked list Disadvantages: –the list needs to be traversed linearly –we still use extra space for the pointers

7 7 Tries Observation: Many nodes have few non-null pointers. –We would like to save space –Idea #2: Compressed Trie Interleave the arrays in the nodes Minor disadvantage : –an unsuccessful search may take more steps to end.

8 8 APS#T    A  APT AT APS#T APS#T PS PA  node 1 node 2 node 3 node 1 node 2 node 3 p1 stands for pointer to node 1 p2p3  AAPTAT PA PS Compressed Tries

9 9 PATRICIA trees Observation: –Sometimes the actual set of keys is a small subset of the potential set of keys. –This may result in a large number of nodes that only have one descendant. –We would like to save some space Idea: –Make the tree more compact by collapsing long chains. –The resulting tree is called a PATRICIA tree * Practical Algorithm To Retrieve Information Coded In Alphanumeric

10 10 PATRICIA trees Idea: –Collapse chains of nodes that have only one child –For each branch indicate how many characters should be skipped (i.e. what the length of the collapsed chain is)

11 11 Patricia tree example     PASSPAST   SAPSAT APS#T APS#TAPS#T APS#T APS#T APS#T   32   PASSPAST  SAPSAT 000 0 0 0 0 0


Download ppt "1 Tries When searching for the name “Smith” in a phone book, we first locate the group of names starting with “S”, then within those we search for “m”,"

Similar presentations


Ads by Google