Question

In: Computer Science

The insertions in red-black trees cost upto 2 rotations (if uncle is black) and upto O(log...

The insertions in red-black trees cost upto 2 rotations (if uncle is black) and upto O(log n) color changes (if uncle is red). Show that amortized color updates per insertion is O(1). (Hint: to develop an appropriate potential function try to see what is decreasing in the structure when we push the red-conflict upwards in case 1.)

Solutions

Expert Solution

In Red-Black tree, we use two tools to do balancing.

1) Recoloring
2) Rotation

We try recoloring first, if recoloring doesn’t work, then we go for rotation. Following is detailed algorithm. The algorithms has mainly two cases depending upon the color of uncle. If uncle is red, we do recoloring. If uncle is black, we do rotations and/or recoloring.

Color of a NULL node is considered as BLACK.

Let x be the newly inserted node.
1) Perform standard BST insertion and make the color of newly inserted nodes as RED.

2) If x is root, change color of x as BLACK (Black height of complete tree increases by 1).

3) Do following if color of x’s parent is not BLACK and x is not root.
….a) If x’s uncle is RED (Grand parent must have been black from property 4)
……..(i) Change color of parent and uncle as BLACK.
……..(ii) color of grand parent as RED.
……..(iii) Change x = x’s grandparent, repeat steps 2 and 3 for new x.

….b) If x’s uncle is BLACK, then there can be four configurations for x, x’s parent (p) and x’s grandparent (g) (This is similar to AVL Tree)
……..i) Left Left Case (p is left child of g and x is left child of p)
……..ii) Left Right Case (p is left child of g and x is right child of p)
……..iii) Right Right Case (Mirror of case i)
……..iv) Right Left Case (Mirror of case ii)

Following are operations to be performed in four subcases when uncle is BLACK.

All four cases when Uncle is BLACK

Left Left Case (See g, p and x)

Left Right Case (See g, p and x)

Right Right Case (See g, p and x)

Right Left Case (See g, p and x)



Examples of Insertion

1) Every node has a color either red or black.

2) Root of tree is always black.

3) There are no two adjacent red nodes (A red node cannot have a red parent or red child).

4) Every path from a node (including root) to any of its descendant NULL node has the same number of black nodes.

Why Red-Black Trees?
Most of the BST operations (e.g., search, max, min, insert, delete.. etc) take O(h) time where h is the height of the BST. The cost of these operations may become O(n) for a skewed Binary tree. If we make sure that height of the tree remains O(Logn) after every insertion and deletion, then we can guarantee an upper bound of O(Logn) for all these operations. The height of a Red-Black tree is always O(Logn) where n is the number of nodes in the tree.

Comparison with AVL Tree
The AVL trees are more balanced compared to Red-Black Trees, but they may cause more rotations during insertion and deletion. So if your application involves many frequent insertions and deletions, then Red Black trees should be preferred. And if the insertions and deletions are less frequent and search is a more frequent operation, then AVL tree should be preferred over Red-Black Tree.

How does a Red-Black Tree ensure balance?
A simple example to understand balancing is, a chain of 3 nodes is not possible in the Red-Black tree. We can try any combination of colours and see all of them violate Red-Black tree property.

A chain of 3 nodes is nodes is not possible in Red-Black Trees. 
Following are NOT Red-Black Trees
        30             30               30       
       / \            /  \             /  \
     20  NIL         20   NIL         20   NIL
    / \             / \              /  \   
  10  NIL          10  NIL          10  NIL  
Violates          Violates        Violates
Property 4.      Property 4       Property 3 

Following are different possible Red-Black Trees with above 3 keys
         20                           20
       /   \                        /   \
     10     30                    10     30
    /  \   /  \                 /  \    /  \
 NIL  NIL NIL NIL             NIL  NIL NIL NIL


From the above examples, we get some idea how Red-Black trees ensure balance. Following is an important fact about balancing in Red-Black Trees.

Black Height of a Red-Black Tree :
Black height is number of black nodes on a path from root to a leaf. Leaf nodes are also counted black nodes. From above properties 3 and 4, we can derive,
a Red-Black Tree of height h has black-height >= h/2.

Number of nodes from a node to its farthest descendant leaf is no more than twice as the number of nodes to the nearest descendant leaf.


Every Red Black Tree with n nodes has height <= 2Log2(n+1)

This can be proved using following facts:
1) For a general Binary Tree, let k be the minimum number of nodes on all root to NULL paths, then n >= 2k – 1 (Ex. If k is 3, then n is atleast 7). This expression can also be written as k <= Log2(n+1)

2) From property 4 of Red-Black trees and above claim, we can say in a Red-Black Tree with n nodes, there is a root to leaf path with at-most Log2(n+1) black nodes.

3) From property 3 of Red-Black trees, we can claim that the number black nodes in a Red-Black tree is at least ⌊ n/2 ⌋ where n is the total number of nodes.

From above 2 points, we can conclude the fact that Red Black Tree with n nodes has height <= 2Log2(n+1)


Related Solutions

Please find and share algorithm about red/black trees. Explain it, give 2 real world usages and...
Please find and share algorithm about red/black trees. Explain it, give 2 real world usages and discuss its efficiency with your friends!
Find and share an algorithm about red/black trees. Explain it, give 2 real-world usages, and discuss...
Find and share an algorithm about red/black trees. Explain it, give 2 real-world usages, and discuss its efficiency.
Please explain each answer Problems Red-Black Trees (a) Given that we follow the insertion operations; What...
Please explain each answer Problems Red-Black Trees (a) Given that we follow the insertion operations; What is the largest possible number of red nodes in a red-black tree of n nodes? What is the smallest possible number? (b) Can a black node have one red child and one black child ? If we relax the condition such that the children of a red node can be either red or black, what are operations necessary to keep the tree balanced ?...
Suppose we have a sack with 2 red balls and 2 black balls, and we draw...
Suppose we have a sack with 2 red balls and 2 black balls, and we draw balls without replacement until the second red ball is drawn. Describe the random variable ? = "the number of balls drawn". Describe by giving the range, probability distribution, expected value, standard deviation, and variance.
A hand contains 2 red cards and 3 black cards. A card is selected from the...
A hand contains 2 red cards and 3 black cards. A card is selected from the hand and set aside. Then another card is selected and put with the first card. Let X be the number of black cards drawn from the hand (i.e. set aside) during this process. Find Pr[X = 1].
A roulette wheel has 38 slots, 18 of which are black, 18 red, and 2 green....
A roulette wheel has 38 slots, 18 of which are black, 18 red, and 2 green. When the wheel is spun, the ball is equally likely to land in any of the 38 slots. Gamblers can place a number of different bets in roulette. One of the simplest wagers chooses red or black. A bet of $1 on red will pay off an additional dollar if the ball lands in a red slot. Otherwise, the player loses the original dollar....
A hat contains a number of cubes: 3 red, 2 white, 1 blue, and 4 black....
A hat contains a number of cubes: 3 red, 2 white, 1 blue, and 4 black. If one cube is chosen at random, what is the probability that it is: A red cube? (3 points) Not a red cube? (3 points) A cube that is white OR black? (4 points) A cube that is neither white nor black? (4 points) What do the answers to part a and part b add up to and why? (5 points) If three cubes...
A roulette wheel has 38 slots: 18 red, 18 black, and 2 green. A ball is...
A roulette wheel has 38 slots: 18 red, 18 black, and 2 green. A ball is tossed into the wheel and eventually settles in a slot at random. You play many games, betting $1 on red each time. If the ball lands in a red slot you win $1, otherwise you lose the dollar you bet. After n games, what is the probability that you have more money than you started with? Give (approximate) numerical answers for n = 100...
Suppose an urn contains 3 black chips, 2 red chips, and two green chips. We draw...
Suppose an urn contains 3 black chips, 2 red chips, and two green chips. We draw three chips at random without replacement. Let A be the event that all three chips are of different color. (a) What is the probability space Ω you are working with? (b) Compute P(A) by imagining that the chips are drawn one by one as an ordered sample. (c) Compute P(A) by imagining that the three chips are drawn all at once as an unordered...
Given the following set of keys: {1, 2, 3} determine the number of distinct left-leaning red-black...
Given the following set of keys: {1, 2, 3} determine the number of distinct left-leaning red-black trees that can be constructed with those keys. Draw the tree for each possible key-insertion order, showing the transformations involved at each step.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT