Data structure avl and splay trees
Data structure avl and splay trees
AVL tree is a self-balancing Binary Search Tree (BST) where the difference between heights
of left and right subtrees cannot be more than one for all nodes. Tree is said to be balanced if
balance factor of each node is in between -1 to 1, otherwise, the tree will be unbalanced and
need to be balanced.
Balance Factor (k) = height (left(k)) - height (right(k))
Example of AVL Tree:
The above tree is AVL because the differences between the heights of left and right
subtrees for every node are less than or equal to 1.
Example of a Tree that is NOT an AVL Tree:
The above tree is not AVL because the differences between the heights of the left and right
subtrees for 8 and 12 are greater than 1.
Insertion in AVL tree is performed in the same way as it is performed in a binary search tree.
However, it may lead to violation in the AVL tree property and therefore the tree may need
balancing. The tree can be balanced by applying rotations.
Let A is the newly inserted node whose balance Factor is other than -1, 0, 1. Re-balance the
tree by performing appropriate rotations.
AVL Rotations
We perform rotation in AVL tree only in case if Balance Factor is other than -1, 0, and 1. There
are basically four types of rotations which are as follows:
A node B has been inserted into the right subtree of A the left
subtree of C, because of which C has become an unbalanced node
having balance factor 2. This case is L R rotation where: Inserted
node is in the right subtree of left subtree of C
RR (anticlockwise) on subtree rooted at A is performed first. After
performing RR rotation, node C is still unbalanced, i.e., having
balance factor 2, as inserted node A is in the left of left of C
A node B has been inserted into the left subtree of C the right
subtree of A, because of which A has become an unbalanced node
having balance factor - 2. This case is RL rotation where: Inserted
node is in the left subtree of right subtree of A
Self-Evaluation:
Augment the standard Binary Search Tree delete operation and make sure that the given tree
remains AVL after every deletion to perform some re-balancing. To delete a node in an AVL
Tree, the same procedure to restore balance is needed as for the code to insert a node
Time Complexity
Algorithm Average case Worst case
Search o(log n) o(log n)
Insert o(log n) o(log n)
Delete o(log n) o(log n)
SPLAY Trees
Splay tree is a self-adjusting binary search tree data structure, which means that the tree
structure is adjusted dynamically based on the inserted elements. The principle behind splay
trees is to bring the most recently accessed or inserted element to the root of the tree by
performing a sequence of tree rotations, called splaying. Splaying is a process of restructuring
the tree by making the most recently accessed or inserted element the new root and gradually
moving the remaining nodes closer to the root.
Operations in a splay tree:
Insertion: To insert a new element into the tree, start by performing a regular binary
search tree insertion. Then, apply rotations to bring the newly inserted element to the
root of the tree.
Deletion: To delete an element from the tree, first locate it using a binary search tree
search. Then, if the element has no children, simply remove it. If it has one child,
promote that child to its position in the tree. If it has two children, find the successor of
the element (the smallest element in its right subtree), swap its key with the element to
be deleted, and delete the successor instead.
Search: To search for an element in the tree, start by performing a binary search tree
search. If the element is found, apply rotations to bring it to the root of the tree. If it is
not found, apply rotations to the last node visited in the search, which becomes the new
root.
Splay trees are characterized by their use of rotations, which are used to bring the accessed
element to the root of the tree, and also to rebalance the tree if it becomes unbalanced after
multiple accesses.
Rotations in Splay Tree
Zig Rotation
Zag Rotation
Zig – Zig Rotation
Zag – Zag Rotation
Zig – Zag Rotation
Zag – Zig Rotation
1) Zig Rotation:
The Zig Rotation in splay trees operates in a manner similar to the single right rotation in
AVL Tree rotations. This rotation results in nodes moving one position to the right from
their current location.
2) Zag Rotation:
The Zag Rotation in splay trees operates in a similar fashion to the single left rotation in
AVL Tree rotations. During this rotation, nodes shift one position to the left from their
current location.
3) Zig-Zig Rotation:
The Zig-Zig Rotation in splay trees is a double zig rotation. This rotation results in nodes
shifting two positions to the right from their current location.
4) Zag-Zag Rotation:
In splay trees, the Zag-Zag Rotation is a double zag rotation. This rotation causes nodes to
move two positions to the left from their present position.
5) Zig-Zag Rotation:
The Zig-Zag Rotation in splay trees is a combination of a zig rotation followed by a zag
rotation. As a result of this rotation, nodes shift one position to the right and then one
position to the left from their current location.
6) Zag-Zig Rotation:
The Zag-Zig Rotation in splay trees is a series of zag rotations followed by a zig rotation.
This results in nodes moving one position to the left, followed by a shift one position to the
right from their current location
B - Tree
B-Tree is a special type of self-balancing search tree in which each node can contain more
than one key and can have more than two children. It is also known as a height-balanced m-
way tree.
Operations
1. Searching:
Searching in B Trees is similar to that in Binary search tree. For example, if we search for an
item 49 in the following B Tree. The process will something like following :
1. Compare item 49 with root node 78. since 49 < 78 hence, move to its left sub-tree.
2. Since, 40<49<56, traverse right sub-tree of 40.
3. 49>45, move to right. Compare 49.
4. match found, return.
Searching in a B tree depends upon the height of the tree. The search algorithm takes O(log n)
time to search any element in a B tree.
2. Inserting
Insertions are done at the leaf node level. The following algorithm needs to be followed in
order to insert an item into B Tree.
1. Traverse the B Tree in order to find the appropriate leaf node at which the node can be
inserted.
2. If the leaf node contains less than m-1 keys, then insert the element in the increasing
order.
3. Else, if the leaf node contains m-1 keys, then follow the following steps.
o Insert the new element in the increasing order of elements.
o Split the node into the two nodes at the median.
o Push the median element up to its parent node.
o If the parent node also contains m-1 number of keys, then split it too by
following the same steps.
Example:
Insert the node 8 into the B Tree of order 5 shown in the following image.
8 will be inserted to the right of 5, therefore insert 8.
The node, now contain 5 keys which is greater than (5 -1 = 4) keys. Therefore, split the node
from the median i.e. 8 and push it up to its parent node shown as follows.
3. Deletion
Deletion is also performed at the leaf nodes. The node which is to be deleted can either be a
leaf node or an internal node. Following algorithm needs to be followed in order to delete a
node from a B tree.
If the node which is to be deleted is an internal node, then replace the node with its in-order
successor or predecessor. Since, successor or predecessor will always be on the leaf node
hence, the process will be similar as the node is being deleted from the leaf node.
Example 1
Delete the node 53 from the B Tree of order 5 shown in the following figure.
Now, 57 is the only element which is left in the node, the minimum number of elements that
must be present in a B tree of order 5, is 2. it is less than that, the elements in its left and right
sub-tree are also not sufficient therefore, merge it with the left sibling and intervening element
of parent i.e. 49.