infinite number of maximally unbalanced red black trees - algorithm

I have to find a family of maximally unbalanced red-black trees and to prove the "respective attributes" of that family to prove that there is an infinitely big family of red black trees that have a height close to 2log(n+1).
Now my guess is that this family consists of basically all the red black trees that have one path with s-r-s-r ... nodes and the rest filled with black nodes. But how do I prove this? and how do i formally write down how such a family looks like?
Thank you!

Now my guess is that this family consists of basically all the red black trees that have one path with s-r-s-r ... nodes and the rest filled with black nodes.
That's a reasonable guess.
But how do I prove this?
Describe an infinite sequence of trees T_0, T_1, T_2, T_3, ..., such that, for every integer n, there exists a tree in the sequence with at least n nodes. Show that there exists a constant C such that, for every i, the height of T_i is at least 2log(n_i+1) - C, where n_i is the number of nodes in T_i. (This is one possible interpretation of the ambiguous term "close to".)
how do i formally write down how such a family looks like?
Inductively. I'll do the all-black trees as an example. The tree T_0 is empty (base case). For all integers i > 0, the tree T_i consists of a black node with left and right subtrees equal to T_{i-1} (inductive step). Then you can prove facts about these trees using induction.

Related

How many permutations of 1, 2,..., n yield a skew tree? [duplicate]

I know what Binary search tree is and I know how they work. But what does it take for it to become a skewed tree? What I mean is, do all nodes have to go on one side? or is there any other combination?
Having a tree in this shape (see below) is the only way to make it a skewed tree? If not, what are other possible skewed trees?
Skewed tree example:
Also, I searched but couldn't find a good solid definition of a skewed tree. Does anyone have a good definition?
Figured out a skewed Tree is the worst case of a tree.
`
The number of permutations of 1, 2, ... n = n!
The number of BST Shapes: (1/n+1)(2n!/n!n!)
The number of skewed trees of 1, 2, ....n = 2^(n-1)
`
Here is an example I was shown:
http://i61.tinypic.com/4gji9u.png
A good definition for a skew tree is a binary tree such that all the nodes except one have one and only one child. (The remaining node has no children.) Another good definition is a binary tree of n nodes such that its depth is n-1.
A binary tree, which is dominated solely by left child nodes or right child nodes, is called a skewed binary tree, more specifically left skewed binary tree, or right skewed binary tree.

Complexity analysis exercise on RB-Trees

BLACK_PATH(T,x)
if x==NIL
then return TRUE
if COLOR(x)==BLACK
then return BLACK_PATH(T,left(x)) || BLACK_PATH(T,right(x))
return FALSE
The exercises asks to analyse the complexity of this procedure. I believe the reccurrence is the following
T(n)<=2T(2n/3)+O(1)
Using the recursion tree I obtain T(n)=O(n). Is this correct?
The complexity of this method is linear (O(n)) in the worst case with regards to the number of elements in the tree.
Using the master theorem in terms of the total number of nodes here is difficult because it does not take into account the properties of a red black tree. While it is true in general for heaps that every subtree of a tree with n nodes has max 2n/3 nodes, it is also true that for red black trees every subtree has at max n/2 black nodes. This is because red black trees are balanced with respect to black nodes (every path downwards to a leaf node from an arbitrary node has the same number of black nodes).
Most importantly: because the number of total nodes is not asymptotically higher than the number of black nodes you can, by analyzing the complexity purely with regards to the total number of black nodes, implicitly analyze the complexity with regards to the total number of nodes.
So rather than using T(n)<=2T(2n/3)+O(1) you should use T(m)<=T(m/2)+O(1) where m is the number of black nodes which gives you O(m) and because, as previously discussed, O(m)==O(n), we have O(n).
Another way to think about it: So long as you can understand that this algorithm is O(n) when all the nodes in the tree are black, you should be able to understand that it could only possibly require fewer operations if some of the nodes in the tree are red, since regardless of where the red node is every node in the subtree rooted at that red node will be ignored and not visited by this recursive algorithm. So it can only be O(n) or better, establishing O(n) as your worst case.

Number of binary tree shapes of N nodes are there with height N-1?

How many binary tree shapes of N nodes are there with height N-1?
Also, how would you go about proofing by induction?
So binary tree of height n-1 with node n means all node will have only 1 child, sort of chain like structure? So number of binary tree will be different permutation of n numbers which is n. Am I thinking in the right direction?
You are thinking in the right direction and you have correctly transformed the original problem to a simple one. However what is strange is that it is explicitly stated that the tree is "binary" when in fact the statement dictates even tighter constraint.

Red-black trees, relation between black and red nodes

One of the questions from my homework was to find exact lower bound of
(#black nodes)/(#red nodes)
in rb-tree. the bound must be not asymptotic.
Any suggestions?
Your help would be very appreciated.
Assuming this is a homework:
Let's review some properties of RedBlack Trees from Wikipedia:
...
The root is black.
All leaves are black.
Both children of every red node are black.
...
To get a lower bound on #B/#R you want to construct a tree that has as many red nodes as possible. (Unfortunately, due to 2,3,4 you cannot construct an all red tree)
Some questions worth thinking about:
can you fit more red nodes in balanced or not-so-balanced trees?
does even or odd maximal height make a difference?
given that a tree contains 3, 7, ..., (2^n)-1 back nodes how many red ones can you fit in?

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