Unified AVL and Red-Black Tree Design
Unified AVL and Red-Black Tree Design
{l_bounif, d_zegour}@[Link]
Abstract. We propose a unique representation of both The Red-Black tree was originally obtained from
AVL and Red-Black trees with the same time and space the 2-3 trees as an amelioration of AVL trees [8].
complexity. We describe all the maintenance operations The first version was designed in 1972 by Rudolf
and also the insertion and deletion algorithms. We give Bayer [6] under the name: "Symmetric Binary B-
the implementation of the proposed tree and the results.
We then make a comparison of the three structures. The
trees", where the author compares the structure
simulation results confirm the performance of the new with the class of B-trees. A few years later
representation relatively to AVL and Red-Black trees. Leonidas J. Guibas and Robert Sedgewick [9]
proposed a new form of the original structure
Keywords. Balanced binary trees, red-black trees, AVL where the tree balance is expressed using Red
trees, binary search tree, partitioning, data structures.
and Black colors.
Because of the difficulty to implement the Red-
1 Introduction Black tree in practice, especially in the deletion
process, some works were proposed to simplify the
Binary search trees are an efficient data structure corresponding algorithms. AA tree is a powerful
for loads applications in computer science but have simplification of Red-Black trees with the same
a poor worst case performance [1]. The good performance and much more approach and coding
remedy for that is the perfect balanced tree with a simplicity [10]. Moreover, several simple
height at most log(n) [2]. But unfortunately keeping implementations of Red-Black trees can be found
a perfect balance of a tree is rude and so in [11, 12]. Recently, the majority of works in terms
expensive in practice. One of the reason to of AVL and Red-Black trees aim basically to
introduce balanced trees is because of the costs simplify rather than get a good performance. In the
are guaranteed to be logarithmic while ensuring AVL tree case, [13] introduces a new simpler
that the tree remains almost balanced. insertion and deletion algorithms for AVL trees by
Balanced trees assure a random search, using virtual nodes. A brief study of AVL trees
insertion and deletion operations in time using this concept is presented in [14]. In the same
proportional to log(n). The first type of these trees spirit, work [15] gives a new algorithm and explains
is the AVL tree [2]; it is simple and deals well in how to easily maintain the balance factor after an
lookup operations. After that many alternatives of updating operation. When it comes to Red-Black
generalization, simplification or complement trees, a revisited version has been proposed [16]
studies of this first balanced tree have been where the code is considerably reduced compared
proposed [3, 4, 5, 6, 7]. to the implementation proposed in [17].
A novel kind of generalization of the AVL tree is The design of a balanced tree is still a rich area,
the Red-Black tree. It is one of the most important and not yet fully explored. A recent proposition with
and used self-balancing data structures. It behaves improvements for binary search trees are
well in update-intensive applications, since it proposed in 2015 [18].
performs log(n) operations in the insertion process The main idea is to assign a non-negative
and at most two restructurings in the deletion one. integer rank to each node and impose eight rank
rules to give the AVL tree, a new kind of balanced These definitions are related mainly to the
tree and different kinds of Red-Black trees: the binary search tree and the partitioning problem
standard version [9] equivalent to the symmetric on graphs.
binary B-trees [6], the binary B-tree [5], the left
leaning trees [16]. Their rank-based framework 2.1 Binary Search Tree
generalizes the dichotomies framework of Guibas
and Sedgewick [9]. It is a very interesting work It is an organized tree in a binary representation
since it provides not only a new framework for where each node contains a key, a data, the left
defining height-based balance but also a new sort child and the right child which can be
of balanced binary tree: the weak AVL tree. missing nodes.
However, in addition to the obligation of satisfying
Consider x a node in a binary search tree. If y
loads of inequalities corresponding to the number
is a node in the left sub-tree of x, then [Link] ≤
of inserted and deleted cases, the framework gives
[Link]. If y is a node in the right sub-tree of x, then
separate rules to define common balanced trees
[Link] ≥ [Link]. It is called the binary-search-tree
rather than a unique hybridization of the most
property which allows us to print out all the keys in
important and useful ones.
a binary search tree in sorted order by the simple
Our main purpose in this work is to represent at recursive algorithm: the in-order tree walk. [19].
the same time the most used balanced binary
In what follows we define some concepts of the
trees: AVL and Red-Black trees. In other words, we
binary search tree:
propose common algorithms for the two data
structures. Only one parameter suffices to switch – A leaf or an external node is a node with no
between the two structures. The new children, while a unary (respectively a binary)
representation is a binary search tree partitioned node is a node with one child (respectively two
either in one class or in classes of heights 0 and 1. children). They denote the internal nodes.
Each class holds an AVL tree. When only one – The height of a node x: h(x) is the max of the
class exists, it generates predictably an AVL tree. height of its left and right children plus one in
Otherwise, the new structure is equivalent to a the case x is not a missing node, otherwise,
Red-Black tree with totally different and simple h(x) equals -1.
algorithms. One extra byte of storage allows
– The size of a node: s(x) is the number of its
representing both the kind and the height of
descendants including itself.
a node.
– The search process starts with the root. First
The rest of the paper is structured as follows: we compare the searched key with the root’s
section 2 introduces the tree terminology. Section key. Next we go to the left (respectively right)
3 describes our contribution. Section 4 presents sub-tree if the searched key is less
the maintenance operations while section 5 gives (respectively greater) than the root’s key. We
insertion and deletion algorithms of the proposed reach the end of the search when we find the
balanced tree. In the section 6 we give the desired key or we reach a missing node.
implementation of the structure, the results of – Update operations concern the insertion and
implementations and the discussion. Section 7 deletion. They are both preceded by a search
shows the applications of the new structure. Finally process. For the insertion, when a missing
section 8 makes a conclusion and looks forward to node is reached, it is replaced by a new node
the future research. with the key to insert. As for the deletion, the
process is more complicated since after the
search, multiple scenarios may arise. If the
2 Tree Terminology node is a leaf we replace it by a missing node,
and if it is a unary node we replace it by its
child. However if this node is an internal node,
We present here after some basic definitions used we find its in-order predecessor node or its in-
across the paper. order successor node and then we switch
The tree partitioning problem arises when 3 The Partitioned Binary Tree
information must be allocated to blocks of memory,
whose capacity is limited. 3.1 The Basic Idea
A variable H is considered as an integer – Rule 3: Every simple node must have a height
initialized at the beginning, and the variation of this equals to 0 or H - 2.
integer can give different kinds of balanced trees. – Rule 4: Every direct path from any node to a
In this work we focus on the main ones. When H = leaf must contain the same number of
2 the tree is considered as a Red-Black tree, and class nodes.
when the limit of H tends to the infinity the tree will
be an AVL. Indeed, there is only one class node This rule corresponds to having the same black
which is the root of the class. All the others are node in a Red-Black tree. We can notice that when
simple nodes. Consequently, in the definition the tree has only class nodes it is a perfect
above, by replacing simple nodes by red nodes balanced tree since any path from the root to a leaf
and class nodes by black ones, we obtain exactly has the same number of class nodes.
the definition of a Red-Black tree. Figure 2 shows an example of our
When the height of a class is equal to 0, this representation when H = 3, we can see that the
means the black node has not a red child. When height of every class is 2 or 1. Moreover every
the height of a class is equal to 1, this means that direct path from the root to a leaf has exactly
the black node has one or two red children. All the two classes.
simple nodes have 0 as height. Classes define
mathematically a partition on the tree. In other a) AVL Tree Case
words, the intersection of any two classes is empty
and the union of all the classes gives the
It is straightforward to observe that for H = ∞ the
whole tree.
new structure generates an AVL Tree.
Indeed, there is only one class node which is We can classify maintenance operations into two
the root of the class. All the others are simple categories: those that are applied inside a class
nodes. In practice, there is no limit for the height and those outside classes. Restructuring,
of the unique class and each path from any node AVL_tree_insert and AVL_tree_delete are
to a leaf contains the same number of class nodes operations of the first category. Operations of the
(0 or 1). second category are: Partitioning, Departitioning,
The minimum number of nodes in an AVL tree Restructuring-Partitioning and Transforming.
of code C with height k satisfies the recurrence:
4.1 Operations inside a Class
n0 = 1, n2 = 2, n3 = 4, nk = 1+ nk-1 + nk-2 for any k ≥
2. This requrence corresponds to Fibonachi trees, We give here after the various basic maintenance
nk = Fk+3 – 1. We have Fk+2 > Øk where Ø is the operations used to perform insertion and deletion
golden ratio [21]. algorithms on the new structure in terms of
operations defined above.
Fk+3 = 1+ Fk-2 + Fk-1 Fk+3 – 1= Fk-2 + Fk-1.
a) Restructuring
Fk+2 > Øk Fk+3 – 1 > Øk k < logØ n
k < 1.4404 log n
Restructuring consists simply in rebalancing the
tree after a tree property violation. It uses
b) Red-Black Case
Restructure operation which performs a rotation
and updates heights of the turned nodes.
It is also pretty straightforward to notice that for H A KindSwap operation can also be performed in
= 2 the tree generates a data structure equivalent Restructure operation. Furthermore, Restructure
to a Red-Black tree.
– The underflow class has a direct sister class As Partitioning, Departitioning does not require
(or its sibling node is a class node) with one or rotations and works in O(1) time.
two children. A Restructuring-Partitioning
operation is performed. c) Restructuring-Partitioning
If node A is a left child, then the right child of is performed, the process terminates. On the other
node A must be a class node with a height 0 or 1. hand, Partitioning can be in cascade.
A first single right rotation of node P is performed
in order to find a direct sister class of the underflow The insertion algorithm begins with the root of
class. Figure 8 (a2) is the result of the the overflow class (Root) and its parent node
Transforming process. Now, the underflow class (Parent). The parent is used to update links when
has a direct sister class and the process continues a restructuring is performed. Top operation gives
either with a Departitioning or a the root of the mother class and its parent node
Restructuring- Partitioning. without popping elements from stack Branch.
Recall that function AVL_INSERT(Root) adjusts
the balance of the class rooted at Root and returns
5 Insertion and Deletion Algorithms the new root of the class.
a) AVL Tree Case: If the root of the class is overflows. It is then partitioned to generate two
modified by the AVL_INSERT function, a other classes: 35 and 85. b) Node 70 becomes
restructuring is done (in AVL_INSERT) and a new leaf of class 20 and then overflows (class
then test (Root’ <> Save_Root) holds. Flag surrounded in red lines). As class 20 has one
Continue is set to False. As a consequence, child, it is structured (left rotation of node 20).
only one iteration of the Repeat loop is 30 becomes the new root.
performed. If the root of the class is not 9. Insert (24, 13): 24 is inserted in class 27 and 13
modified, test "Height2(Root’) = 2 and in class 16.
REDBLACK" fails since REDBLACK is false.
Furthermore, the stack is empty as there is no 5.2 Deleting an Existing Element
Restructuring in AVL_INSERT.
b) Red-Black Tree Case: Function AVL_INSERT An element is always removed from a leaf class.
is called at each new iteration. It inserts the root For the AVL tree case (Parameter RED_BLACK is
of the partitioned class. Recall that False), the process terminates. However, for the
AVL_INSERT performs at most one Red-Black tree case (Parameter RED_BLACK is
Restructuring. On the other hand, several True), if the height of this class became equal to -
Partitioning operations can be done. Indeed, 1, i.e. it underflows, the algorithm described below
each time a Partitioning is made, the root of the is applied. This consists in going upward the tree
partitioned class migrates to mother class from the removed node towards the root of the tree
which can be again partitioned if its height (Tree), by making one or several operations
reaches 2. among the following:
Modify node Grandparent to point Q departitioned classes is removed from the mother
Else Tree Q; class which can be again departitioned if its height
Pop(Branch, X); Push(Branch, Q); reaches -1.
Push(Branch, X; Grandparent Parent2 It is straightforward to observe that the deletion
If ( |(Height2(Root’) – Height2 (New_Sister )| algorithm works as follows:
> 1) If the underflow class has not a direct sister
Q RESTRUCTURING_ class (test "Kind(Sister) = Simple"), a Transforming
PARTITIONING (Parent, Dir) is first done to find its direct sister class. If the
If (Parent == Tree) Tree Q difference in heights between the underflow class
Else If (Grandparent <> Null) and its direct sister class exceeds one in absolute
Modify node Grandparent to point Q value, a Restructuring-Partitioning is performed.
Else Tree Q Otherwise a Departitioning is performed.
Continue = False
Else Scenario Example: Figure 8 shows step by step
Kind_Parent Kind (Parent) the deletion mechanism through an example when
DEPARTITIONING (Parent, Dir) parameter REDBLACK is true.
Top( Branch, Root, Parent, Grandparent)
1. Delete (70): 70 is replaced by 75 (its in-order
If (Kind_Parent = Simple) Pop (Branch) successor) and then this latter is removed
Else Continue False from class 84.
Until (Not Continue) 2. Delete (20): Again, 20 is replaced by 24(its in-
order successor) which is removed from
Comment and Analysis class 25.
The element is first removed from a class using the 3. Delete (16): 16 is removed from class 15.
AVL_DELETE function. 4. Delete (30): 30 is replaced by 40(its in-order
successor). Class 40 underflows and has a
a) AVL Tree Case direct sister class 84 with no children. They
are then departitioned into the new class 75.
5. Delete (25): 25 is removed and causes an
If the root of the class is modified by the underflow of class 25. Class 25 has a direct
AVL_DELETE function, one or two restructurings sister class with one child. A Restructuring-
are done and then test "(Root’ <> Save_Root) And Partitioning is performed (Right rotation of
(Root’ <> Null)" holds. Flag Continue is set to node 24 followed by a KindFlip)
False. As a consequence, only one iteration of the 6. Delete (40): 40 is replaced by 75(its in-order
Repeat loop is performed. If the root of the class is successor). 75 is removed from class 80. 80
not modified, test "Height2 (Root’) = -1 and
becomes the new root.
REDBLACK" fails since RED_BLACK is false.
7. Delete (80): 80 is removed and causes an
Furthermore, the stack is empty as there is no
underflow of class 80. Class 80 does not have
restructuring in AVL_DELETE.
a direct sister class. A Transforming is
completed by a right rotation of node 75. Now,
b) Red-Black tree case node 75 has at its left class 24 and at its right
class 80. A Departitioning of node 75 is
Function AVL_DELETE is called at each new then performed.
iteration. It always deletes a leaf inside a class. 8. Delete (75): 75 is removed from class 75. 24
This leaf becomes the root of departitioned becomes the new root.
classes. Recall that AVL_DELETE performs at 9. Delete (24): class 24 underflows. Classes 24
most two restructurings. and 1 are then departitioned in order to
On the other hand, several Departitioning generate the new class 15.
operations can be done. Indeed, each time a 10. Delete (15): 15 is removed from class 15. 1
Departitioning is made, the root of the becomes the new root.
6 Implementation End
First, simulation results confirm the superiority of Table 1. Results of the simulation tests
Red-Black trees (Column RB) compared to AVL
trees (Column AVL) in applications where N AVL RB Z_AVL Z_RB
insertions and deletions are very common. As an 100,000 187 140 140 141
example to insert / delete 100 000 elements in a
200,000 405 281 281 281
tree containing previously 500 000, AVL consumed
920 ms, while RB consumed 702 ms. 300,000 578 437 421 437
It is clear from the table above that the 400,000 734 546 546 562
performance of the Red-Black tree generated by 500,000 920 702 702 717
the new structure (Z_RB) gives the same results
as the standard Red-Black tree (Column RB).
It is surprising that the performance of the AVL implementation of the algorithms gives satisfying
tree generated by the new structure (Column results comparing to the previous propositions.
Z_AVL) is better than that of AVL trees (AVL). This Several applications of these combining algorithms
could be explained by the fact that the new are suggested like the real time systems,
structure uses the height in nodes while the especially trees in the priority queue for Dynamic
standard AVL uses the balance (0, +1 or -1). Data-Driven Application Systems: when the
Let us notice that the performance of the Red- system anticipates intensive search operations the
Black tree generated by the new structure (Z_RB) system will convert the tree to AVL. On the other
is comparable to the performance of the AVL tree side, when the system anticipates intensive update
generated by the new structure (Z_AVL) because operations it convert the tree to Red-Black.
we used the same code. Our work gives simple insertion and deletion
algorithms but its implementation requires a
storage of 8 bits in order to take in consideration
7 Applications both the height and the type of the node in one
way, and do not guarantee switching from one
This new structure can be applied in all structure to another in real time systems. A
applications where AVL and Red-Black trees are possible amelioration of this proposition is to define
used since it is equivalent to both structures and a method in order to allow the structure switching
gives very good execution times. However it can from one structure to another in a dynamic
be a very efficient structure for real time systems. environment.
In this context [22] demonstrates the usefulness of
using both AVL and Red-Black tree in the priority
queue in Dynamic Data-Driven Application References
Systems: when the system anticipates intensive
search operations, the system will convert the tree 1. Sedgewick, R. & Addison, W. (2002). Algorithms
to AVL, while when the system anticipates in Java, Parts 1-4. Professional, pp. 768.
intensive updates operations, it convert the tree to 2. Adelson-Velskii, M. & Landis, E.M. (1963). An
Red-Black. This transformation can be done easily algorithm for the organization of information. Dokl.
since we have the same code. Akad. Nauk SSSR 146, Vol. 3, pp. 1259–1262.
3. Foster, C.C. (1965). Information retrieval:
information storage and retrieval using AVL trees.
8 Conclusion and Future Work Proceedings 20th national conference (ACM), pp.
192–205. DOI: 10.1145/800197.806043.
In the current work we have described the 4. Foster, C.C. (1973). A generalization of AVL trees.
possibility to connect the two most useful and Communications of the ACM, Vol. 16, No. 8, pp.
intriguing balanced search trees, AVL and Red- 513–517. DOI: 10.1145/355609.362340.
Black trees in a simple way. This is accomplished 5. Bayer, R. (1971). Binary B-trees for virtual memory.
through a binary search tree partitioned into Proc ACM SIGFIDET Workshop, pp. 219–235.
classes that are in fact AVL sub-trees. The DOI:10.1145/1734714.1734731.
6. Bayer, R. (1972). Symmetric Binary B-Trees: Data tree. International Journal of Advance Research in
structure and maintenance algorithms. Acta Computer Science and Management Studies, pp.
Informatica, Vol. 1, No. 4. pp. 290–306. DOI: 191–194.
10.1007/BF00289509. 16. Sedgewick, R. (2008). Left-leaning red-black trees.
7. Brown, M. (1978). A storage scheme for height- Dagstuhl Workshop on Data Structures, pp. 4–10.
balanced trees. Inf. Process, pp. 231–232. 17. Wiener, R. (2005). Generic Red-Black Tree and its
8. Sedgewick, R. & Wayne, K. (2011). Algorithms 4 C# Implementation. Journal of Object Technology,
edition. Princeton University. Vol. 4, No. 2, pp. 59–80.
9. Guibas, L.J. & Sedgewick, R. (1978). A 18. Haeupler, B., Siddhartha, S., & Tarjan, R.E.
dichromatic framework for balanced trees. In 19th (2015). Rank-balanced trees. ACM Transactions on
Annual Symposium on Foundations of Computer Algorithms (TALG), Vol. 11, No. 4. DOI:
Science IEEE. DOI: 10.1109/SFCS.1978.3. 10.1145/2689412.
10. Andersson, A. (1993). Balanced search trees 19. Cormen, T.H. (2009). Introduction to algorithms.
made simple. Algorithms and Data Structures. MIT Press, pp. 1292.
Springer Berlin Heidelberg, pp. 60–71. DOI: 20. Lukes, J.A. (1974). Efficient Algorithm for
10.1007/3-540-57155-8_236. Partitioning of Trees. IBM J. Res. Develop.
11. Okasaki, C. (1999). Purely functional data 21. Knuth, D.E. (1973). The Art of Computer
structures. Cambridge University Press, pp. 1–203. Programming, Sorting and Searching. Addison-
12. Kahrs, S. (2011). Red-black trees with types. Wesley, Vol. 3.
Journal of functional programming, Vol. 11, No. 4, 22. Kumar, N.C., Vyas, S., Shidal, J.A., Cytron, R.,
pp. 425–432. DOI: 10.1017/S0956796801004026.
Gill, D.C., Zambreno, J., & Jonesa, P.H. (2012).
13. Rajeev, T. & Kumar, R. (2010). Balancing of AVL Improving system predictability and performance
tree using virtual node. RN 10 20. DOI: via hardware accelerated data structures. Proc.
10.5120/1331-1695. Computer Science, Vol. 9, pp. 1197–1205. DOI:
14. Chauhan, S., Thakur, S., & Rana, S. (2014). A brief 10.1016/[Link].2012.04.129.
study of balancing of AVL tree. International Journal
of Research 1, Vol. 11, pp. 406–408.
15. Mondal, G. (2014). A New Way of Inserting and Article received on 04/12/2017; accepted on 07/09/2018.
Deleting the Node To and From the AVL search Corresponding author is Lynda Bounif.