Langsung ke konten utama

AVL Tree [Theory]

AVL TREE
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. For example :
  
(Left : Correct AVL Tree || Right : Wrong AVL Tree)
The reason why right AVL tree is wrong because the above tree is not AVL because differences between heights of left and right subtrees for 8 and 18 is greater than 1.
AVL trees have an additional guarantee:
The difference between the depth of right and left subtrees cannot be more than one. In order to maintain this guarantee, an implementation of an AVL will include an algorithm to rebalance the tree when adding an additional element would upset this guarantee. AVL trees have a worst case lookup, insert and delete time of O(log n).
In AVL Tree, we can perform Insertion and Deletion.
·       Insertion
To make sure that the given tree remains AVL after every insertion, we must augment the standard BST insert operation to perform some re-balancing. Following are two basic operations that can be performed to re-balance a BST without violating the BST property (keys(left) < key(root) < keys(right)).
AVL Insertion Process
You will do an insertion similar to a normal Binary Search Tree insertion. After inserting, you fix the AVL property using left or right rotations.
·       If there is an imbalance in left child of right subtree, then you perform a left-right rotation.
·       If there is an imbalance in left child of left subtree, then you perform a right rotation.
·       If there is an imbalance in right child of right subtree, then you perform a left rotation.
·       If there is an imbalance in right child of left subtree, then you perform a right-left rotation.
·       Deletion
To make sure that the given tree remains AVL after every deletion, we must augment the standard BST delete operation to perform some re-balancing. Following are two basic operations that can be performed to re-balance a BST without violating the BST property (keys(left) < key(root) < keys(right)).
·       Balance
In AVL tree, after performing every operation like insertion and deletion we need to check the balance factor of every node in the tree. If every node satisfies the balance factor condition then we conclude the operation otherwise we must make it balanced. We use rotation operations to make the tree balanced whenever the tree is becoming imbalanced due to any operation.
Rotation operations are used to make a tree balanced.There are four rotations and they are classified into two types:

Single Left Rotation (LL Rotation)

In LL Rotation every node moves one position to left from the current position.

Single Right Rotation (RR Rotation)

In RR Rotation every node moves one position to right from the current position.

Left Right Rotation (LR Rotation)

The LR Rotation is combination of single left rotation followed by single right rotation. In LR Rotation, first every node moves one position to left then one position to right from the current position.

Right Left Rotation (RL Rotation)

The RL Rotation is combination of single right rotation followed by single left rotation. In RL Rotation, first every node moves one position to right then one position to left from the current position.
References :
https://www.freecodecamp.org/news/avl-tree-insertion-rotation-and-balance-factor/

Komentar

Postingan populer dari blog ini

Heap & Tries {Theory}

HEAP & TRIES 1.        Heap Dalam  ilmu komputer , sebuah  heap  adalah struktur data yang berdasarkan konsep struktur data  pohon . Di heap, juga diterapkan konsep binary tree. Terdapat 3 jenis heap, diantaranya : a.      Min heap Min heap merupakan heap yang node rootnya adalah angka terkecil dan setiap anak dari rootnya selalu lebih kecil daripada parentnya. Biasanya min heap ini, dia ascending ( dari terkecil ke terbesar ). b.     Max heap Max heap ini sama seperti min heap, tapi bedanya adalah angka yang berada di root adalah angka terbesar dan max heap ini descending ( dari terbesar ke terkecil ). c.      Min Max heap Untuk min max heap ini cukup unik, dimana root dari heap ini merupakan angka terkecil didalam tree nya, tapi angka yang berada dii node anak dari sebuah root merupakan angka paling besar dan berlaku seterusnya sampai ke bawah. Berikut ad...

Binary Tree Introduction

BINARY TREE DATA STRUCTURE               A tree whose elements have at most 2 children is called a binary tree. Since each element in a binary tree can have only 2 children, we typically name them the left and right child. PARTS OF NODE IN BINARY TREE : 1.       Data, 2.       Pointer to left child, and 3.       Pointer to right child. THE USED FROM BINARY TREE ARE FOLLOWING : 1.       Manipulate hierarchical data, 2.       Make information easy to search, 3.       Manipulate sort list of data, 4.       Composting digital images for visual effect, 5.       Router Algorithms, and 6.         Form of a multi-stage decision-making (see business chess). TREE’S PART 1. ...