What is considered a leaf in red black trees? - algorithm

I am studying red black trees from CLRS.
I have 2 questions about the part where properties of red-black trees are discussed.
The passage from CLRS is as follows:
A red-black tree is a binary tree that satisfies the following red-black properties:
Every node is either red or black
The root is black
Every leaf(NIL) is black
If a node is red, then both its children are black
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. Second, why is the leaf in a red-black tree the 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. Why are the leaves depicted as the Nil's in CLRS?

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 nilare 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); 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.

Related

Red Black Tree Insertion & Deletion Uniqueness

I've been learning and working on implementing a red-black tree data structure. I'm following this article on red-black tree deletion examples and looking at example 5 they have:
When I insert the same nodes into my tree, I get the following:
I understand that red black trees are not unique (I think), therefore both of the above trees are valid since they don't violate any of the properties.
In the example article, after deleting node 1, they get the following:
But after deleting node 1 in my code, I get the following:
Since in my case, node 1 is red, I don't call my delete_fix function which takes care of re-arranging the tree and such. The deletion algorithm I was following simply states to call a delete_fix function if the node to be deleted is black.
However, after comparing my tree with the one in the example article I can see that mine is not exactly optimized. It still follows the rules of the red-black tree though. Is this to be expected with red-black trees or am I missing something here?
However, after comparing my tree with the one in the example article I can see that mine is not exactly optimized.
It is optimised. Your tree will be fast at deleting nodes 5, 7, 20 & 28. The other only 5 & 7.
Bear in mind that for Red-Black Trees, they can be bushy in one direction. If the black tree height of real nodes is N, then the minimum path from root to leaf node is N (all black) and maximum path from root to leaf node is 2 * N (alternatively black-red-black-red etc). If you try to add a new node to the bushy path that is at maximum height, the tree will recolour and/or rebalance.
If you want a more balanced search tree you should use an AVL tree. Red-Black trees favour minimal insertion/deletion fixups over finding a node. Your tree is fine.

What should a 4-node red-black tree look like

Hi I know little about red-black tree before and maybe it's a stupid question.
Here's a 4-node tree:
Is it a legal red-balck tree?
In my opinion, it actually violates rules of red-black tree:
Every red node has both of its children colored black.
Every path from the root to a tree leaf contains the same number (the "black-height") of black nodes.
If not what should a 4-node red-black look like?
Thanks
You are correct that this tree violates the rules of red-black trees because the path 3-2-1-Null goes through 3 black nodes, while 3-4-Null only goes through 2.
Recall that there is no constraint that black nodes have to have their children painted red, only the reverse. Even a tree with all black nodes is technically a red-black tree, as long as it is balanced and therefore satisfies that "Every path from the root to a tree leaf contains the same number (the 'black-height') of black nodes."
As a result, you could make this tree a valid red-black tree by painting nodes 2 and 4 black and painting node 1 red. Notice node 1 (the only red node) would still have 2 black children and all paths from the root to leaves would hit 3 black nodes. Therefore satisfying the rules of a red-black tree.

Why Red Black Trees always having nil nodes as their leaf nodes and what is it implies?

I don't know why we need the NIL node as a leaf node in Red-Black Trees. Can anyone give an explanation about its purpose?
NIL is a special type of node which indicates the leaf nodes in the other trees like binary search trees and AVL trees
NIL nodes helps for balancing the black height
When you delete a node black height is immediately pass to child if it has no children it has to passed someone... so NIL nodes helps to it
Another way when you insert a new node nil node helps us to identify the case we have faced (either uncle red or uncle black) sometimes uncle will can be NIL node
Red-black trees don't need NIL nodes. Indeed, in typical implementations of the structure, NIL nodes are omitted entirely to reduce memory consumption. Moreover, we can alter the definition of red-black tree to avoid mention of NIL nodes without losing any of the essential qualities of the structure:
Define a red-black tree to be a binary search tree with the additional properties that
Each node possesses a color attribute which is either red or black.
If a black node has just one child, the child must be red and a leaf.
A red node is either a leaf or has two black children.
All paths from root to leaf must have the same number of black nodes.
Take any red-black tree with NIL nodes, erase the NIL nodes, and it will satisfy the definition above. Conversely, take a tree that satisfies the definition above, put black NIL nodes beneath each non-NIL node with fewer than two children so that they have two children, and it will be a red-black tree according to the standard definition.

Largest and smallest number of internal nodes in red-black tree?

The smallest number of internal nodes in a red-black tree with black height of k is 2k-1 which is one in the following image:
The largest number of internal nodes with black height of k is 22k-1 which, if the black height is 2, should be 24 - 1 = 15. However, consider this image:
The number of internal nodes is 7. What am I doing wrong?
(I've completely rewritten this answer because, as the commenters noted, it was initially incorrect.)
I think it might help to think about this problem by using the isometry between red-black trees and 2-3-4 trees. Specifically, a red-black tree with black height h corresponds to a 2-3-4 tree with height h, where each red node corresponds to a key in a multi-key node.
This connection makes it easier for us to make a few neat observations. First, any 2-3-4 tree node in the bottom layer corresponds to a black node with either no red children, one red child, or two red children. These are the only nodes that can be leaf nodes in the red-black tree. If we wanted to maximize the number of total nodes in the tree, we'd want to make the 2-3-4 tree have nothing but 4-nodes, which (under the isometry) maps to a red/black tree where every black node has two red children. An interesting effect of this is that it makes the tree layer colors alternate between black and red, with the top layer (containing the root) being black.
Essentially, this boils down to counting the number of internal nodes in a complete binary tree of height 2h - 1 (2h layers alternating between black and red). This is equal to the number of nodes in a complete binary tree of height 2h - 2 (since if you pull off all the leaves, you're left with a complete tree of height one less than what you started with). This works out to 22h - 1 - 1, which differs from the number that you were given (which I'm now convinced is incorrect) but matches the number that you're getting.
You need to count the black NIL leafs in the tree if not this formula won't work. The root must not be RED that is in violation of one of the properties of a Red-Black tree.
The problem is you misunderstood the black height.
The black height of a node in a red-black tree is the the number of black nodes from the current node to a leaf not counting the current node. (This will be the same value in every route).
So if you just add two black leafs to every red node you will get a red-black tree with a black height of 2 and 15 internal nodes.
(Also in a red-black tree every red node has two black children so red nodes can't be leafs.)
After reading the discussion above,so if I add the root with red attribute, the second node I add will be a red again which would be a red violation, and after node restructuring, I assume that we again reach root black and child red ! with which we might not get (2^2k)-1 max internal nodes.
Am I missing something here , started working on rbt just recently ...
It seems you havent considered the "Black Leaves" (Black nodes) -- the 2 NIL nodes for each of the Red Nodes on the last level. If you consider the NIL nodes as leaves, the Red nodes on the last level now get counted as internal nodes totaling to 15.
The tree given here actually has 15 internal nodes. The NIL black children of red nodes in last layer are missing which are actually called external nodes ( node without a key ). The tree has black-height of 2. The actual expression for maximum number of internal nodes for a tree with black-height k is 4^(k)-1. In this case, it turns out to be 15.
In red-black trees, external nodes[null nodes] are always black but in your question for the second tree you have not mentioned external nodes and hence you are getting your count as 7 but if u mention external nodes[null nodes] and then count internal nodes you can see that it turns out to be 15.
Not sure that i understand the question.
For any binary tree where all layers (except maybe last one) have max number of items we will have 2^(k-1)-1 internal nodes, where k is number of layers. At second picture you have 4 layers, so number of internal nodes is 2^(4-1)-1=7

Are AVL trees always a subset of red black trees?

I am searching for a proof that all AVL trees can be colored like a red-black tree?
Can anyone give the proof?
By definition R/B trees can be slightly less balanced then AVL-s, as |maxPath - minPath| must be <= 1 for AVLs and maxPath <= 2 * minPath for R/Bs so that not every R/B is an AVL but on the other hand there is no need for the AVL-s To have Empty subTrees so
4
/ \
3 6
/\ /\
1 E 5 8
is a perfectly legal AVL and it is not an R/B because R/B cannot contain Leaves and must be terminated by Empty trees which are coloured always Black - so that you cannot colour the tree above.
To make it R/B you are allowed to convert every leaf x into node E x E
and then follow these rules:
R/B Tree:
Must be a BST
must contain only nodes and empty trees which are coloured either Black or Red
Every Red node has black children
All Empty Trees are Black
Given a node, all paths to Empty Trees must have same number of Black Nodes
Any Leaf can be replaced with Node whose Left & Right subTrees are Empty
Max Path T ≤ 2 * Min Path T
Btw just realized it coloured my nodes and leaves in Red - this was not intended.
Karol
The red-black tree adjusts the branch heights by red nodes, so if the height difference is bounded, you can always adjust all the branches, starting from the shortest black branch. But it requires a very large cost, because you have to count all the branch heights.
The maximum local height difffernce bound of red-black tree is 2.
I suspect the answer is no.
AVL trees balance better than RB trees, which means they balance differently, which would rather imply that you could not colour every AVL tree as a valid RB tree.
The answer is yes, every AVL tree can be colored Red-Black, and the converse doesn't hold.
I haven'y exactly figured out HOW to do it tho, and am also seeking the proof.

Resources