Introduction
A radix tree, also known as compressed trie or compressed prefix tree, is a tree-like data structure for storing a set of strings. The edges of the tree are labeled by nonempty strings, and each node is either terminal or nonterminal. The strings that the tree contains are exactly the labels of all paths from the root to a terminal node. The tree must be in the normal form defined by the following conditions:
- All nonterminal non-root nodes have at least two children.
- For every node, all outgoing edges have different first characters.
For example, here is the radix tree containing the set ["test", "testing", "tested", "team", "teams", "technical", "sick", "silly"]
, with (N)
representing a nonterminal node and (T)
a terminal node:
-(N)-te-(N)-st-(T)-ing-(T)
| | |
| | +-ed-(T)
| |
| +-am-(T)-s-(T)
| |
| +-chnical-(T)
|
+-si-(N)-ck-(T)
|
+-lly-(T)
In this challenge, your task is to compute the height of the tree, given the strings as input.
Input
Your input is a non-empty list of strings of lowercase ASCII letters. It will not contain duplicates, but it may be in any order. It may contain the empty string. You may take the input in any reasonable format.
Output
Your output shall be the length of the longest root-to-leaf path in the corresponding radix tree, measured in the number of nodes it contains.
In the above example, the correct output is 4, corresponding to the paths
(N)-te-(N)-st-(T)-ing-(T)
(N)-te-(N)-st-(T)-ed-(T)
(N)-te-(N)-am-(T)-s-(T)
which contain 4 nodes.
Further examples
Here are a couple more examples of radix trees:
[""]
-(T)
["","fuller"]
-(T)-fuller-(T)
["","full","fuller"]
-(T)-full-(T)-er-(T)
["full","fuller"]
-(N)-full-(T)-er-(T)
["full","filler"]
-(N)-f-(N)-ull-(T)
|
+-iller-(T)
Rules and scoring
You can write a full program or a function. This is code golf, so the lowest byte count wins.
You can use any built-ins or libraries you want, but remember to include all import
s etc. in your byte count.
Third-party libraries – those not included in the standard install of your language – are allowed, but must be listed in the header separately, e.g. Python + pytrie0.2, 60 bytes.
Test cases
[""] -> 1
["fuller"] -> 2
["","fuller"] -> 2
["","full","fuller"] -> 3
["full","fuller"] -> 3
["full","filler"] -> 3
["full","filler","filter"] -> 4
["full","filler","fi","filter"] -> 5
["test","testing","tested","team","teams","technical","sick","silly"] -> 4
["a","aaaa","aabbaa","aabbaab","abaaa","aab","aabbb","aabba"] -> 8
["dbdbaca","ab","a","caaabaaa","adbbabdb","dbdbdbaca","dbbadbacaba","db"] -> 4
["db","dbbdbb","dbaa","cabcacaa","","acaabcacab","b","abaaaca","bacaaaaa"] -> 3
["aabaabdbb","bacaabadbbdb","abb","aabaa","ab","bcadbb","adbbcaaadbbb","caaa","bbdbcacadbab","dbbdbdb"] -> 4
["bbcaaabbbabbcadbbacadbbdbdb","b","bbbbaaaaaababa","ca","bb","bdbbacadbbdbbdbbababaacaca","abbaabbabcabaaa","bbbacacacabcacacabaaabb","bbcaaaab","bbbbcaacaadbcaaa","babbabcadbdbacacabbcacab","abcabbbaacadbcadb","bbcabbcadbcacaacaadbadbcaadb","dbbbdbbdbacaabbacabcadbdbacaca","bbaabdbdb","cabcadbbbadbadbbaadbcaca","adbadbadbdbcacadbdbbcaadbcaca","abaabbcab","aaabcaabcaab","bacacabcacaacadbadbb"] -> 6