11

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:

enter image description here

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:

enter image description here

The number of internal nodes is 7. What am I doing wrong?

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
HMdeveloper
  • 2,772
  • 9
  • 45
  • 74

8 Answers8

11

(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.

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • 1
    +1. I took it for granted that the tree he posted had a maximum number of internal nodes. – IVlad Oct 13 '13 at 21:20
  • I think I could not express it well, in the second image black height is 1 so if we follow the formula we have 2^2-1 which 3 so it means there is 3 internal nodes but you can see that there is 7 – HMdeveloper Oct 13 '13 at 21:23
  • @user2860721- I think you have the definition of "black height" wrong. The black height of a red-black tree is the number of black nodes on any path from the root to the leaves, not the height of the highest black node. (If you know about the connection to 2-3-4 trees, the black height of the red-black tree is the height of the 2-3-4 tree that corresponds to the red-black tree). Does that make sense? – templatetypedef Oct 13 '13 at 21:26
  • Thanks,so with this I can get 2^4-1=15 but thisis the total # of nodes not internal nodes but we always say that 2^2k-1 is the number of internal nodes am I wrong? – HMdeveloper Oct 13 '13 at 21:30
  • @user2860721 - the image you posted does not have the maximum possible number of internal nodes, which is 15. – IVlad Oct 13 '13 at 21:33
  • @user2860721- As I mentioned in my answer, the maximum number of internal nodes is 2^(2k) - 1 and the maximum number of total nodes is 2^(2k + 1) - 1. You need to make the root node red to get the maximum total number of nodes. – templatetypedef Oct 13 '13 at 21:33
  • May be my confusion is here, if I want to expand the above image I should add black nodes then my black height would be higher than 2 and as I understood 15 is the total number of internal nodes with the black height of 2 so I think the above image shows the largest internal number with black height of 2 .please help? – HMdeveloper Oct 13 '13 at 21:38
  • @user2860721- Make a tree with five layers. The first, third, and fifth layers are red and the second and fourth layers are black. Then the black height is 2 and there are 15 internal nodes. Does that make sense? – templatetypedef Oct 13 '13 at 21:40
  • @user2860721: No you don't understand. Not all red-black trees start with a black node, so what you're describing is _not_ the only way to expand your tree. You could also do it by templatetypedef's way, in which case you would have a tree with black height of 2 _as well_, and that would be the maximum-internal-nodes scenario. I suggest you draw it so that you can visualize it and understand the logic. – geomagas Oct 13 '13 at 21:44
  • now it is better but as far as I know the root can not be red and if we make the first layer red we are violating red black tree property. Am I right? – HMdeveloper Oct 13 '13 at 21:45
  • @user2860721- Some authors require that the root node be black, but there is no reason that this must be the case. Typically, the root can be any color. – templatetypedef Oct 13 '13 at 21:49
  • in coremen textbook he insisted for root to be black and at the same time he believes that the largest # of internal nodes is 2^2k-1 and for me it is a contradiction how he got to that? – HMdeveloper Oct 13 '13 at 21:53
  • @user2860721- What edition / printing of the book are you using? – templatetypedef Oct 13 '13 at 21:55
  • @user2860721- I am almost positive that's a typo for exactly the reason you've given above. I think those bounds apply to red-black trees where the root can be red or black. – templatetypedef Oct 13 '13 at 22:02
  • This is incorrect. The root has to be black, see e.g. https://en.wikipedia.org/wiki/Red%E2%80%93black_tree#Properties property 2. –  Nov 03 '15 at 12:11
  • @CamilStaps You're right. I wrote this answer a few years back when I knew less about red/black trees than I do now. I've updated the answer - can you take a look at it and tell me what you think? – templatetypedef Nov 04 '15 at 20:30
  • @templatetypedef yes, this seems correct to me (though I'm just a student). Thanks for caring about your years-old posts! –  Nov 04 '15 at 20:36
  • @templatetypedef what about the total # nodes in red-black tree? is it just 2^(h+1) - 1 ? – Square-root Jun 13 '16 at 19:44
8

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.

John F.
  • 154
  • 1
  • 10
3

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

1

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 ...

Rishabh Puri
  • 91
  • 1
  • 7
0

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.

0

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.

stillanoob
  • 1,279
  • 2
  • 16
  • 29
0

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.

Gagan92
  • 1
  • 1
-1

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

Andrei Galatyn
  • 3,322
  • 2
  • 24
  • 38