Ticker

6/recent/ticker-posts

AVL Tree part 1 | Graph | Data Structure & Algorithm




 

AVL Tree

Table of Contents
•What is the AVL Tree? •Why AVL Trees? •Advantages of AVL tree. •Algorithm for different Operations on AVL. For Insertion: For Deletion: What is the AVL Tree? AVL tree is a binary search tree in which the difference of heights of left and right subtrees of any node is less than or equal to one. The technique of balancing the height of binary trees was developed by Adelson, Velskii, and Landi and hence given the short form as AVL tree or Balanced Binary Tree. An AVL tree can be defined as follows: Let T be a non-empty binary tree with TL and TR as its left and right subtrees. The tree is height balanced if: 1. TL and TR are height balanced 2. |hL - hR| less than or equal to 1, where hL - hR are the heights of TL and TR The Balance factor of a node in a binary tree can have value 1, -1, 0, depending on whether the height of its left subtree is greater, less than or equal to the height of the right subtree. Why AVL Trees? Most of the BST operations (e.g., search, max, min, insert, delete.. etc) take O(h) time where h is the height of the BST. The cost of these operations may become O(n) for a skewed Binary tree. If we make sure that height of the tree remains O(Logn) after every insertion and deletion, then we can guarantee an upper bound of O(Logn) for all these operations. The height of an AVL tree is always O(Logn) where n is the number of nodes in the tree. Advantages of AVL tree. Since AVL trees are height balance trees, operations like insertion and deletion have low time complexity. Algorithm for different Operations on AVL For Insertion: Step 1: First, insert a new element into the tree using BST's (Binary Search Tree) insertion logic. Step 2: After inserting the elements you have to check the Balance Factor of each node. Step 3: When the Balance Factor of every node will be found like 0 or 1 or -1 then the algorithm will proceed for the next operation. Step 4: When the balance factor of any node comes other than the above three values then the tree is said to be imbalanced. Then perform the suitable Rotation to make it balanced and then the algorithm will proceed for the next operation. For Deletion: Step 1: Firstly, find that node where k is stored Step 2: Secondly delete those contents of the node (Suppose the node is x) Step 3: Claim: Deleting a node in an AVL tree can be reduced by deleting a leaf. There are three possible cases: •When x has no children then, delete x •When x has one child, let x' becomes the child of x. •Notice: x' cannot have a child, since subtrees of T can differ in height by at most one : •then replace the contents of x with the contents of x' •then delete x' (a leaf) Step 4: When x has two children, •then find x's successor z (which has no left child) •then replace x's contents with z's contents, and •delete z In all of the three cases, you will end up removing a leaf.

Post a Comment

0 Comments