简体   繁体   English

使用二叉树实现字典

[英]Dictionary implementation using Binary Trees

I read at multiple locations that Binary Trees should be preffered over Hash Tables where memory is limited as Binary Trees would keep the data in sorted order while Hash Tables won't. 我在多个位置读到,二叉树应该优先于哈希表,在这里内存受到限制,因为二叉树将使数据保持排序的顺序,而哈希表则不会。 The tradeoff being Binary Trees would have a O(log n) lookup and insertion as compared to constant time insertion and lookup in case of hash tables. 与哈希表情况下的固定时间插入和查找相比,折衷方案是二叉树将具有O(log n)查找和插入。

I was wondering if I choose Binary Trees to implement my Address Book(always sorted), what should the key look like? 我想知道我是否选择二叉树来实现我的地址簿(总是排序),密钥应该是什么样? would value be the name and number pair? 值将是名称和数字对吗?

BTree != binary tree. BTree !=二叉树。 I'll assume you mean the latter. 我假设你是说后者。

How your address book would look depends on what lookups you want to do. 通讯录的外观取决于您要进行的查找。 If you want to find the address of a person given their name, then the keys are names and the addresses/numbers are the values. 如果要查找给定名字的人的地址,则键为名称,地址/数字为值。

If you want lookup from addresses to names, then you just reverse the keys and the values. 如果要从地址查找到名称,则只需反转键和值。 If you want two-way lookup, you'll need two trees per address book. 如果要双向查找,则每个地址簿都需要两棵树。

Note that binary tree-based dictionaries are available in the C++ standard library as std::map in the header <map> . 请注意,基于C ++标准库的基于二进制树的字典在标头<map>std::map的形式提供。 Don't roll your own unless you want a programming exercise; 除非您想进行编程练习,否则请不要自己动手; std::map is very hard to beat in terms of performance and features. std::map在性能和功能方面很难被击败。

When considering something like an address book, how the data will be used (lookups) should take precedence over how the data will be stored (physically in memory). 在考虑地址簿之类的内容时,数据的使用方式(查找)应优先于数据的存储方式(物理上在内存中)。

If your name is the key, how would that be stored? 如果您的名字是钥匙,那么如何存储? "First Last", "Last, First", etc. That will impact how the lookup can be performed. “最后一个”,“最后一个”,“最后一个”等。这将影响执行查询的方式。 What if you want to lookup by number (for instance, if you start typing a phone number and you want matching contacts to popup)? 如果要按号码查找(例如,如果您开始输入电话号码并希望弹出匹配的联系人)怎么办?

I'd suggest really thinking about how you plan to use the address book so that you don't inadvertently tie yourself down to some implementation before you've fully understood the requirements. 我建议您认真考虑一下您打算如何使用通讯簿,以免在完全理解需求之前不会无意间将自己束缚在某种实现上。

Given that, I'd have a look at boost::multi_index . 鉴于此,我来看看boost :: multi_index That library provides a set of data structures that can be used to store the data once, and provide an arbitrary number of "lookup keys" against it. 该库提供了一组数据结构,可用于一次存储数据,并为其提供任意数量的“查找关键字”。 Very handy for cases like this. 对于此类情况非常方便。

I think the key is just the name. 我认为关键只是名字。 The number is the value. 数字是值。 You search by name, right? 您按名称搜索,对不对?

And, if you want good performance, use AVL trees. 而且,如果您想要良好的性能,请使用AVL树。 That way, all subtrees has the same (± 1) deepness. 这样,所有子树的深度相同(±1)。

For a dictionary a type trie and the data structure binary tree is a good choice. 对于字典,类型trie和数据结构二叉树是一个不错的选择。 It'a also name crit-bit tree, radix-tree or patricia tree. 它也称为临界位树,基数树或贵族树。 A simplier trie with a hash-key is a kart-trie where the hash is used to define left and right but it's data structure is still a binary tree. 带有哈希键的更简单的特里是kart-trie,其中的哈希用于定义左右,但它的数据结构仍然是二叉树。 Then there is a ternary trie but it's data structure is from a B-tree and it has 3 leafs. 然后有一个三元树,但是它的数据结构来自B树,有3个叶子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM