Patricia Trie与Radix Trie的不同

RadixTree和PatriciaTrie都是基于前缀树的数据结构,旨在减少额外节点。PatriciaTrie通过在每个节点存储决定路径的位索引直接跳跃决策位,消除了一般Trie的一维分支,同时通过内部节点存储数据,消除了外部节点。其节点数量与插入元素严格相等,提供高效的查找性能。PatriciaTrie的创新之处在于仅使用一种类型的节点,简化了结构。
摘要由CSDN通过智能技术生成

在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类算法遇到的如下两个问题。

  1. the one-way branching leads to the creation of extra nodes in the trie
  2. 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.

具体用例,如下所示

逐步建立Patricia Trie的流程如下所示
总结

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值