# What would be the probabilty of a randomly generated tree to be a Red-Black Tree

The question is not related to the homework

I was working on a homework, and the specification was to generate a random tree with n elements(n being in the thousands for the assignment) and asked me how many of the trees generated were RBT. I have been iterating to the millions now, so I began to wonder what was the probability of this happening, that a tree with lets say 1000 randomly inserted elements, that have randomly generated colors(red or black) is a Red-Black Tree.

• What do you mean by "a tree is RBT"? A tree doesn't come with colors on the nodes. Also, note that there are several ways to generate a random binary tree. You are generating a binary tree from a random permutation. – Yuval Filmus Jun 5 '17 at 17:46
• Sorry, a tree is a Red-Black Tree. I generated the colors randomly and inserted the value(integer, color) in an array without using any order, straight up like this: (123,'black') and placed that in the array so it wont follow conventional insertion rules. How can I calculate the probability of inserting the values in that way such that it follows all the Red-Black Tree rules(black root, red children's must be black, leaves black and black depth) – Daniel Rodríguez Jun 5 '17 at 17:51
• It sounds pretty difficult. – Yuval Filmus Jun 5 '17 at 17:55
• @YuvalFilmus I tried starting with small cases but the permutations are just way too much because of the given rules for a binary tree being Red-Black – Daniel Rodríguez Jun 5 '17 at 18:00
• I don't know if this helps, because it depends on the actual nature of the problem, but the red-black colouring of nodes is just a way to construct an isomorphism between completely balanced (all leaves are at exactly the same depth) 2-3-4 trees and a subset of binary trees. There's an obvious recursion which can be used to count balance 2-3-4 trees, which might be slightly easier than the recursion to count balanced R-B trees, although they will turn out to be very similar. – rici Jun 5 '17 at 23:48