2-3-4树和红黑树哪个更容易实现?
我正在学习的教科书(Lafore)首先介绍了红黑树,并且不包含任何伪代码,尽管所提供的相关算法似乎相当复杂,有许多独特的情况。
接下来他提出了 2-3-4 树,在我看来,它更容易理解,而且我猜也更容易实现。他包含了一些非常清晰的实际 Java 代码。他似乎暗示 2-3-4 更容易实施,根据我迄今为止所看到的情况,我同意这一点。
然而,维基百科却说相反......我认为这可能是不正确的:
http:// en.wikipedia.org/wiki/2-3-4_tree
2-3-4树是红黑树的等距树,这意味着它们是 等效的数据结构。换句话说,对于每棵 2-3-4 树, 至少存在一棵红黑树,其中包含数据元素 相同的顺序。此外,2-3-4树上的插入和删除操作 导致节点扩展、分裂和合并等价于 红黑树中的颜色翻转和旋转。简介 红黑树通常首先引入2-3-4树,因为它们是 概念上更简单。 但是,2-3-4 棵树可能很难 由于数量众多,因此可以在大多数编程语言中实现 涉及树操作的特殊情况。红黑树是 实现起来更简单,因此倾向于使用。
具体来说,关于“大量特殊情况”的部分对我来说似乎相当落后(我认为是 RB 具有大量特殊情况,而不是2-3-4)。但是,我仍在学习(而且老实说还没有完全理解红黑树),所以我很想听听其他意见。
作为旁注……虽然我确实同意拉福尔所说的大部分内容,但我认为有趣的是,与维基百科所说的常见顺序相比,他以相反的顺序呈现了它们(2-3-4 在 RB 之前)。我确实认为 2-3-4 首先会更有意义,因为 RB 很难概念化。也许他选择这个顺序是因为 RB 与他在上一章中刚刚完成的 BST 关系更密切。
The textbook I'm learning from (Lafore) presents Red-Black Trees first, and does not include any pseudo-code, although the associated algorithms as presented seems fairly complex, with many unique cases.
Next he presents 2-3-4 Trees which seem to me much simpler to understand, and I would guess, to implement. He includes some actual Java code which is very clear. He seems to imply that 2-3-4 is easier to implement, and I would agree based on what I've seen so far.
Wikipedia, however, says the opposite... I think it is incorrect perhaps:
http://en.wikipedia.org/wiki/2-3-4_tree
2-3-4 trees are an isometry of red-black trees, meaning that they are
equivalent data structures. In other words, for every 2-3-4 tree,
there exists at least one red-black tree with data elements in the
same order. Moreover, insertion and deletion operations on 2-3-4 trees
that cause node expansions, splits and merges are equivalent to the
color-flipping and rotations in red-black trees. Introductions to
red-black trees usually introduce 2-3-4 trees first, because they are
conceptually simpler. 2-3-4 trees, however, can be difficult to
implement in most programming languages because of the large number of
special cases involved in operations on the tree. Red-black trees are
simpler to implement, so tend to be used instead.
Specifically, the part about the "larger number of special cases" seems quite backward to me (I think it is the RB which have the large number of special cases, not the 2-3-4). But, I'm still learning (and still honestly have not quite fully wrapped my head around the Red-Black Trees), so I'd love to hear other opinions.
As a sidenote... while I do agree with most of what Lafore says, I think it's interesting he presented them in the opposite order compared to what Wikipedia says is common (2-3-4 before RB). I do think 2-3-4 first would make more sense since the RB is so much harder to conceptualize. Perhaps he chose that order because the RB was more closely related to BST's which he had just finished in the previous chapter.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
RB 树可以在十几个左右实现行,如果您的语言中有模式匹配:
这个著名的定义 来自冈崎的论文
RB Trees can be implemented in a dozen or so lines, if you have pattern matching in your langugage:
This famous definition from Okasaki's paper