Skip to main content

Red Black Tree Insertion in Java

Red Black Tree is a binary search tree. With extra property with each node having color either RED or BLACK.
It has following properties:
  1. Every node is either RED or BLACK
  2. Root element will have BLACK color.
  3. Every RED node has BLACK children.
  4. Every path from root to leaf will have same number of BLACK nodes.
We have few theorems over RB tree:

Note: Height of RB tree is θ(logn).

Insertion:
Insertion in RB tree is little tricky as compared to BST, we have following cases to insert a node in RB tree:

  1. When tree is null just insert the node and it will be root, color will be BLACK.
  2. To insert an element first find its position:
    1. if key <= node.key then move to left
    2. else move to right
  3. Once we find the correct place to insert, we insert the node with color RED.
  4. But if the parent of the node just inserted has color RED then it will violate 3 property.
  5. So we have to fix this issue. We call it double RED problem, we resolve it using following few cases.
DOUBLE RED PROBLEM:
Few naming conventions used are parent, grandparent and uncle. And these conventions are inspired from real life.

Case 1: When uncle has red color

We will make the changes as shown in the figures:
When node x is in left side of parent and uncle is in right side of grand parent of x, second image is solution:

 When node x is in right side of parent and uncle is in left side of grand parent of x, second image is solution of problem shown in first image:




Note 1: After solving the problem it could happen that at new X double red problem could occur, so we will call our function at new X again.
Note 2: Triangles shown under X are null nodes and when problem propagates they could be trees also. But under Uncle the triangles could be null nodes or trees.

Case 2: When uncle has black color (Zig-Zag Rotation)

When x is in right side of its parent and uncle is in right side of x's grandparent.
After Zig-Zag Rotation



When x is in left side of its parent and uncle is in left side of x's grandparent.


After Zig-Zag Rotation

Case 3: When uncle and sibling both have black color (Zig-Zig Rotation)
When x is in left side of its parent and uncle is in right side of x's grandparent.

After Zig-Zig Rotation


When x is in right side of its parent and uncle is in left side of x's grandparent.
After Zig-Zig Rotation



Java Code for Insertion:
There are two classes and one interface:
  1. RBTreeFunctions - interface
  2. RBTree - class with functions
  3. RBTreeTest class - testing(Driver) class
Code: click here

Comments

Popular posts from this blog

Goodness of Fit Test for normal and poisson distribution

Meaning of Goodness of fit test: We find out which distribution fits the sample data the most. And this is achieved using chi-square distribution (Snedecor and Cochran, 1989). How to apply: There are 4 steps to follow: State the hypothesis:  Data follows a distribution or not Criteria to reject null hypothesis: if Χ 2  > Χ 2 (k,1-α) then reject null hypothesis. Analyze sample data: Compute the chi-square value using below formula: ∑(Oi- Ei) 2 /Ei        : Oi is observed frequency and Ei is expected frequency Interpret the results: Declare the results after comparing the values of Χ 2 and Χ 2 (k,1-α), where k is degree of freedom and  α is significance level. Degree of Freedom: It is  =  n - 1 - m m: number of parameter in the distribution. So in case of normal distribution m is 2 ( μ ,α) and in case of poisson dist. m is = 1 ( λ). Example 1: Goodness of fit test for Normal Distribution Year wise d...

Reliable User Datagram Protocol

RUDP provides  a solution where UDP  is too primitive because guaranteed-order  p acket  delivery is desirable, but  T CP  adds too much overhead.  In order for RUDP to gain higher Quality of Service , RUDP implements features that are similar to TCP with less overhead. Implementations: In order to ensure quality, it extends UDP by means of adding the following features: Acknowledgment of received packets Flow control Re-transmission of lost packets Over buffering (Faster than real-time streaming) RUDP is not currently a formal standard, however it was described in an n 1999. It has not been proposed for standardization. Working Example: One way to think about how RUDP types of transport work is to use a basic model where all the data is sent in UDP format, and each missing packet is indexed.  Once the main body of the transfer is done, the recipient sends the sender the index list and the sender re-transmits ...

Bounding Rank of Fibonacci Heap

Bounding Rank of Fibonacci Heap to lgn: Before understanding the concept see below notations, tree(H)=number of nodes in the root list in below Fibonacci Heap Example rank(x)=number of children it has rank(H)=max rank(x)  Note: Black nodes are marked nodes (when one of its children is deleted it is marked if it is not already marked). We have following operation in Fibonacci Heap: Insert - O(1) Union - O(1) Decrease Key - O(1) Delete-Min - O(Rank(H)) = O(lgn) Delete - O(Rank(H)) = O(lgn) In this blog we will see how the rank is bounded to be lgn . So some assumptions I have made that you know at a time we cannot delete >1 child of any node. And after deleting child of a node we make it as Marked Node  if it is not marked already. Some Background; Merge Operation: We do merge operation when we do delete minimum. Merge operation is when rank of two nodes is same, we merge them as making minimum of them as root node and o...