When is a red-black tree left leaning? - data-structures

When is a red-black tree left leaning ?
Does it just mean that on the letf side from the root are more nodes then on the right side?
I cant really grasp the definition what left leaning really means somewehre I also read that its related to the 2-3-4 Tress.
Thanks for your help

It means that a black node is limited to having a maximum of one red child and if it has that red child it will appear as the left child. Left-leaning red-black trees model a 2-3 tree instead of a 2-3-4 tree the way the classic red-black tree does.

Related

Converting 2-3-4 to Red-Black Tree

I am familiar with converting individual 2-node, 3-node, and 4-nodes straight to Red-Black trees. And this Stackoverflow link is a good explanation 2-3-4 to Red-Black. However, I have a question about the example given in that link.
This is how the Stackoverflow question 2-3-4 to red-black was illustrated 2-3-4 to Red-Black
I highlighted the part that I am questioning. Why is it on this guide 4-node connected to 2-node I found and others on the internet, they say when encountering a 4 node connected to a 2 or 3 node, you need to switch the colors around. But in the StackOverflow example that I highlighted red, they didn't. Thanks
The following image does not imply that the colors of the red-back tree need to be swapped:
It merely describes the process of splitting a B-tree node, which leads to an alternative B-tree for the same data.
Then it shows how that different shape of the B-tree leads to a different coloring in the corresponding red-black tree, and how that new coloring is also a valid alternative.
But the translation from B-tree to red-black tree follows the rules you referred to:
If we look at the left side of the image, we see at the bottom layer of the B-tree a 4-node. According to the rules, this translates to a black node (c) with two red children (b and d). The 2-node at the root translates to a black node (a).
If we look at the right side of the image, we see two 2-nodes at the bottom layer of the B-tree. These translate each to a back node (b and d). The root 3-node is translated to a back node (a) with a red node (c) as child.
This is exactly what is depicted at the bottom of the image. The point is that these two variants are valid red-black trees for the same data, but derived from different shapes of B-trees.
Such a transition from the left to the right version might be needed when inserting a node. For instance, if an "e" would be added, then it cannot be added as a child of the "d" node in the red-black tree without recoloring. By switching to the right-side version of the red-black tree, the node can be added as (red) child of node "d".

Is it possible to convert all AVL trees to red-black trees?

While I was studying Search Trees, I found a problem
Show that the nodes of any AVL tree T can be colored "red" and "black" so
that T becomes a red-black tree.
And now I wonder whether converting any AVL tree to red black tree is possible.
Yes, it is possible.
General idea is to turn red some of nodes that are roots of subtrees of odd height.
More specifically, we turn red only those whose parents are root of subtrees of even height. You can prove that using recursion.

AVL tree - Why is it a must that the heights of the left and right children of some node may differ by 1?

What's wrong with the heights of the left and right children of some node differ being by 2?
This is my first encounter with AVL trees, and I can't seem to understand why it is a must?
Really, what's wrong with the children being different by 2?
Regards
By definition, a balanced binary tree can only differ by one. If you look at the algorithms that operate on the AVL trees, you'll see that this property is always maintained.
While it's possible to make some sort of data structure where the height differs by +- 2 at most, there is not real benefit in doing so. By leaving it as +- 1, you create a simpler, self-balancing data structure.
Well that's the concept of an AVL tree, the height of left and right childs must not differ by at most one.
From Wikipedia
In an AVL tree, the heights of the two child subtrees of any node
differ by at most one.
Since it's balanced, this make the search 0(logn), so it's faster than a non-balanced binary tree, where all the elements could be on the left, making it 0(n)

Comparison about balanced binary search trees

I've read some Q&As about self-balancing binary trees, but I'm not quite familiar with all of them.
The first one of them I got to know is AVL, the second is Red-Black tree.
There are something I don't quite understand: according to some books and articles, AVL can perform searching a little bit faster than Red-Black tree, well, this is understandable.
Then what's Red-Black tree's edge over AVL?
In AVL, probably after each insertion, we have to check for balance, but in Red-Black tree we don't have to do something like that frequently, right?
PS:
I search SO for something similar, but I didn't get satisfying answer.
Hope some friends can give me a detailed comparison of self-balancing trees.
An AVL tree has the following property: from each node, the difference in height of the left and the right subtree is at most 2.
In a red-black tree, on the other hand, the height of the left or right subtree of any node is at most twice the height of the other tree. That is, they differ at most by a factor of 2.
This shows intuitively that lookup is indeed faster in an AVL tree on average.
However, when inserting or deleting a node, we have to rebalance the AVL tree more often, to preserve the much stricter height invariant (on the other hand, rebalancing in a red-black tree is algorithmically much more complicated). This means that in practice, a red-black tree may perform much better than an AVL tree, in particular when it’s often changed.

How does a red-black tree work?

There are lots of questions around about red-black trees but none of them answer how they work. Why is it called red-black? How does this keep the tree balanced (thus increasing performance over an unbalanced normal binary search tree)? I'm just looking for an overview of how and why it works.
For searches and traversals, it's the same as any binary tree.
For inserts and deletes, more sophisticated algorithms are applied which aim to ensure that the tree cannot be too unbalanced. These guarantee that all single-item operations will always run in at worst O(log n) time, whereas in a simple binary tree the binary tree can become so unbalanced that it's effectively a linked list, giving O(n) worst case performance for each single-item operation.
The basic idea of the red-black tree is to imitate a B-tree with up to 3 keys and 4 children per node. B-trees (or variations such as B+ trees) are mainly used for database indexes and for data stored on hard disk.
Each binary tree node has a "colour" - red or black. Each black node is, in the B-tree analogy, the subtree root for the subtree that fits within that B-tree node. If this node has red children, they are also considered part of the same B-tree node. So it is possible (though not done in practice) to convert a red-black tree to a B-tree and back, with (most) structure preserved. The only possible anomoly is that when a B-tree node has two keys and three children, you have a choice of which key to goes in the black node in the equivalent red-black tree.
For example, with red-black trees, every line from root to leaf has the same number of black nodes. This rule is derived from the B-tree rule that all leaf nodes are at the same depth.
Although this is the basic idea from which red-black trees are derived, the algorithms used in practice for inserts and deletes are modified to enforce all the B-tree rules (there might be a minor exception - I forget) during updates, but are tailored for the binary tree form. This means that doing a red-black tree insert or delete may give a different structure for the result than that you'd expect comparing with doing the B-tree insert or delete.
For more detail, follow the Wikipedia link that MigDus already supplied.
A red-black tree is an ordered binary tree where each vertex is coloured red or black. The intuition is that a red vertex should be seen as being at the same height as its parent (i.e., an edge to a red vertex is thought of as "horizontal" rather than "descending").
[I don't believe the Wikipedia entry makes this point clear.]
The usual rules for red-black trees require that a red vertex never point to another red vertex. This means that the possible vertex arrangements for any subtree rooted with a black vertex (bbb, bbr, rbb, rbr -- for [left child][root][right child]) correspond to 234 trees.
Searching a red-black tree is just the same as searching an ordinary binary tree. Insertion and deletion are similar, except that a "fix-up" rotation may be required at some point to preserve the red-black invariant.
Cheers!

Resources