Balance Search Trees
Balance Search Trees
6.1 INTRODUCTION
A binary tree is balanced if for each node it holds that the number of inner nodes in
the left subtree and the number of inner nodes in the right subtree differ by at most 1.
A binary tree is balanced if for any two leaves the difference of the depth is at most 1.
These are all examples of balanced trees:
But the following are trees that are unbalanced, because the height of the left subtree is too
large compared to the right subtree:
• AVL trees
• red-black trees.
• B-trees
o 2-3 trees
o 2-3-4 trees
• Splay tree
• They can be used to construct and maintain ordered lists, such as priority queues.
• They can also be used for associative arrays; key-value pairs are inserted with an
ordering based on the key alone. In this capacity, self-balancing BSTs have a number
of advantages and disadvantages over their main competitor, hash tables.
• They can be easily extended to record additional information or perform new
operations. These extensions can be used, for example, to optimize database queries
or other list-processing algorithms.
The AVL tree, named after its inventors Georgy Adelson-Velski & Evgenii Landis, is a type
of self-balancing binary search tree. It was the first such data structure to be invented. In an
AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any
time they differ by more than one, rebalancing is done to restore this property. The tree re-
organises itself after every insert and delete operation.
Note: The difference between the heights of the left and right subtrees is called the balance
factor. It is calculated as follows:
An AVL tree uses a number of special tree transformations called rotations to ensure that the
correct balance is maintained after an insertion or deletion. These rotations are guaranteed to
respect the ordering properties of the BST. To balance itself, an AVL tree may perform the
following four kinds of rotations:
Example
In our example, node A has become unbalanced as a node is inserted in the right subtree of
A's right subtree. We perform the left rotation by making A the left-subtree of B.
2. Right Rotation
AVL tree may become unbalanced, if a node is inserted in the left subtree of the left subtree.
The tree then needs a right rotation.
Example
3. Left-Right Rotation
Double rotations are slightly complex version of already explained versions of rotations. To
understand them better, we should take note of each action performed while rotation. Let's
first check how to perform Left-Right rotation. A left-right rotation is a combination of left
rotation followed by right rotation.
State Action
A node has been inserted into the right subtree of the left subtree. This
makes C an unbalanced node. These scenarios cause AVL tree to
perform left-right rotation.
We shall now right-rotate the tree, making Bthe new root node of this
subtree. C now becomes the right subtree of its own left subtree.
4. Right-Left Rotation
The second type of double rotation is Right-Left Rotation. It is a combination of right
rotation followed by left rotation.
State Action
A node has been inserted into the left subtree of the right subtree. This
makes A, an unbalanced node with balance factor 2.
Search, insertion, and deletion all take O(log n) time in both the average and worst cases,
where n is the number of nodes in the tree prior to the operation. Insertions and deletions may
require the tree to be rebalanced by one or more tree rotations.
1. Insertion
The height of few nodes might get changed on inserting a new node into an AVL tree.
If AVL tree becomes unbalanced during the insertion operation, then we need to travel up the
tree from the newly created node until we find the first node X such that its grandparent Z is
• Step 1: Inset the new element into the tree using Binary Search Tree insertion logic.
• Step 2: After insertion , check the Balance Factor of every node.
• Step 3: If the Balance Factor of every node is 0 or 1 or -1 then go for next operation.
• Step 4: If the Balance Factor of any node is other than 0 or 1 or -1 then tree is said to
be imbalanced. Then perform the suitable rotation to make it balanced. And go for
next operation.
Example 1
Insert 3:
Insert 6:
Insert 2:
Insert 0:
Example 2
Insert 9, 27, 50, 15, 2, 21, and 36 into AVL tree
2. Deletion
AVL Deletion AVL deletion is not too different from insertion. The key difference here is
that unlike insertion, which can only imbalance one node at a time, a single deletion of a node
may imbalance several of its ancestors. Thus, when we delete a node and cause an imbalance
of that node’s parent, not only do we have to make the necessary rotation on the parent, but
we have to traverse up the ancestry line, checking the balance, and possibly make some more
rotations to fix the AVL tree. Fixing the AVL tree after a deletion may require making O(log
n) more rotations, but since rotations are O(1) operations, the additional rotations does not
affect the overall O(log n) runtime of a deletion.
• Step 3: Go from the deleted leaf towards the root and at each ancestor of that leaf:
Notes prepared by Ms. Peninah J. Limo Page 11
a. update the balance factor
b. rebalance with rotations if necessary.
Example 1
Delete the node 30 from the AVL tree shown in the following image.
Solution
In this case, the node B has balance factor 0, therefore the tree will be rotated by using R0
rotation as shown in the following image. The node B(10) becomes the root, while the node
A is moved to its right. The right child of node B will now become the left child of node A.
Example 2
Delete Node 55 from the AVL tree shown in the following image.
Example 3
Delete the node 60 from the AVL tree shown.
Solution
Deleting the node 60, disturbs the balance factor of the node 50 therefore, it needs to be R-1
rotated. The node C i.e. 45 becomes the root of the tree with the node B(40) and A(50) as its
left and right child.
In a Red-Black Tree, every new node must be inserted with the color RED. The insertion
operation in Red Black Tree is similar to insertion operation in Binary Search Tree. But it is
inserted with a color property. After every insertion operation, we need to check all the
properties of Red-Black Tree. If all the properties are satisfied then we go to next operation
otherwise we perform the following operation to make it Red Black Tree.
• Recolor
• Rotation
• Rotation followed by Recolor
The insertion operation in Red Black tree is performed using the following steps...
CASE 1: x’s parent is RED, and its uncle exists and is RED: Recolor the parent and uncles
to BLACK, recolor the grandparent to RED. Now the grandparent becomes the new x.
Repeat.
CASE 2: x’s parent is RED, it’s uncle is BLACK or does not exist, and x is a right child, and
it parent is a left child: Rotate-left at x, and transform to case 3.
Note: The symmetrical case where x is a left child, and it parent is a right child is handled
symmetrically: Rotate-right at x, and transform to case 3.
Note: The case where x is a right child and its parent is a right child is handled
symmetrically: rotate-left at grand-parent of x. Done.
Example 1
Insert 2, 1, 4, 5, 9, 3, 6, 7
Show the red-black trees that result after successively inserting the keys 41,38,31,12,19,8 into
an initially empty red-black tree.
Solution:
Example 3:
Solution
Tree is empty. So insert new Node as Root node Tree is not empty.So insert new node with red color.
with black color.
Tree is not Empty. So insert new node with red The tree is not empty. So insert new node with red color
color.
The tree is not empty. So insert new node with red The tree is not empty. So insert new node with re
color. color.
Insert (40)
Insert (80)
The tree is not empty. So insert new node with red color.
Build a red-black tree by inserting these nodes in the following order: 10, 85, 15, 70, 20, 60,
30, 50, 65, 80, 90, 40, 5, 55.
Solution
The deletion operation in Red-Black Tree is similar to deletion operation in BST. But after
every deletion operation, we need to check with the Red-Black Tree properties. If any of the
properties are violated then make suitable operations like Recolor, Rotation and Rotation
followed by Recolor to make it Red-Black Tree.
Example 1
Consider the red-black tree delete 20
In this example if either u or v is red, we mark the replaced child as black (No change in
black height). Note that both u and v cannot be red as v is parent of u and two consecutive
reds are not allowed in red-black tree.
Example 2
Example 3
B-trees are usually attributed to R. Bayer and E. McCreight who described the B-tree in a
1972 paper. By 1979, B-trees had replaced virtually all large-file access methods other than
hashing. B Tree is a specialized m-way tree that is widely used for disk access. A B-Tree of
order m can have at most m-1 keys and m children. One of the main reason of using B tree is
its capability to store large number of keys in a single node and large key values by keeping
the height of the tree relatively small. B-trees of order 4 are known as 2-3-4 trees, and a B-
tree of order 3 is known as a 2-3 tree.
The B-Tree is a flat tree i.e. the height of the B tree is kept to a minimum. Instead, as many
keys are put in each node of the B-tree. By keeping the height of the B-tree to the minimum,
the access is faster when compared to other balanced trees like AVL trees.
It is not necessary that, all the nodes contain the same number of children but, each node
must have m/2 number of nodes.
6.5.3 OPERATIONS
The search operation in B-Tree is similar to the search operation in Binary Search Tree. In a
Binary search tree, the search process starts from the root node and we make a 2-way
decision every time (we go to either left subtree or right subtree). In B-Tree also search
process starts from the root node but here we make an n-way decision every time. Where 'n'
is the total number of children the node has. In a B-Tree, the search operation is performed
with O(log n) time complexity. The search operation is performed as follows:
In a B-Tree, a new element must be added only at the leaf node. That means, the new
keyValue is always attached to the leaf node only. The insertion operation is performed as
follows...
1. 2-3-4 TREE
A 2-3-4 tree is a balanced search tree having following three types of nodes.
1. 2-node has one key and two child nodes (just like binary search tree node).
2. 3-node has two keys and three child nodes.
3. 4-node has three keys and four child nodes.
The reason behind the existence of three types is to make the tree perfectly balanced (all the
leaf nodes are on the same level) after each insertion and deletion operation.
Insertion
3. If that child is a leaf, insert the value into the child node and finish.
Example 1
Insert 50,30,10,70,60
Solution
Insert 50
Insert 30
Insert 10 and 70
Insert 60
Inserting 70 ...
Inserting 90 ...
Example 3
Insert the values 53, 27, 75, 25, 70, 41, 38, 16, 59, 36, 73, 65, 60, 46, 55, 33, 68, 79, and 48.
Case 1.1: If x is either in a 3-node or 4-node -Delete x. If the node is a 3-node, it becomes 2-
node and if the node is a 4-node, it becomes 3-node.
Case 1.2: If x is in a 2-node -This is called underflow. To resolve this, we need to consider
further three cases.
• Case 1.2.1: If the node containing x has 3-node or 4-node siblings -Convert the 2-
node into a 3-node by stealing the key from the sibling. This can be done by left or
right rotation. If the left sibling is a 3-node (or 4-node), do the left rotation otherwise
do the right rotation. The left rotation is illustrated in figure below. Here node with key
76 is being deleted.
The right rotation is symmetric. After the rotation, use case 1.1 to delete x.
• Case 1.2.2: If both the siblings are 2-node but the parent node is either a 3-node
or a 4-node - In this case, we convert the 2-node into a 4-node using the merge (or
fusion) operation. We merge the following three nodes.
After the fusion, the keys in the parent node decreases by 1. This is illustrated in figure
below where a 2-node containing 90 is being deleted.
1. Find the predecessor of the node containing x. The predecessor is always a leaf node.
2. Exchange the node containing x with its predecessor node. This moves the node
containing x to the leaf node.
3. Since x is in a leaf node, this is case 1. Use the rules given in case 1 to delete it.
Example 1: Leaf
Delete 2 Solution
Just delete the key .Make sure that a leaf is not empty after deleting a key.
When key deletion would create an empty leaf, borrow a key from leaf 's immediate siblings
(i.e. to the left and then right).
Example 3: Leaf
Delete 6 solution
If siblings are 2-nodes (no immediate sibling from which to borrow a key), steal a key from
our parent by doing the opposite of a split.
Example 4
if parent is a 2-node (one key) then Steal from siblings (parent’s) and Merge
Delete 7
Example 5:
if parent is a 2-node (one key) then Steal from siblings (parent’s) and Merge.
Delete 9
Delete the predecessor, and swap it with the node to be deleted. first delete 4, then swap 4 for
5.
Delete the predecessor, and swap it with the node to be deleted. first delete 1, then swap 1 for
2.
Key to delete may move.
2. 2-3 TREE
A 2-3 tree is a tree where a node can have 2 or 3 children nodes. A node with 2 children has
one key (data) on it and we call it a 2-node whereas a node with 3 children has two keys on it
and we call it a 3-node.
OPERATIONS
1. Search
An internal node in a 2–3 tree holds one key if it has two children (including two nil pointers)
and two if it has three children. A search that reaches a three-child node must compare the
target with both keys to decide which of the three subtrees to recurse into. As in binary trees,
these comparisons take constant time, so we can search a 2–3 tree in O(log n) time.
2. Insert
The insertion operation in a 2-3 is a little bit tricky. The insertion operation must make sure
that the tree remains perfectly balance after the insertion. To insert a node N in a 2-3 tree T,
we need to consider 2 cases as follows.
Example 1:
Insert 95,31,77,14,69,50,60 into a 2-3 tree.
solution
After the removal of the key value, if a leaf becomes empty, there are two possibilities:
1. If it has a 3-node sibling, a key is moved from the sibling to the parent, and the key in
the parent is moved into the empty leaf.
2. If it has no 3-node sibling, it is merged with a sibling with key from the parent. This
process may repeat at the parent. If the root become empty, it is removed.
In a 2-3 tree, all leaves are at the same level, and the complexity of major operations is O(log
n).
Example1
Consider deletion of the value 24:
This would leave the middle leaf empty, which is not allowed. However, the left sibling
contains an “extra” value. We may manage the deletion by “demoting and borrowing”:
We move the “separator”
value between the siblings
to the leaf, and move the
appropriate value from the
sibling to the parent…
Every operation on splay tree performs the splaying operation. For example, the insertion
operation first inserts the new element using the binary search tree insertion process, then the
newly inserted element is splayed so that it is placed at the root of the tree. The search
operation in a splay tree is nothing but searching the element using binary search process and
then splaying that searched element so that it is placed at the root of the tree.
In a splay tree, to splay any element we use the following rotation operations:
Example
In zig rotation every node moves one position to the right from its current position.
(ii) Zag Rotation(Left Rotation)
The Zag Rotation in a splay tree is similar to the single left rotation in AVL Tree rotations. In
Zag rotation every node moves one position to the left from its current position.
To make the zag rotation, we perform a left rotation at x’s parent node.
Example
Zig-Zig is a double rotation. We do a zig-zig rotation on x when x is a left child and x’s parent
node is also a left child. The zig-zig rotation is done by rotating x’s grandparent node to the
right followed by right rotation at x’s parent node.
Example
In zag-zig rotation node moves one position to the left followed by one position to the right
from its current position. The zig-zig case occurs when the accessed item is the left child of
its parent and the parent is also a left child (or vice versa). The parent is first rotated with the
grandparent, and then the accessed items’ node is rotated with its parent.
(iv) zag-zag
We do zag-zag rotation on x if x is a right child and x’s parent is also a right child. To
make the zag-zag rotation, we first do a left rotation at x’s grandparent and then do the left
rotation at x’s parent node.
Example
Zig-zag rotation is also a double rotation. We perform zig-zag rotation on x when x is a right
child and x’s parent is a left child. Zig-zag rotation is done by doing a left rotation at x’s
parent node followed by right rotating x grandparent (new parent) node.
1) Node is root We simply return the root, don’t do anything else as the accessed node is
already root.
2) Zig: Node is child of root (the node has no grandparent). Node is either a left child of root
(we do a right rotation) or node is a right child of its parent (we do a left rotation).
Notes prepared by Ms. Peninah J. Limo Page 46
Deletion
• First access the node to force it to percolate to the root.
• Remove the root.
• Determine the largest item in the left subtree and rotate it to the root of the left subtree
which results in the left subtree new root having no right child.
• The right subtree is then added as the right child of the new root. Insertion
• Starts the same as a BST with the new node created at the a leaf. The new leaf node is
splayed until it becomes the root.
Insertion
The insertion operation in Splay tree is performed using following steps:
• Step 1: Check whether tree is Empty.
• Step 2: If tree is Empty then insert the newNode as Root node and exit from the
operation.
• step 3: If tree is not Empty then insert the newNode as a leaf node using Binary
Search tree insertion logic.
• Step 4: After insertion, Splay the newNode
6.6.3 Advantages and Disadvantages of Splay tree
Advantages:
• Simpler coding than AVL trees
• Average amortized cost is roughly the same as a balanced BST.
• No balance bookkeeping information need be stored.
• Rotations on long access paths tend to reduce search cost in the future.
• Most accesses occur near the root minimizing the number of rotations.
Disadvantages:
• Height can degrade to be linear.
• Tree changes when only a search is performed. In a concurrent system multiple processes
accessing the same splay tree can result in critical region problems.