0% found this document useful (0 votes)
27 views47 pages

Balance Search Trees

Uploaded by

beastsnokie
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)
27 views47 pages

Balance Search Trees

Uploaded by

beastsnokie
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

6.

0 BALANCED BINARY 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:

There are two basic ways used to keep trees balanced.


• Use tree rotations.

Notes prepared by Ms. Peninah J. Limo Page 1


• Allow nodes to have more than two children.
Searching into a balanced tree is significantly faster than searching into a non-
balanced tree.
There are many kinds of balanced search trees. Some of the more common types are:

• AVL trees
• red-black trees.
• B-trees
o 2-3 trees
o 2-3-4 trees
• Splay tree

6.2 Self-Balancing BST Applications:

• 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.

6.3 AVL TREE

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.

6.3.1 Properties Of AVL Tree

An AVL tree must have the following properties:

Notes prepared by Ms. Peninah J. Limo Page 2


• In an AVL tree, the heights of the two child subtrees of any node differ by at most
one; therefore, it is also said to be height-balanced.
• Lookup, 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.
• Insertions and deletions may require the tree to be rebalanced by one or more tree
rotations.
• The balance factor of a node is the height of its right subtree minus the height of its
left subtree and a node with a balance factor 1, 0, or -1 is considered balanced.

Note: The difference between the heights of the left and right subtrees is called the balance
factor. It is calculated as follows:

Balance factor =abs(height(left subtree) – height(right subtree))

6. 3.2 AVL Rotations

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:

• Left Rotation (LL Rotation)

• Right Rotation (RR Rotation)

• Left-Right Rotation (LR Rotation)

• Right-Left Rotation (RL Rotation)


The first two rotations are single rotations and the next two rotations are double rotations. To
have an unbalanced tree, we at least need a tree of height 2.

Notes prepared by Ms. Peninah J. Limo Page 3


1. Left Rotation
If a tree becomes unbalanced, when a node is inserted into the right subtree of the right
subtree, then we perform a single left rotation.

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.

Left rotation when the tree is heavier on the right:


Left rotation around X:

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

Notes prepared by Ms. Peninah J. Limo Page 4


As depicted, the unbalanced node becomes the right child of its left child by performing a
right rotation.

Right rotation when tree is heavier on the left


Right rotation around X
X moves down and to the right, into the position of
its right child . X's right subtree is unchanged.
X's left child A moves up to take X's place. X is
now A's new right child.
This leaves the old right child of A unaccounted for.
Since it comes from the left of X, it is less than X.
So it becomes X's new left child.

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.

Notes prepared by Ms. Peninah J. Limo Page 5


We first perform the left rotation on the left subtree of C. This
makes A, the left subtree ofB.

Node C is still unbalanced, however now, it is because of the left-


subtree of the left-subtree.

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.

The tree is now balanced.

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.

Notes prepared by Ms. Peninah J. Limo Page 6


First, we perform the right rotation along C node, making C the right
subtree of its own left subtree B. Now, B becomes the right subtree
of A.

Node A is still unbalanced because of the right subtree of its right


subtree and requires a left rotation.

A left rotation is performed by making B the new root node of the


subtree. A becomes the left subtree of its right subtree B.

The tree is now balanced.

6.3.3 OPERATIONS OF AVL

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

Notes prepared by Ms. Peninah J. Limo Page 7


unbalanced node and we need to re-balance or reorganize it using either single rotation or
double rotation.

The insertion operation is performed as follows:

• 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

Suppose we start with the empty tree.

Insert 3:

Insert 6:

Insert 2:

So far things have been easy! Insert 1:

Insert 0:

Notes prepared by Ms. Peninah J. Limo Page 8


The subtrees under `2' are not height
balanced hence a rotation is needed. `1'
rotates up into `2's position, `2' goes down on
the opposite side:

Example 2
Insert 9, 27, 50, 15, 2, 21, and 36 into AVL tree

Notes prepared by Ms. Peninah J. Limo Page 9


Example 3
Insert the sequence 50, 25, 10, 5, 7, 3, 30, 20, 8, 15 into AVL tree

Notes prepared by Ms. Peninah J. Limo Page 10


Class exercise
Insert into an initially empty AVL tree items with the following keys (in this order). Draw the
resulting AVL tree ¡ 30, 40, 24, 58, 48, 26, 11, 13

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.

The deletion operation is performed as follows:

• Step 1: find the node x where k is stored


• Step 2: delete the contents of node x .
Claim: Deleting a node in an AVL tree can be reduced to deleting a leaf . There are three
possible cases (just like for BSTs):
a. If x has no children (i.e., is a leaf), delete x.
b. If x has one child, let x' be the child of x. Notice: x' cannot have a child, since
subtrees of T can differ in height by at most one replace the contents of x with the
contents of x'delete x' (a leaf)
c. If x has two children, find x's successor z (which has no left child) replace x's
contents with z's contents, and delete z. [since z has at most one child, so we use
case (1) or (2) to delete z].In all 3 cases, we end up removing a leaf.

• 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.

Notes prepared by Ms. Peninah J. Limo Page 12


Solution
Deleting 55 from the AVL Tree disturbs the balance factor of the node 50 i.e. node A which
becomes the critical node. This is the condition of R1 rotation in which, the node A will be
moved to its right (shown in the image below). The right of B is now become the left of A
(i.e. 45).

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.

Notes prepared by Ms. Peninah J. Limo Page 13


3. SEARCHING
Searching in an AVL tree is done as in any binary search tree. The special thing about AVL
trees is that the number of comparisons required, i.e. the AVL tree's height, is guaranteed
never to exceed log(n).

Advantages of AVL trees:


i. Search is O(log N) since AVL trees are always balanced.
ii. Insertion and deletions are also O(logn)
iii. The height balancing adds no more than a constant factor to the speed of insertion.
Disadvantages of AVL trees:
i. Difficult to program & debug; more space for balance factor.
ii. Asymptotically faster but rebalancing costs time.
iii. Most large searches are done in database systems on disk and use other structures
(e.g. B-trees).
iv. May be OK to have O(N) for a single operation if total run time for many consecutive
operations is fast (e.g. Splay trees).

6.4 RED-BLACK TREES


A red-black tree is a self-balancing binary search tree invented by Rudolf Bayer in 1972.
Red-black tree is used to implement the associative array and set because it is the most
efficient data structure of all self-balancing binary search trees. Linux system and many C++
STL, such as map and multimap, apply this data structure to implement the data structure for
searching.
A red-black tree is a binary search tree with one extra bit of storage per node: its
color, which can be either RED or BLACK. By constraining the way nodes can be colored on

Notes prepared by Ms. Peninah J. Limo Page 14


any path from the root to a leaf, red-black trees ensure that no such path is more than twice as
long as any other, so that the tree is approximately balanced.
Red-black tree is better at insertion and deletion than AVL tree owing to its fewer
rotations; lookup in red-black tree is slower than that of AVL tree because that AVL tree
maintains a more rigid balance than red-black tree does.
6.4.1 Properties of Red-black trees
A red-black tree is a BST with the following properties:

1. Every node is colored red or black.


2. Every leaf is a NIL node, and is colored black.
3. If a node is red, then both its children are black.
4. Every simple path from a node to a descendant leaf contains the same number of
black nodes.
5. The root is black.

6.4.2 OPERATIONS OF RED-BLACK TREES


1. Insertion

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...

• Step 1 - Check whether tree is Empty.


• Step 2 - If tree is Empty then insert the newNode as Root node with color Black and
exit from the operation.
• Step 3 - If tree is not Empty then insert the newNode as leaf node with color Red.

Notes prepared by Ms. Peninah J. Limo Page 15


• Step 4 - If the parent of newNode is Black then exit from the operation.
• Step 5 - If the parent of newNode is Red then check the color of parent node's sibling
of newNode.
• Step 6 - If it is colored Black or NULL then make suitable Rotation and Recolor it.
• Step 7 - If it is colored Red then perform Recolor. Repeat the same until tree becomes
Red Black Tree.

Recolor and structuring

Do following if color of x’s parent is not BLACK or x is not root.

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.

Notes prepared by Ms. Peninah J. Limo Page 16


CASE 3: x’s parent is RED, it’s uncle is BLACK or does not exist, and x is a left child, and
its parent is a left child: rotate-right at grandparent of x. Done.

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

Notes prepared by Ms. Peninah J. Limo Page 17


Example 2:

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:

Notes prepared by Ms. Peninah J. Limo Page 18


Insert (41) Insert (38)

Insert (31) Insert (12)

Insert (19) Insert (8)

Example 3:

Create a Red-black tree by inserting following sequence of numbers 8,18,5,15,17,25,40 and


80.

Solution

Insert (8) Insert (18)

Tree is empty. So insert new Node as Root node Tree is not empty.So insert new node with red color.
with black color.

Notes prepared by Ms. Peninah J. Limo Page 19


Insert(5) Insert (15)

Tree is not Empty. So insert new node with red The tree is not empty. So insert new node with red color
color.

Insert (17) Insert (25)

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)

Notes prepared by Ms. Peninah J. Limo Page 20


The tree is not empty. So insert new node with red color.

Insert (80)

The tree is not empty. So insert new node with red color.

Notes prepared by Ms. Peninah J. Limo Page 21


Example 4:

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

Notes prepared by Ms. Peninah J. Limo Page 22


Deletion Operation

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

Notes prepared by Ms. Peninah J. Limo Page 23


6.5 B-TREE

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.

6.5.1 Properties of B-Tree


B-trees have the following properties:
1. Every node in a B-Tree contains at most m children.
2. Every node in a B-Tree except the root node and the leaf node contain at least m/2
children.
3. The root nodes must have at least 2 nodes.
4. All leaf nodes must be at the same level.

It is not necessary that, all the nodes contain the same number of children but, each node
must have m/2 number of nodes.

Notes prepared by Ms. Peninah J. Limo Page 24


6.5.2 Structure of B-tree

6.5.3 OPERATIONS

a) Search Operation in B-Tree

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:

• Step 1 - Read the search element.


• Step 2 - Compare the search element with first key value of root node in the tree.
• Step 3 - If both are matched, then display "Given node is found!!!" and terminate the
function.
• Step 4 - If both are not matched, then check whether search element is smaller or
larger than that key value.
• Step 5 - If search element is smaller, then continue the search process in left subtree.
• Step 6 - If search element is larger, then compare the search element with next key
value in the same node and repeate steps 3, 4, 5 and 6 until we find the exact match or
until the search element is compared with last key value in the leaf node.
• Step 7 - If the last key value in the leaf node is also not matched then display
"Element is not found" and terminate the function.

Notes prepared by Ms. Peninah J. Limo Page 25


b) Insertion Operation in B-Tree

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...

• Step 1 - Check whether tree is Empty.


• Step 2 - If tree is Empty, then create a new node with new key value and insert it into
the tree as a root node.
• Step 3 - If tree is Not Empty, then find the suitable leaf node to which the new key
value is added using Binary Search Tree logic.
• Step 4 - If that leaf node has empty position, add the new key value to that leaf node
in ascending order of key value within the node.
• Step 5 - If that leaf node is already full, split that leaf node by sending middle value
to its parent node. Repeat the same until the sending value is fixed into a node.
• Step 6 - If the spilting is performed at root node then the middle value becomes new
root node for the tree and the height of the tree is increased by one.

c) Deletion Operation in B-Tree


Just like insertion, the deletion of the key is also carried out at leaf nodes level. But unlike
insertion, deletion is more complicated. The key to be deleted can be either a leaf node or an
internal node.

To delete a key, we follow the below sequence of steps (algorithm).


1. Locate the leaf node.
2. In case the number of keys in a node > m/2, then delete the given key from the node.
3. In case the leaf node is not having m/2 keys, then we need to complete the keys by taking
keys from the right or left subtrees to maintain the B tree.
We follow these steps:
• If the left subtree contains m/2 elements then we push its largest element to the parent
node and then move the intervening element to the place where the key was deleted.
• If the right subtree contains m/2 elements then we push its smallest element to the
parent node and then move the intervening element to the place where the key was
deleted.

Notes prepared by Ms. Peninah J. Limo Page 26


4. If no node has m/2 nodes then the above steps cannot be performed, thus we create a new
leaf node by joining two leaf nodes and the intervening element of the parent node.
5. If a parent is without m/2 nodes, then we repeat the above steps for the parent node in
question. These steps are repeated until we get a balanced B tree.

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.

1. Each node stores at most 3 values


2. Each internal node is a 2-node, 3-node, or 4-node
3. All the leaves are on the same level

Insertion

1. If the current node is a 4-node:


• Remove and save the middle value to get a 3-node.
• Split the remaining 3-node up into a pair of 2-nodes (the now missing middle value is
handled in the next step).

2. If this is the root node (which thus has no parent):

Notes prepared by Ms. Peninah J. Limo Page 27


• the middle value becomes the new root 2-node and the tree height increases by 1.
Ascend into the root.
• Otherwise, push the middle value up into the parent node. Ascend into the parent
node.
• Find the child whose interval contains the value to be inserted.

3. If that child is a leaf, insert the value into the child node and finish.

• Otherwise, descend into the child and repeat from step 1.

Example 1
Insert 50,30,10,70,60
Solution
Insert 50

Insert 30

Insert 10 and 70

Insert 60

Notes prepared by Ms. Peninah J. Limo Page 28


Example 2
Inserting 60, 30, 10, 20, 50, 40, 70, 80, 15, 90, 100
solution
Inserting 60, 30, 10, 20 ...

Inserting 50, 40 ...

Inserting 70 ...

Inserting 80, 15 ...

Inserting 90 ...

Notes prepared by Ms. Peninah J. Limo Page 29


Inserting 100 ...

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.

Inserting the 25 results in a split:

Inserting 38 causes a split:

Notes prepared by Ms. Peninah J. Limo Page 30


Inserting 73 causes a split:

Inserting 46 results in a split that propagates up to the root:

Inserting 55 causes a split:

Notes prepared by Ms. Peninah J. Limo Page 31


Example 4

Notes prepared by Ms. Peninah J. Limo Page 32


Notes prepared by Ms. Peninah J. Limo Page 33
Deletion 2-3-4
Delete is a bit tricker than insert operation. Depending upon the location of the node
containing the target (x) to be deleted, we need to consider several cases.
Case 1: If x is in a leaf node
This has further two cases.

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.

1. The current 2-node containing x.


2. The left or right sibling node (which is also a 2-node).
3. The parent node corresponding to these two 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.

Notes prepared by Ms. Peninah J. Limo Page 34


After the merge, we use case 1.1 to delete x.
• Case 1.2.3: If both siblings and the parent node are a 2-node - We encounter this
scenario rarely. In this particular case, the parent node must be a root. Just like fusion,
we combine both the siblings and the parent node to make it a 4-node. This process
shrinks the height of the tree by 1. Figure below illustrates this. The node containing
76 is being deleted.

Case 2: If x is in an internal node


In this case, we do the following:

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.

Notes prepared by Ms. Peninah J. Limo Page 35


Example 2: leaf
Delete 4 Solution

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

Notes prepared by Ms. Peninah J. Limo Page 36


Example 6: internal node
Delete 5

Delete the predecessor, and swap it with the node to be deleted. first delete 4, then swap 4 for
5.

Example 7: internal node


Delete 2

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.

Notes prepared by Ms. Peninah J. Limo Page 37


Here are the properties of a 2-3 tree:

• Every non-leaf node has either 2 or 3 children.


• All leaves are at the same depth.
• Information (keys and associated data) is stored only at leaves (internal nodes are for
organization only).
• Keys at leaves are ordered left to right.
• In addition to child pointers, each internal node stores:
o the value of the max key in the left subtree (leftMax)
o the value of the max key in the middle subtree (middleMax)

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.

1. If the tree is null, we make N the root of the tree T.


2. Otherwise, search the tree unless we reach a leaf node where we need to insert it. In
this case, we need to consider further 2 cases.
a. If the leaf node is a 2-node, make it a 3-node. This is illustrated in Figure below.

Notes prepared by Ms. Peninah J. Limo Page 38


c. If the leaf node is a 3-node then (a) make it a temporary 4-node (b) split the 4-node and
make the middle key the parent of the other two keys. The left key becomes the left
child and the right becomes the right child. This is illustrated in Figure below:

Example 1:
Insert 95,31,77,14,69,50,60 into a 2-3 tree.
solution

Notes prepared by Ms. Peninah J. Limo Page 39


Example 2:

Notes prepared by Ms. Peninah J. Limo Page 40


3. Delete
To remove a key value from a 2-3 tree, the first step is to search for it. If it is in a leaf, simply
remove it. If it is in an internal node, replace it by its predecessor (or successor), which must
be in a leaf.

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…

6.6 SPLAY TREES

Notes prepared by Ms. Peninah J. Limo Page 41


Splay Tree is a self - adjusted Binary Search Tree in which every operation on element
rearranges the tree so that the element is placed at the root position of the tree.In a splay tree,
every operation is performed at the root of the tree. All the operations in splay tree are
involved with a common operation called "Splaying". Splaying an element, is the process of
bringing it to the root position by performing suitable rotation operations.
In a splay tree, splaying an element rearranges all the elements in the tree so that
splayed element is placed at the root of the tree. By splaying elements we bring more
frequently used elements closer to the root of the tree so that any operation on those elements
is performed quickly. That means the splaying operation automatically brings more
frequently used elements closer to the root of the tree.

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.

6.6.1 Rotations in Splay Tree

In a splay tree, to splay any element we use the following rotation operations:

• Zig Rotation (Right Rotation)


• Zag Rotation (Left Rotation)
• Zig-Zag (Zig followed by Zag)
• Zag-Zig (Zag followed by Zig)
• Zig-Zig
• Zag-Zag
The two major operations (zig-zig and zig-zag) move a node up two levels at a time, and then
a zig is a one-level operation that is necessary in case the height is odd.

(i) Zig Rotation(Right Rotation):

Notes prepared by Ms. Peninah J. Limo Page 42


Zig rotation is done When the desired node is one level below the root, a single rotation is
performed to move the node to the root. special case when X's parent, P, is the root of the
tree.

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

Notes prepared by Ms. Peninah J. Limo Page 43


(iii) Zig-zig(Right-Right rotation)

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

Notes prepared by Ms. Peninah J. Limo Page 44


The Zag- Zag rotation in a splay tree is a double zag rotation. In zag-zag rotation every
node moves two position to the left from its current position

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

Notes prepared by Ms. Peninah J. Limo Page 45


(v) Zig-zag Rotation

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.

(vi) Zag-Zig Rotation


The last rotation is the zag-zig rotation. It is a mirror of zig-zag rotation. To do zag-zig
rotation on node x, we do the right rotation at x’s parent node and left rotation at x
grandparent (new parent) node.

6.6.2 OPERATION OF SPLAY TREE


Search Operation
The search operation in Splay tree does the standard BST search, in addition to search, it also
splays (move a node to the root). If the search is successful, then the node that is found is
splayed and becomes the new root. Else the last node accessed prior to reaching the NULL is
splayed and becomes the new root.
There are following cases for the node being accessed.

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.

Notes prepared by Ms. Peninah J. Limo Page 47

You might also like