红黑树 平衡二叉搜索树_红黑树:自我平衡的二叉搜索树,并举例说明

红黑树 平衡二叉搜索树

什么是红黑树? (What is a Red-Black Tree?)

Red-Black Tree is a type of self-balancing Binary Search Tree (BST). In a Red-Black Tree, every node follows these rules:

红黑树是一种自平衡二进制搜索树(BST)。 在红黑树中,每个节点都遵循以下规则:

  1. Every node has two children, colored either red or black.

    每个节点都有两个孩子,颜色为红色或黑色。
  2. Every tree leaf node is always black.

    每个树叶节点总是黑色的。
  3. Every red node has both of its children colored black.

    每个红色节点都有两个黑色的子节点。
  4. There are no two adjacent red nodes (A red node cannot have a red parent or red child).

    没有两个相邻的红色节点(红色节点不能有红色父节点或红色子节点)。
  5. Every path from root to a tree leaf node has the same number of black nodes (called "black height").

    从根到树叶节点的每条路径都具有相同数量的黑色节点(称为“黑色高度”)。

插入红黑树 (Inserting into Red-Black Trees)

A node is initially inserted into a Red-Black Tree just like any Binary Search Tree. The new node is then given a color of red. After that node has been inserted, the tree must be validated to ensure none of the five properties have been violated. If a property has been violated, there are three potential cases requiring either a left-rotation, right-rotation, and/or a recoloring of the nodes. The cases are dependent on the "uncle" of the current node. Specifically, whether the "uncle" node is black or red. For more info on inserting, the three cases can be found here.

像任何二进制搜索树一样,节点最初会插入到Red-Black树中。 然后,新节点将被赋予红色。 插入该节点后,必须对树进行验证,以确保没有违反这五个属性。 如果违反了属性,则存在三种可能的情况,要求节点向左旋转,向右旋转和/或重新着色。 情况取决于当前节点的“叔叔”。 具体来说,“叔叔”节点是黑色还是红色。 有关插入的更多信息,可以在此处找到三种情况。

左斜红黑树 (Left-Leaning Red–Black Tree)

A left-leaning red–black (LLRB) tree is a type of self-balancing binary search tree. It is a variant of the red–black tree and guarantees the same asymptotic complexity for operations, but is designed to be easier to implement.

左倾红黑(LLRB)树是一种自平衡二进制搜索树。 它是红黑树的一种变体,可以保证相同的渐进复杂性,但设计起来更易于实现。

左斜红黑树的性质 (Properties of Left Leaning Red-Black Trees)

All of the red-black tree algorithms that have been proposed are characterized by a worst-case search time bounded by a small constant multiple of log N in a tree of N keys, and the behavior observed in practice is typically that same multiple faster than the worst-case bound, close to the optimal log N nodes examined that would be observed in a perfectly balanced tree.

所提出的所有红黑树算法都以最坏情况下的搜索时间为特征,该搜索时间以N个键的树中log N的较小恒定倍数为界,并且在实践中观察到的行为通常快于最坏情况下的边界,接近在理想平衡树中观察到的最优log N节点。

Specifically, in a left-leaning red-black 2-3 tree built from N random keys: ->A random successful search examines log2 N − 0.5 nodes. ->The average tree height is about 2 log2 N

具体来说,在由N个随机键构成的左倾红黑2-3棵树中:->随机成功搜索检查log2 N -0.5个节点。 ->平均树高约为2 log2 N

翻译自: https://www.freecodecamp.org/news/red-black-trees/

红黑树 平衡二叉搜索树

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值