C Program for Red Black Tree Insertion

A particular kind of self-balancing binary search tree known as a red-black tree guarantees logarithmic time complexity for insertion, deletion, and search operations. They are extensively employed in computer science and play a key part in preserving data balance across a range of applications. This article will explain Red-Black Trees in detail and provide a step-by-step tutorial on how to use the insertion technique in C.

  • Red-Black Trees in C and their insertion algorithm.
  • Because they ensure balanced binary search trees, red-black trees are an essential component of computer science.
  • We provide a thorough understanding of this crucial data structure by going over all of its aspects, such as color coding, rotations, and the methodical procedure to keep the balance during insertion.
C Program for Red Black Tree Insertion

Understanding Red-Black Trees

It's important to understand Red-Black Tree properties and regulations before going into the code.

1. Binary Search Tree (BST) Property:

Being a binary search tree, the Red-Black Tree follows the specifications of a binary tree in which each node can have a maximum of two offspring. Every node in a node's left subtree has values smaller than the node's value, while every node in its right subtree has larger values.

2. Coloring Scheme

Every node in a red-black tree is given one of two colors: either black or red. A node's color denotes specific characteristics that guarantee the balance of the tree. These attributes are:

  • Each node has two colors: red and black.
  • The tree's roots are always black.
  • All of the leaf nodes (NIL) are dark.
  • There cannot be any consecutive red nodes along any path if a node's children are red.
  • There must be an equal number of black nodes on any simple path that leads from a node to any of its child leaf nodes.

To maintain the balance of the tree, these characteristics are made to ensure that the longest path from the root to any leaf is never longer than twice as long as the shortest one.

3. Rotations

Rotations Red-Black Trees use both left- and right-handed rotations to preserve their characteristics during insertion and deletion. The tree is reorganized using these rotations, preserving the color attributes and BST property.

Let's go to the detailed description of the Red-Black Tree insertion algorithm in C now.

Red-Black Tree Insertion Algorithm

The procedures below will be used to put the Red-Black Tree insertion method into practice:

  • Insert a binary search tree (BST) conventionally.
  • The newly inserted node should be colored red.
  • To preserve the Red-Black Tree's characteristics, make the required rotations and recolors of the nodes.
C Program for Red Black Tree Insertion

Here's the C code for the Red-Black Tree algorithm:

Output:

5 Black 10 Black 15 Red 20 Black 30 Black

An example of a C Red-Black Tree insertion algorithm is shown in the code above. First, the global pointers for the root and NIL (sentinel) nodes are defined, along with the structure of a Red-Black Tree node. The insertFixUp function makes sure that the Red-Black Tree attributes are preserved after insertion, while the leftRotate and rightRotate functions are used to rotate left and right, respectively.

The newly added node is shown in red when the insert function performs a regular BST insertion. The insertFixUp function is then used to make any required color and rotation adjustments.

The Red-Black Tree is tested and visualized using the inOrderTraversal function, which also provides node colors and an in-order traversal of the tree.

The NIL node is initialized, a set of values is inserted into the Red-Black Tree, and the outcome of an in-order traversal is printed, showing the values and their corresponding colors. All of this occurs in the main function.

Running the Red-Black Tree Insertion Algorithm

You can use your choice of C compiler to compile the C code and test the Red-Black Tree insertion algorithm. For instance, you may execute the following command in your terminal to compile the code if you're using GCC:

An executable file called red_black_tree_insertion will be produced as a result. The software can then be used as follows:

After inserting the values {10, 20, 30, 15, 5} into the Red-Black Tree, the program will show the numbers and their colors in an in-order traversal.

Conclusion

Red-black trees are an effective data structure for balanced binary search trees and are used in many different applications, including self-balancing data structures and database indexing. We have covered the Red-Black Tree attributes in great length in this post, along with a step-by-step tutorial on how to use the insertion algorithm in C. Computer scientists and programmers can benefit greatly from knowing and using Red-Black Trees since they provide effective data management in a variety of applications.