简体   繁体   English

什么被认为是红黑树中的叶子?

[英]What is considered a leaf in red black trees?

I am studying red black trees from CLRS .我正在研究CLRS 的红黑树。 I have 2 questions about the part where properties of red-black trees are discussed.我有两个关于讨论红黑树属性的部分的问题。 The passage from CLRS is as follows:来自CLRS的段落如下:

A red-black tree is a binary tree that satisfies the following red-black properties:红黑树是满足以下红黑性质的二叉树:

  1. Every node is either red or black每个节点要么是红色要么是黑色

  2. The root is black根是黑色的

  3. Every leaf(NIL) is black每片叶子(NIL)都是黑色的

  4. If a node is red, then both its children are black如果一个节点是红色的,那么它的两个孩子都是黑色的

  5. For each node, all simple paths from the node to descendant leaves contain the same number of black nodes对于每个节点,从节点到后代叶子的所有简单路径都包含相同数量的黑色节点

First of all, it says a red-black tree is a binary tree .首先,它说红黑树是二叉树 Why didn't they say a red-black tree is a binary search tree .为什么他们不说红黑树是二叉搜索树 I thought the whole purpose of a red-black tree is to maintain the balance in a search tree to insure logN operations.我认为红黑树的全部目的是保持搜索树中的平衡以确保logN操作。 Second, why is the leaf in a red-black tree the NIL ?其次,为什么红黑树的叶子是NIL

In regular binary trees, we're used to this:在常规的二叉树中,我们习惯于这样:

               4
         5            7
    3        2     Nil Nil
 Nil Nil  Nil Nil

In this case, 3, 2, and 7 are the leaves.在这种情况下,3、2 和 7 是叶子。 Why are the leaves depicted as the Nil's in CLRS?为什么叶子在 CLRS 中被描述为 Nil's?

In the book, they refer to a red-black tree as a binary search tree, it's right at the beginning of the chapter.在书中,他们将红黑树称为二叉搜索树,就在本章的开头。

Also in the chapter, they indicate that nodes labeled as nil are actual nodes (called sentinels ), with the same fields as an ordinary node but with a color field with fixed value "black" (the other fields can be set to arbitrary values);同样在本章中,它们表示标记为nil的节点是实际节点(称为sentinels ),具有与普通节点相同的字段,但具有固定值“黑色”的color字段(其他字段可以设置为任意值) ; these nodes always appear as leafs in the tree.这些节点总是在树中显示为叶子。 So it's different from the usual BST where a leaf is a node whose left subtree and right subtree are both nil .所以它不同于通常的 BST,其中叶子是一个节点,其左子树和右子树都是nil

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

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