Docsity

AVL Trees: A Comprehensive Guide to Self-Balancing Binary Search Trees, Study notes of Data Structures and Algorithms

Partial preview of the text

Download AVL Trees: A Comprehensive Guide to Self-Balancing Binary Search Trees and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Bangladesh University of Professionals

Dept. of CSE

Faculty of Science and Technology

Data Structure and Algorithm I

CSE 210 1

Assignment on AVL Tree

Submitted by Rifah Zakiah

Id no. 2252421024

Submitted to Md. Hasan Al Banna

Date 31

st

October, 2023

AVL TREE:

It was the early 60’s (1962 to be specific) when two Russian mathematicians, G.M. Adelson- Velsky and E.M. Landis, invented the first self-balancing binary search tree data structure. Now the tree is called AVL in honor of its inventors. An AVL tree is a self-(height)balancing binary search tree with a constrain imposed that the difference between the height of the left and right subtrees cannot be no more than one. A binary search tree is height balanced binary search tree if it is either an empty tree or if T is a non-empty binary search tree with TL and TR as its left and right subtrees respectively, if and only if i) TL and TR are height balanced and ii) - 1 <= (hL- hR) <= 1 where hL and hR are the heights of TL and TR respectively. The aforementioned condition is restored after each tree modification and thus forces the general shape of an AVL tree. Figure 1: The left and right subtrees of an AVL tree has to differ in height by at most 1 Why AVL tree is also called height balancing tree? In an AVL tree, the heights of the two sub-trees, left and right, of a node may differ by at most one. So, with each insertion or deletion operation, this height balancing is maintained strictly. The AVL balance condition, known also as the balance factor represents an additional piece of information stored for each node. This is combined with a technique that efficiently restores the balance condition for the tree. In an AVL tree the inventors make use of a well-known technique called tree rotation. Due to this property, the AVL tree is also known as a height-balanced tree. h h + 1 root

Figure 3 : Unbalanced binary search tree Figure 4: AVL trees, insertion order: - a)1,2,3,4,5 - b)1,5,4,3,

AVL Tree Rotations: A tree rotation is a constant time operation on an AVL tree that changes the shape of a tree in order to maintain the height balance while preserving standard binary search tree properties. There are left and right rotations both of them decrease the height of a BST by moving smaller subtrees down and larger subtrees up. There are four types of rotations and application of these rotations depends on the position of the inserted node with reference to the critical node. The four categories of rotations are:

  1. LL rotation (Left rotation): The new node is inserted in the right sub-tree of the right sub-tree of the critical node.
  2. RR rotation (Right rotation): The new node is inserted in the left sub-tree of the left sub-tree of the critical node.
  3. LR rotation (Left-Right rotation): The new node is inserted in the right sub-tree of the left sub-tree of the critical node.
  4. RL rotation (Right-Left rotation): The new node is inserted in the left sub-tree of the right sub-tree of the critical node. The first two types of rotation are single rotations (i.e., only one move in the position takes place) and the latter two are double rotations (i.e., two moves in the position takes place). Left Rotation: Left Rotation, shortly LL rotation, is the rotation which occurs if a tree becomes unbalanced, when a node is inserted into the right subtree of the right subtree, then a single left rotation is performed. Every node moves one position to left from the current position. Figure 5: Left rotation in AVL tree Tree (a) in the figure is initially an AVL tree. In tree (b), a new node is inserted in the right sub- tree of the right sub-tree of the critical node A (node A is the critical node because it is the closest ancestor whose balance factor is not – 1, 0, or 1) which disrupts the balance of the tree, so we apply left rotation as shown in tree (c). Note that the new node has now become a part of tree T 3.

Figure 7 : Left-Right rotation in AVL tree Tree (a) in the figure is initially an AVL tree. In tree (b), a new node is inserted in the right sub- tree of the left sub-tree of the critical node A (node A is the critical node because it is the closest ancestor whose balance factor is not – 1, 0 or 1), which disrupts the balance of the tree, so we apply Left-Right rotation as shown in tree (c). Note that the new node has now become a part of tree T 2. While rotation, node C becomes the root, with B and A as its left and right children. Node B has T 1 and T 2 as its left and right sub-trees and T 3 and T 4 become the left and right sub- trees of node A. Right-Left Rotation: The Right-Left rotation, shortly RL rotation, is a sequence of single right rotation followed by a single left rotation. At first, every node moves one position to the right and one position to left from the current position. Figure 8 : Right-Left rotation in AVL tree

Tree (a) in the figure is initially an AVL tree. In tree (b), a new node is inserted in the left sub- tree of the right sub-tree of the critical node A (node A is the critical node because it is the closest ancestor whose balance factor is not – 1, 0, or 1), which disrupts the balance of the tree, so we apply Right-Left rotation as shown in tree (c). Note that the new node has now become a part of tree T 2. While rotation, node C becomes the root, with A and B as its left and right children. Node A has T 1 and T 2 as its left and right sub-trees and T 3 and T 4 become the left and right sub- trees of node B. Insertion in an AVL Tree: Let’s insert these items in AVL tree: Mar, May, Nov, Aug, Apr, Jan, Dec, Jul, Feb, Jun, Oct, Sep: