Red-Black Trees For Dynamic Data
Structures

Red-black trees or commonly known as rbtree can be
very useful in making dynamic data structures which can be used for a
variety of purposes from string based hash tables to array like
linked list. The Linux kernel extensively uses rbtree for managing
various sort of data elements where inputs are dynamic and huge.

Tree like data structures have been used for long as
they provide better search efficiency as compared to other data
structures like linked list and arrays. But there is a fundamental
problem with the tree like structures that is when the input are
progressive for example numeric keys 1, 3, 4, 9, 100 and so on, the
tree tends to align in one direction thus reducing the benefit of
tree like data structure itself. This problem can be easily solved by
using rbtree which balances itself dynamically. For instance if the
data is being added and it is no longer the center of the node, it
will change the central node so that the number of children are
balanced.

Illustration 1: Tree - Generated from http://people.ksp.sk/~kuko/bak/

As shown in the illustration 1 and 2, tree generated
by rbtree is better and efficient.

Coming back to the implementation, there are a lot
of interesting things we can do with the rbtree. One of the basic and
the most useful complex data structure can be an array wherein any
new element that is added is numbered serially thus we get
flexibility of adding or removing elements easily without having to
work on a fixed range. For example when we add first element its
index automatically becomes 1, the consecutive element is numbered 2
and so on. Whenever an element is to removed all the elements with
higher indexes can be renumbered thus reordering the complex array.

Secondly, keys can be based on the strings
themselves thus we can implement hash tables based on strings wherein
a group of elements can be inserted with a string identifier.