Maximum number of Red nodes in Red-black tree? - binary-tree

Lets assume there is a Red-Black tree. the black height of it is h.
What will be the maximum number of Red nodes in the tree as a function of h?

The maximum number of red links in a red-black BST with N nodes is ~1/2 N.
Consider a 2-3 tree containing N nodes, all of which are 3-nodes. The corresponding red-black BST has 2N nodes and N red links (one corresponding to each 3-node).
I hope this answers your question.

Related

Determine whether a subtree of a red-black tree has at most 3n/4 nodes?

I have a red-black tree with n nodes, rooted at x. How can I prove or disprove that the number of nodes in the left subtree of x (including the root of x.left) is at most 3n / 4 without counting?
You can just construct a counter-example with as many red nodes as possible on the left, and no red nodes at all on the right.
If the right is a complete all black tree with 2^h-1 nodes, and the left can be a complete tree with 2^(2h)-1 nodes.
When h>=3 the left side has more than 3n/4 nodes.

Minimum number of vertices in a binary tree of height 5

What is the Formula to Find the minimum number of vertices required to make a binary tree (not a complete binary tree) of height 5 ?
A binary tree's height cannot be bigger than the number of nodes or vertices in the tree. So yes, the minimum number of vertices required for a binary tree of height 5 will be 5. Also, there must be n-1 edges between them. You can imagine a single series of connected nodes, and that is basically what you get.
Alternately, a full binary tree is a binary tree in which each internal vertex has exactly two children.This means a binary tree with n internal vertices has 2n + 1 vertices, 2n edges, and n + 1 leaves.
the minimum number of vertices in a binary tree of height n (with no further constraints) is always n.
proof:
if there were less than n nodes, the tree wouldn't be a valid binary tree (I guess I don't need to further explain this point)
if there were more than n nodes, it means at least one of the nodes has a brother (pigeonhole principle), which could be taken off the tree and it would result in a valid, smaller binary tree, so we know that this tree is not minimal, which opposes our assumption of a minimal tree.
-> a binary tree T is minimal -> T has n nodes

Formula for finding number of possible AVL trees with n nodes

let the number of nodes be 3.
If a,b,c.. are in order c>a>b then possible avl trees are:
n=1 gives 1,n=2 gives 2..(look image)
As we know for a BST it is 2n C n/ (n+1).Have anyone tried to deduce a formula that can find the number of avl trees when the number of nodes are given.
example question:what is the number of possible avl trees with 11 nodes?
I doubt that simple formula exists. But you can find number of possible AVL trees with dynamic programming, filling 2D table, where n is number of nodes, h is tree height, then sum all non-zero n-nodes entries:
F(n, h) = Sum[by all possible i]{F(i,h-1)*F(n-1-i,h-1)} +
Sum[by all possible j]{F(j,h-1)*F(n-1-j,h-2)} +
Sum[by all possible k]{F(k,h-2)*F(n-1-k,h-1)}
Explanation: we can make n-nodes h-height AVL tree, connecting root node with two valid trees of equal height (h-1), or with (h-1) and (h-2) trees, or with (h-2) and (h-1) trees.

Relationship between number of nodes and height

I am reading The Algorithm Design Manual. The author states that the height of a tree is:
h = log n,
where
h is height
n = number of leaf nodes
log is log to base d, where d is the maximum number of children allowed per node.
He then goes on to say that the height of a perfectly balanced binary search tree, would be:
h = log n
I wonder if n in this second statement denotes 'total number of leaf nodes' or 'total number of nodes'.
Which brings up a bigger question, is there a mathematical relationship between total number of nodes and the height of a perfectly balanced binary search tree?
sure, n = 2^h where h, n denote height of the tree and the number of its nodes, respectively.
proof sketch:
a perfectly balanced binary tree has
an actual branching factor of 2 at each inner node.
equal root path lengths for each leaf node.
about the leaf nodes in a perfectly balanced binary tree:
as the number of leafs is the number of nodes minus the number of nodes in a perfectly balanced binary tree with a height decremented by one, the number of leafs is half the number of all nodes (to be precise, half of n+1).
so h just varies by 1, which usually doesn't make any real difference in complexity considerations. that claim can be illustrated by remembering that it amounts to the same variations as defining the height of a single node tree as either 0 (standard) or 1 (unusual, but maybe handy in distinguishing it from an empty tree).
It doesn't really matter if you talk of all nodes or just leaf nodes: either is bound by above and below by the other multiplied by a constant factor. In a perfectly balanced binary tree the number of nodes on a full level is the number of all nodes in levels above plus one.
In a complete binary tree number of nodes (n) and height of tree (h) have a relationship like this in below.
n = 2^(h+1) -1
this is the all the nodes of the tree

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