Red-Black Trees

前端 未结 12 2272
逝去的感伤
逝去的感伤 2020-12-07 10:15

I\'ve seen binary trees and binary searching mentioned in several books I\'ve read lately, but as I\'m still at the beginning of my studies in Computer Science, I\'ve yet to

12条回答
  •  悲&欢浪女
    2020-12-07 10:55

    Since you ask which tree people use, you need to know that a Red Black tree is fundamentally a 2-3-4 B-tree (i.e a B-tree of order 4). A B-tree is not equivalent to a binary tree(as asked in your question).

    Here's an excellent resource describing the initial abstraction known as the symmetric binary B-tree that later evolved into the RBTree. You would need a good grasp on B-trees before it makes sense. To summarize: a 'red' link on a Red Black tree is a way to represent nodes that are part of a B-tree node (values within a key range), whereas 'black' links are nodes that are connected vertically in a B-tree.

    So, here's what you get when you translate the rules of a Red Black tree in terms of a B-tree (I'm using the format Red Black tree rule => B Tree equivalent):

    1) A node is either red or black. => A node in a b-tree can either be part of a node, or as a node in a new level.

    2) The root is black. (This rule is sometimes omitted, since it doesn't affect analysis) => The root node can be thought of either as a part of an internal root node as a child of an imaginary parent node.

    3) All leaves (NIL) are black. (All leaves are same color as the root.) => Since one way of representing a RB tree is by omitting the leaves, we can rule this out.

    4)Both children of every red node are black. => The children of an internal node in a B-tree always lie on another level.

    5)Every simple path from a given node to any of its descendant leaves contains the same number of black nodes. => A B-tree is kept balanced as it requires that all leaf nodes are at the same depth (Hence the height of a B-tree node is represented by the number of black links from the root to the leaf of a Red Black tree)

    Also, there's a simpler 'non-standard' implementation by Robert Sedgewick here: (He's the author of the book Algorithms along with Wayne)

提交回复
热议问题