Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- L = length of number
- N = number of words
- M = length of each word
- Here's a trie. It has 11 nodes.
- root
- / | \
- a b d
- / | / \ / | \
- a b a d a b d
- Here's a number: 2222222... (L 2's)
- According to the traversal algorithm
- a. Look for ('a', 'b', 'c') at the start of the trie.
- b. If the relevant node exists, repeat the procedure from that node.
- c. If it doesn't exist, terminate.
- So in this example, we go to nodes (a) and (b). Then from there we go to nodes (aa), (ab), and (ba).
- After that, we've terminated all paths. The number of traversals is not dependent on L -- it's capped by the number
- of nodes in the trie.
- A bit more of a generic explanation:
- The only place where we do a recursive call in the algorithm is b.
- In order to make a recursive call at (b), a node must exist in the trie.
- However, we can also show that each node in the trie will be visited at most once:
- - Each node in a trie corresponds to a unique prefix
- - Each path in the number traversal corresponds to a unique prefix
- --> So two paths in the number traversal can't visit the same node.
- As a result, the number of recursive calls is no more than the number of nodes on the trie.
Add Comment
Please, Sign In to add comment