What are the Misunderstandings about Red-Black Tree Properties?

  • Thread starter Thread starter Joseph1739
  • Start date Start date
  • Tags Tags
    Properties Trees
Click For Summary
Red-black trees have specific properties that must be adhered to for proper functioning, including that all leaves (NIL nodes) are black, but this does not mean that all black nodes are leaves. The confusion arises from misinterpreting the definition of leaf nodes, which are nodes without children, while nodes like 1 and 25 are black but not leaves as they have children. The colors of non-leaf nodes are not random; they must follow strict rules, such as the root being black and red nodes having black children. Understanding these properties is crucial for maintaining the balance and integrity of a red-black tree. Misunderstandings about these rules can lead to incorrect implementations or assumptions about tree structure.
Joseph1739
Messages
33
Reaction score
0
I don't understand the requirements for a red-black tree.
Using this photo:
https://upload.wikimedia.org/wikipedia/commons/6/66/Red-black_tree_example.svg

One of the 5 required properties is that all leaves are black.
A leaf is a node without a child, so that would only be 11 and 15 two links down from the root. So why is 1 and 25 black? They clearly have a child, so they are not leaves. Similarly, why is 6, 22, and 27 not black. They don't have children so they should be black.
 
Technology news on Phys.org
That all leaves are black does not imply that all black nodes are leaves.

6, 22, and 27 do have child nodes, which are leaves marked NIL. Whether a given red-black tree has explicit leaves does not affect their role in balancing the tree.
 
Integrand said:
That all leaves are black does not imply that all black nodes are leaves.

6, 22, and 27 do have child nodes, which are leaves marked NIL. Whether a given red-black tree has explicit leaves does not affect their role in balancing the tree.
So if a node is not a leaf, then the color can be chosen at random?
 
Joseph1739 said:
I don't understand the requirements for a red-black tree.
Using this photo:
https://upload.wikimedia.org/wikipedia/commons/6/66/Red-black_tree_example.svg

One of the 5 required properties is that all leaves are black.
A leaf is a node without a child, so that would only be 11 and 15 two links down from the root. So why is 1 and 25 black? They clearly have a child, so they are not leaves. Similarly, why is 6, 22, and 27 not black. They don't have children so they should be black.
What are the other rules?
 
Joseph1739 said:
One of the 5 required properties is that all leaves are black.
That all leaf nodes are black says something quite different than all black nodes are leaves. You apparently misread that property as the latter.

A leaf is a node without a child, so that would only be 11 and 15 two links down from the root. So why is 1 and 25 black? They clearly have a child, so they are not leaves. Similarly, why is 6, 22, and 27 not black. They don't have children so they should be black.
A leaf node in a binary search tree contains no data and has no children. The black nodes that contain 11 and 15, along with the red nodes that contain 6, 22, and 27 are not leaf nodes. Each of those nodes contains data (11, 15, 6, 22, and 27, respectively) and each has two children (all of which are leaf nodes).
Joseph1739 said:
So if a node is not a leaf, then the color can be chosen at random?
You need to look at the other properties. The root node is black, so it's color cannot be chosen at random. Both child nodes of a red node must be black, so the colors of those child nodes cannot be chosen at random. Finally, there's the balancing rule. Just because a tree obeys the basic properties of a binary search tree and first four properties of a red-black tree does not necessarily mean that it is a red-black tree.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
6K
  • · Replies 4 ·
Replies
4
Views
9K
  • · Replies 19 ·
Replies
19
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
Replies
2
Views
2K
  • · Replies 15 ·
Replies
15
Views
6K
  • · Replies 10 ·
Replies
10
Views
4K