0% found this document useful (0 votes)
51 views16 pages

Unified AVL and Red-Black Tree Design

This document presents a unique representation of AVL and Red-Black trees that maintains the same time and space complexity while simplifying maintenance operations and algorithms for insertion and deletion. It introduces a partitioned binary tree structure that allows for a seamless transition between AVL and Red-Black trees using a single parameter, enhancing performance and implementation simplicity. The authors provide a comprehensive analysis of the proposed structure, including its algorithms and simulation results, confirming its efficiency compared to traditional tree structures.

Uploaded by

Tarif Islam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views16 pages

Unified AVL and Red-Black Tree Design

This document presents a unique representation of AVL and Red-Black trees that maintains the same time and space complexity while simplifying maintenance operations and algorithms for insertion and deletion. It introduces a partitioned binary tree structure that allows for a seamless transition between AVL and Red-Black trees using a single parameter, enhancing performance and implementation simplicity. The authors provide a comprehensive analysis of the proposed structure, including its algorithms and simulation results, confirming its efficiency compared to traditional tree structures.

Uploaded by

Tarif Islam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ISSN 2007-9737

Toward a Unique Representation for AVL and Red-Black Trees

Lynda Bounif, Djamel Eddine Zegour

Ecole Nationale Supérieure d’Informatique,


Laboratoire de la Communication dans les Systèmes Informatiques,
Algeria

{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

Computación y Sistemas, Vol. 23, No. 2, 2019, pp. 435–450


doi: 10.13053/CyS-23-2-2840
ISSN 2007-9737

436 Lynda Bounif, Djamel Eddine Zegour

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

Computación y Sistemas, Vol. 23, No. 2, 2019, pp. 435–450


doi: 10.13053/CyS-23-2-2840
ISSN 2007-9737

Toward a Unique Representation for AVL and Red-Black Trees 437

– Ass_Kind (P, A_Kind): Make A_kind the new


kind of node P
– Height(P): Height of simple node P inside the
class it belongs
– Height2(P): Height of class node P
– Ass_height (P, H): Make H the new height of
node P
Fig. 1. The right rotation of the node G
– Rotation(P, Dir): makes a left rotation around
between the node we found and the item we node P if Dir=1 and a right rotation if Dir=0. It
want to delete. returns the node that replaces P
– KindSwap(P, F): swaps kinds of nodes P and
– The restructuring operations allow us to F
maintain the binary tree balanced. We – KindFlip(P, F, Dir): is invoked after F=
generally use single or double rotations after Rotation(P, Dir). It transforms new children of
an update operation. See figure 1. F into class nodes and attributes to F the initial
kind of P.
2.2 The Partitioning Problem

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

Assume a tree T = (V, E). A partition of T is


defined as a collection of k clusters of nodes Ci The notion of partitioning a graph in a form of a tree
witch we name class, while i varies between 1 and is studied earlier by [20]. The application of the
algorithm can be: in the allocation of computer
K, such that: ⋃𝑘𝑖=1 𝐶𝑖 = 𝑉Ci ∩ Cj = ∅
information to physical storage space or in finding
As a result, the union of the sub-trees gives the a suboptimal partition of any connected graph.
whole tree and the intersection of two given sub-
trees is null.
An edge (i, j) of T is said to be cut by a partition The proposed work is a binary search tree
of T if nodes i and j are in different clusters. partitioned in classes. Each class is in fact a sub
An optimal partition of T: Pt(𝑜𝑝𝑡) = {C1, C2, tree holding an AVL tree of height H or H-1. The
C3,… Ck} root node of this sub-tree is a class node; the other
nodes are simple. Furthermore, the new structure
Is one in which each cluster Ci satisfies the
is perfectly balanced considering only class nodes.
weight constraint:

∑ 𝑊𝑗 ≤= 𝑊 Beside the data field, a node contains a byte


𝑗∈𝐶𝑗 called a code to designate both its kind and its
height. The height of a node is in fact the depth of
2.3 Basic Operations the sub-tree rooted at this node inside the class it
belongs. The storage of a byte in a node delivers a
Here after are some basic operations used in the range of benefits:
representation of our proposed tree:
– To minimize the number of the requirements
– Lc(P): Left child of node P for a unique framework
– Rc(P): Right child of node P – To detect easily the type of the balanced
– Ass_Rc(P, Q): Make Q a right child of node P tree used
– Ass_Lc(P, Q): Make Q a left child of node P – To trigger the restructuring operations after
– Kind(P): Kind of node P an update

Computación y Sistemas, Vol. 23, No. 2, 2019, pp. 435–450


doi: 10.13053/CyS-23-2-2840
ISSN 2007-9737

438 Lynda Bounif, Djamel Eddine Zegour

– To control and detect the type of a node at any


time in the tree for the operations inside and
outside a class.

3.2 The Rules

In this section we give formally the requirements


that a binary tree must have in order to be
considered as a partitioned tree, we emphasis on
the fact that these requirements must be satisfied
for AVL and Red-Black tree case on the globalism,
not a separate rule for each type like the case of
[18]. In consequence we have the same
representation of both AVL and Red-Black tree.
Formally, the new structure should respect
these four rules:
– Rule 1: Every node can be either a simple or a Fig. 2. The representation of the new structure H = 3
class node.
In order to simplify the presentation of the trees
We describe two categories of nodes: simple in the figures below, class nodes are represented
and class nodes. The class node encloses a sub- inside blue squares and simple nodes inside
tree of simple nodes of height one or infinity circles. Furthermore, classes are surrounded.
of nodes. Figure 3(a) shows the new structure as a
structure equivalent to an AVL tree. There is only
– Rule 2: Every class must have a height equals one class containing an AVL tree. Values under the
to H - 1 or H - 2. nodes designate the height of the nodes.

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.

Computación y Sistemas, Vol. 23, No. 2, 2019, pp. 435–450


doi: 10.13053/CyS-23-2-2840
ISSN 2007-9737

Toward a Unique Representation for AVL and Red-Black Trees 439

The subtree rooted at any node n has at least


2bh(n) - 1 internal nodes.
If N is nil, then its height is 0. For the inductive
step, we consider an internal node x with two
children having black-height of bh(n) or bh(n) – 1
depending on its color. Considering ch(n) the child,
applying the hypothesis, it has at least 2bh(n) – 1 - 1
internal nodes. Thus the subtree rooted by n
contains at least: 2bh(n) - 1 - 1 + 2bh(n) - 1 - 1 + 1 = 2bh(n)
– 1 internal nodes [19].
We know also that at least half the nodes on
any simple path from the root to a leaf, not
including the root, must be black. Consequently,
the black-height of the root must be at least H/2;
thus, n > 2h/2 – 1  H < [Link] (n + 1)
Lemma 3.1: The maximum height of the
partitioned tree is 2 Log n

Proof: The minimum number of any node in PBT


tree of height H satisfies the recurrence: n0 = 1, n2
= 2, n3 = 4, nk = 2* nk-2 + 1 for any k ≥ 2.

By induction nk ≥ 2H/2 which gives: h ≤ 2 log nk.

Fig. 3. The new structure as an AVL tree 4 Maintenance Operations

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.

Computación y Sistemas, Vol. 23, No. 2, 2019, pp. 435–450


doi: 10.13053/CyS-23-2-2840
ISSN 2007-9737

440 Lynda Bounif, Djamel Eddine Zegour

Fig. 4. The new structure as a Red-Black tree

Fig. 5. The Partitioning operation

can be preceded by a Reverse_balance operation Begin


which consists in reversing the balance of a F  Rotation (P, Dir)
given: node. Max1 = Max( Height(Lc(P)), Height(Rc(P)) )
Ass_Height(P, Max1 + 1)
Function Restructuring (PBT P, int Dir): PBT
Max2 = Max( Height(Lc(F), Height(Rc(F)) )
Variables
Ass_Height(F, Max2 + 1)
S: PBT
If (REDBLACK Or (Kind (P) = Class) )
Begin
KindSwap(P, F)
If(Dir = 0)
Return F
If(Height(Rc(Rc(P)))–Height( Lc(Rc(P)))=1)
END
Ass_Rc(P,Reverse_Balance(Rc (P),1 ))
S  Restructure (P, 0) Function Reverse_Balance (PBT P, int Dir): PBT
Else If(Dir = 1) Variables
If(Height (Rc(Lc(P)))–Height(Lc(Lc(P)))= 1) F: PBT
Ass_Lc (P,Reverse_Balance (Lc (P), 0 )) Begin
S  Restructure(P, 1) F  Rotation (P, Dir)
Return S Max1 = Max(Height (Lc(P)),Height(Rc(P)) )
End Ass_Height(P,Max1 + 1)
Function Restructure (PBT P, int Dir): PBT Max2 = Max( Height (Lc(F), Height(Rc(F)) )
Variables Ass_Height(F, Max2+ 1)
F: PBT Return F
Max1, Max2: INT End

Computación y Sistemas, Vol. 23, No. 2, 2019, pp. 435–450


doi: 10.13053/CyS-23-2-2840
ISSN 2007-9737

Toward a Unique Representation for AVL and Red-Black Trees 441

b) AVL Tree Insert restructured. The process can continue upward


the tree.
AVL tree insert algorithm uses the Restructuring
operation defined above to rebalance the tree each Function Avl_Delete (PBT Root)
time the tree becomes unbalanced in the sense of Begin
AVL trees. In our purpose, the algorithm is Avl_Delete  Root
expressed with the height instead of balance in Continue  True
each tree node. Repeat
This algorithm is applied when an item is Pop (Branch, P)
inserted into the tree. Stack Branch holds the path
traversed by search process from the tree root Kind_P  Kind_ (P)
toward the parent of the new inserted node. Nodes Save_height  Height (P)
are popped in order to update their height fields. If Update P’s height
the balance of a node becomes (in absolute value) If ( |Height (Lc(P)) - Height (Rc) | > 1 )
greater than 1, the tree is restructured and the If ( Height (Lc (P)) > Height (Rc (P)) )
process is stopped. The algorithm is the following: Q  Restructuring (P, 1)
Function Avl_insert (PBT Root) Else Q  Restructuring (P, 0)
Begin If (Kind_P = Simple)
Avl_Insert  Root Parent  Top (Branch)
Continue  True
Modify node Parent to point now Q
Repeat
Else Avl_Delete  Q
Pop(Branch, P)
Kind_P  Kind (P) PQ
Update P’s height Until (Not Save_height – Height(P) = 0) Or
If ( |Height (Lc (P)) - Height (Rc (P)) | > 1) (Kind_P = Class))
If ( Height (Lc (P)) > Height (Rc (P)) ) End
Q  Restructuring (P, 1)
Else Q  Restructuring (P, 0) 4.2 Operations Outside a Class
If (Kind_P = Simple)
Pop (Branch, Parent) Naturally, operations between classes concern
Modify node Parent to point Q only the structure equivalent to a Red-Black tree.
Else Avl_Insert  Q
Continue  False During the process of insertion, an item is
Else Continue  (Kind_P = Class) always inserted into a leaf class. The class can
Until (Not Continue) overflow, i.e. its height reaches 2. A Restructuring
End is performed if the class has only one child.
Otherwise, a Partitioning operation is performed.
c) AVL_tree_delete During the process of deletion, an item is
always removed from a leaf class. The leaf class
can underflow, i.e. its height reaches -1. Several
AVL_tree_delete algorithm also uses cases occur:
Restructuring operation. The algorithm below is
applied when an item is deleted from a leaf class – The underflow class has not a direct sister
rooted at Root. Stack Branch holds the path class (or its sibling node is a simple node). A
traversed by the search process from the root of Transforming operation is performed.
the entire tree toward the parent of the deleted – The underflow class has a direct sister class
node. Nodes are popped in order to update their (or its sibling node is a class node) with no
height fields. If the balance of a node becomes (in child. A Departitioning operation is performed.
absolute value) greater than 1, the tree is

Computación y Sistemas, Vol. 23, No. 2, 2019, pp. 435–450


doi: 10.13053/CyS-23-2-2840
ISSN 2007-9737

442 Lynda Bounif, Djamel Eddine Zegour

Fig. 6. The Departitioning operation

Fig. 7. The Restructuring-Partitioning operation

Computación y Sistemas, Vol. 23, No. 2, 2019, pp. 435–450


doi: 10.13053/CyS-23-2-2840
ISSN 2007-9737

Toward a Unique Representation for AVL and Red-Black Trees 443

Fig. 8. The Transforming operation

– 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

We describe henceforth, all the operations Restructuring-Partitioning is undertaken when a


mentioned. class underflows, its direct sister class exists and
has one or two children. Such situations are
a) Partitioning depicted in figures 7(a1), 7(b1) and 7(c1) where
the underflow class is to the right of node P. As
It consists in transforming one class into two node P can be a simple or class node, it is
classes. In Figure 5(a1), after an insertion represented inside a triangle. The conflict is first
operation in class Z, this is partitioned since its solved by possibly applying a Reverse_Balance
height reaches 2. Dashed lines correspond to the operation on the sister class (a simple rotation).
four possible cases. Node Z becomes a simple Second, a Restructure_Partition operation is
node and its two children X and Y become class performed. It performs a rotation, a KindFlip
nodes (Figure 5(a2)). Node Z becomes thus a new operation and updates heights of the concerned
leaf in the mother class. nodes. Figures 7(a2) and 7(b2) are new situations
This operation corresponds simply to the of Figures 7(a1) and 7(b1). Nodes C and P become
modification of three nodes’ kinds. Partitioning class nodes and the kind of node X after the
does not require rotations and is done in O(1). rotation is the one of P before the rotation. As node
X had already a left child, a Reverse_Balance
b) Departitioning operation is not necessary. For Figure 7(c1), a
reversing of balance of class X is first performed.
In Figures 6(a1) and 6(b1), after a delete operation, As a consequence, the result is depicted in
the height of class Y becomes -1 while its direct Figure 7(c2).
sister class has a height equal to 0. A
Departitioning operation holds. Node Z is deleted d) Transforming
from the mother class as depicted in Figures 6(a2)
and 6(b2). Figure 6(b2) shows a situation where Recall that Transforming occurs when a class
the conflict is not yet resolved. This means that the underflows while it does not have a direct sister
process continues since the mother class has no class (the sibling node is simple).
child. Two nodes’ kinds will be modified: the parent It is the case of Figure 8(a1) where the
node and its child. The parent node becomes a underflow class is Y and its direct sister class is A.
class node while its child becomes a simple node. P is their parent node.

Computación y Sistemas, Vol. 23, No. 2, 2019, pp. 435–450


doi: 10.13053/CyS-23-2-2840
ISSN 2007-9737

444 Lynda Bounif, Djamel Eddine Zegour

Fig. 9. A step by step insertion algorithm

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.

Once the maintenance operations are presented,


we can now give the algorithms of insertion and Repeat
deletion of the new structure. Save_Root  Root
Root’  AVL_INSERT (Root)
5.1 Inserting a New Element If (Root’ <> Save_Root)
If (Parent <> Null)
In the insertion process an element is always Modify node Parent to point now Root’
added into a leaf class. In AVL tree case Else Tree  Root’
(Parameter REDBLACK = False), the process Continue  False
terminates. In Red-Black tree case (Parameter ElseIf (Height2 (Root’) = 2 and REDBLACK)
REDBLACK = True), if the height of this class PARTITIONING (Root’)
becomes 2, the algorithm described below is Top (Branch, Root, Parent)
applied. It uses a stack containing all the nodes Until (Empty (Branch)) or (Not continue)
traversed from the root (Tree) of the entire tree until
the parent of the newly inserted node. The Comment and Analysis
algorithm goes upward the tree from the inserted
The element is first inserted inside a class using
node towards the root of the tree by making either
the AVL_INSERT method.
Restructuring or Partitioning. When Restructuring

Computación y Sistemas, Vol. 23, No. 2, 2019, pp. 435–450


doi: 10.13053/CyS-23-2-2840
ISSN 2007-9737

Toward a Unique Representation for AVL and Red-Black Trees 445

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:

Scenario Example: Figure 7 shows step by step – Departitioning


the construction mechanism through an example – Transforming
when parameter REDBLACK is true, i.e. H = 2. – Restructuring-Partitioning

1. Insert (70): a class is created with one element. When a Restructuring-Partitioning is


2. Insert (20): 20 is inserted into class 70. performed, the process stops. On the other hand,
3. Insert (16): 16 is inserted into class 70 and Departitioning can be in cascade. Transforming is
causes a restructuring (right rotation of node performed only one time but the
70). 20 becomes the root of the class. Indeed, process continues.
class 70 overflows while it has one child. The algorithm uses a stack containing all the
4. Insert (30): 30 is inserted into class 20 and nodes traversed from the root until the parent of the
causes a Partitioning since class 20 overflows newly removed node.
while it has two children. It has as input the root of the underflow class
5. Insert (27): 27 is inserted to the left of node 30 (Root), its parent node (Parent) and its
and this causes a restructuring (right rotation of grandparent node (Grandparent). Parent is used to
node 70). update links when a restructuring is performed.
6. Insert (35): 35 augments the height of class 30 Grandparent is used when a maintenance
and this is balanced in the sense of an AVL tree. operation is performed. Top operation gives the
As this class overflows, it is partitioned to root of the mother class, its parent node and its
generate two other classes: 27 and 70. 30 is grandparent node without popping elements from
transformed into a simple node and belongs stack Branch.
now to the mother class 20. Recall that function AVL_DELETE(Root)
7. Insert (85): 85 is inserted into class 70 as a right adjusts the balance of the class rooted at Root and
child. returns the new root of this class.
8. Insert (75): This case is represented by two
trees (75a and 75b). a) 75 is inserted into class Repeat
70 and augments its height. Class 70 is Save_Root  Root
balanced in the sense of an AVL tree but it Root’  AVL_DELETE(Root)

Computación y Sistemas, Vol. 23, No. 2, 2019, pp. 435–450


doi: 10.13053/CyS-23-2-2840
ISSN 2007-9737

446 Lynda Bounif, Djamel Eddine Zegour

Fig. 10. A step by step deletion algorithm

If (Root’ <> Save_Root) And (Root’ <> Null) Else


If (Parent <> Null) Sister  Lc (Parent)
Modify node Parent to point now Root’ Dir  1
Else Tree  Root’ If (Kind (Sister) = Simple)
Continue  false Parent2  Sister
Else If ((Height2(Root’)= -1) And REDBLACK) If (Dir = 1) New_Sister  Rc (Sister)
If (Lc(Parent)= Root’) Else New_Sister  Lc( Sister )
SisterRc(Parent) Q  TRANSFORMING (Parent, Dir)
Dir  0; If (Grandparent <> Null)

Computación y Sistemas, Vol. 23, No. 2, 2019, pp. 435–450


doi: 10.13053/CyS-23-2-2840
ISSN 2007-9737

Toward a Unique Representation for AVL and Red-Black Trees 447

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.

Computación y Sistemas, Vol. 23, No. 2, 2019, pp. 435–450


doi: 10.13053/CyS-23-2-2840
ISSN 2007-9737

448 Lynda Bounif, Djamel Eddine Zegour

6 Implementation End

6.1 Description Function Height (A: Ptr_node): integer;


Begin
The new balanced tree has one Boolean If (A=nil) Height  -1
parameter: RED_BLACK. If this parameter is true, Else If ([Link] >= 128) // Class node
we are in the case of a Red-Black tree structure. Height  -1
Else, it is about an AVL tree. This parameter is Else Height  [Link] Mod 128
used in both insertion and deletion algorithms, as End
well as in maintenance operations.
The new structure uses one additional byte in Procedure Ass_Height (P: Ptr_node; H: integer);
every node. Bit 1 is set to 1 if the node is a class Begin
node. Otherwise this bit is set to 0. Bits 2 to 8 hold If ([Link] < 128) [Link]  H
node height. In this way, Height(Code) is simply Else [Link]  128 + H
Code Mod 128. Moreover, if Code ≥ 128 then it is End
a class node. Otherwise, it is a simple node.
6.3 Experimental Tests
6.2 Data Structure for the Proposed Tree
We considered the following experiment:
We give hence the pseudo code of the 1. Build a Red-Black tree (RB), an AVL tree (AVL)
proposed tree. and the two new binary search trees generated by
the new structure (Z_AVL and Z_RB) with a same
Type TypeNode = (Simple, Class) sequence (S1) of N random integer values.
Type Ptr_node = * T_node 2. Build a random sequence (S2) of about N
T_node  record insertion and removal operations.
Begin 3. (A) - Run sequence S2 separately on each data
Data: Anykind structure.
Code: Byte (B) - Compute:
Lc, Rc: Ptr_node – The total number of rotations.
End – The execution time made by both the insertion
and removal algorithms in each kind of trees.
Function Kind (A: Ptr_node): Typenode
Begin 4. Repeat 1 – 3 three times for N = 100 000 to 500
If ([Link] >= 128) 000 by step of 100 000 nodes.
Kind  Class
Else Kind  Simple 6.4 Results
End
We have not shown the numbers of rotations
Procedure Ass_kind(A:Ptr_node, A_kind: performed by each data structure. These have
Typenode) been computed only to verify correctness. As
Begin expected, we obtained the same number of
If (A_kind = Simple) rotations in AVL and Z_AVL as well as in RB and
[Link] [Link] Mod 128 Z_RB. We focused then our attention only on the
Else [Link] = 128 + [Link] Mod 128; execution time.
End Table 1 shows in columns "AVL","RB", "Z_AVL"
and "Z_RB" the execution times in milliseconds
Function Height2 (A: Ptr_node): integer; taken by each data structure. Column N denotes
Begin the size of trees initially generated as well as the
If (A = nil) Height2  -1 number of inserted/ deleted operations. Values
Else Height2  [Link] Mod 128 denote the average values of three tests.

Computación y Sistemas, Vol. 23, No. 2, 2019, pp. 435–450


doi: 10.13053/CyS-23-2-2840
ISSN 2007-9737

Toward a Unique Representation for AVL and Red-Black Trees 449

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.

Computación y Sistemas, Vol. 23, No. 2, 2019, pp. 435–450


doi: 10.13053/CyS-23-2-2840
ISSN 2007-9737

450 Lynda Bounif, Djamel Eddine Zegour

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.

Computación y Sistemas, Vol. 23, No. 2, 2019, pp. 435–450


doi: 10.13053/CyS-23-2-2840

You might also like