0% found this document useful (0 votes)
9 views

Data structure avl and splay trees

AVL trees are self-balancing binary search trees where the height difference between left and right subtrees is at most one, requiring rotations to maintain balance during insertions and deletions. Splay trees are self-adjusting binary search trees that bring recently accessed elements to the root through a process called splaying, with various rotation types to manage tree structure. B-trees are height-balanced m-way trees that allow multiple keys per node and require specific algorithms for searching, inserting, and deleting elements while maintaining balance.
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)
9 views

Data structure avl and splay trees

AVL trees are self-balancing binary search trees where the height difference between left and right subtrees is at most one, requiring rotations to maintain balance during insertions and deletions. Splay trees are self-adjusting binary search trees that bring recently accessed elements to the root through a process called splaying, with various rotation types to manage tree structure. B-trees are height-balanced m-way trees that allow multiple keys per node and require specific algorithms for searching, inserting, and deleting elements while maintaining balance.
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
You are on page 1/ 10

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

Operations on AVL tree


1. Insertion

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:

1. L L rotation: Inserted node is in the left subtree of left subtree of A


2. R R rotation : Inserted node is in the right subtree of right subtree of A

3. L R rotation: Inserted node is in the right subtree of left subtree of A

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

Now we perform LL clockwise rotation on full tree

4. R L rotation: Inserted node is in the left subtree of right subtree of A

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

LL (clockwise) on subtree rooted at C is performed first. After


performing LL rotation, node A is still unbalanced, i.e. having
balance factor -2.

Now we perform RR rotation (anticlockwise rotation) on full


tree. Balance factor of each node is now either -1, 0, or 1, i.e., BST
is balanced now
Where node A is the node whose balance Factor is other than -1, 0, 1.

Self-Evaluation:

Construct an AVL tree having the following elements


H, I, J, B, A, E, C, F, D, G, K, L
2. Deletion:

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

Applications of the splay tree:


 Caching: Splay trees can be used to implement cache memory management, where the
most frequently accessed items are moved to the top of the tree for quicker access.
 Database Indexing: Splay trees can be used to index databases for faster searching and
retrieval of data.
 File Systems: Splay trees can be used to store file system metadata, such as the allocation
table, directory structure, and file attributes.
 Data Compression: Splay trees can be used to compress data by identifying and
encoding repeating patterns.
 Text Processing: Splay trees can be used in text processing applications, such as spell-
checkers, where words are stored in a splay tree for quick searching and retrieval.
 Graph Algorithms: Splay trees can be used to implement graph algorithms, such as
finding the shortest path in a weighted graph.
 Online Gaming: Splay trees can be used in online gaming to store and manage high
scores, leaderboards, and player statistics.

Advantages of Splay Trees:


 Splay trees have time complexity of O(log n) for many operations, making them
faster than many other balanced tree data structures
 They automatically balance themselves as items are inserted and removed.
Disadvantages of Splay Trees:
 Splay trees can have worst-case time complexity of O(n) for some operations, making
them less predictable than other balanced tree data structures like AVL trees
 Splay trees may not be suitable for certain applications where predictable
performance is required.

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.

Properties of B-Trees of order m:

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.

A B tree of order 4 is shown below:

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.

1. Locate the leaf node.


2. If there are more than m/2 keys in the leaf node, then delete the desired key from the
node.
3. If the leaf node doesn't contain m/2 keys, then complete the keys by taking the element
from eight or left sibling.
o If the left sibling contains more than m/2 elements, then push its largest element
up to its parent and move the intervening element down to the node where the
key is deleted.
o If the right sibling contains more than m/2 elements, then push its smallest
element up to the parent and move intervening element down to the node where
the key is deleted.
4. If neither of the sibling contain more than m/2 elements, then create a new leaf node by
joining two leaf nodes and the intervening element of the parent node.
5. If parent is left with less than m/2 nodes then, apply the above process on the parent
too.

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.

53 is present in the right child of element 49. Delete it.

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.

The final B tree is shown as follows.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy