在StackExchange上有这样一个问题—What is the difference between radix trees and Patricia tries?(原问题)。然而,该问题下的回答并没有说明白这俩算法的区别。不仅如此,网上所有的相关资料都认为这俩是一样的,除了名字。
Radix Tree
关于Radix Tree,可以参考Wikipedia上的讲解。
In computer science, a radix tree (also radix trie or compact prefix tree) is a data structure that represents a space-optimized trie (prefix tree) in which each node that is the only child is merged with its parent.
这里说,Radix Tree是对前缀树的压缩,若该节点只有一个孩子节点,则将其合并到父节点中,以此来节省存储资源。Radix Tree很简单,也很容易理解。一个Radix Tree的例图,如下所示。

Patricia Trie
Patricia Trie,由Morrison在1968年提出,并发表于“Journal of the ACM” ,讲解可参考Robert Sedgewick所著的《Algorithms in C++, Parts 1–4: Fundamentals, Data Structure, Sorting, Searching, Third Edition》中Chapter Fifteen。Patricia trie主要解决Trie-based类算法遇到的如下两个问题。
- the one-way branching leads to the creation of extra nodes in the trie
- there are two different types of nodes in the trie, which leads to complications
在存储性能上,Patricia trie节点个数和插入key个数严格相等,并且查找性能很高(log N)。Robert Sedgewick原文对Patricia Trie算法的评价如下。
Like DSTs, patricia tries allow search for N keys in a tree with just N nodes; like tries, they require only about lg N bit comparisons and one full key comparison per search, and they support other ADT operations. Moreover, these performance characteristics are independent of key length, and the data structure is suitable for variable-length keys.
Patricia算法思想描述如下
Starting with the standard trie data structure, we avoid one-way branching via a simple device: we put into each node the index of the bit to be tested to decide which path to take out of that node. Thus, we jump directly to the bit where a significant decision is to be made, bypassing the bit comparisons at nodes where all the keys in the subtree have the same bit value. Moreover, we avoid external nodes via another simple device: we store data in internal nodes and replace links to external nodes with links that point back upwards to the correct internal node in the trie. These two changes allow us to represent tries with binary trees comprising nodes with a key and two links (and an additional field for the index), which we call patricia tries. With patricia tries, we store keys in nodes as with DSTs, and we traverse the tree according to the bits of the search key, but we do not use the keys in the nodes on the way down the tree to control the search; we merely store them there for possible later reference, when the bottom of the tree is reached.
具体用例,如下所示


总结
根本上来讲,两者都是Trie-based算法,而且设计的初衷都有减少额外的节点的考虑。但是Patricia更加巧妙和高效,Patricia trie节点个数严格的和插入的元素个数相同,且Patricia trie算法中只有一种节点,不存在中间节点和叶子节点的区别。