1 / 20

AVL Trees

AVL Trees. Performance of Binary Search Trees (BST) depends on how well balanced the tree is. Well-balanced : O (log n ) search, insertion, and deletion Ill-balanced : O ( n ) search, insertion, and deletion.

Download Presentation

AVL Trees

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. AVL Trees Performance of Binary Search Trees (BST) depends on how well balanced the tree is Well-balanced: O(log n) search, insertion, and deletionIll-balanced: O(n) search, insertion, and deletion. If the insertion and deletion algorithms can keep the tree balanced (at a small cost), we will get O(log n) performance always. AVL Trees

  2. Examples of (un)balanced BST A tree of height h is balanced if all nodes at level ≤h– 2 have two children, nodes at level h – 1 have 0, 1 or 2 children, and nodes at level h have no children. level 3 level 2 level 4 level 1 level 1 level 3 level 2 50 50 20 20 60 60 80 80 90 Balanced Imbalanced AVL Trees

  3. Examples of re-balanced BST 60 60 60 60 50 50 50 50 60 20 50 50 50 20 20 80 60 60 20 20 20 level 1 level 1 level 1 level 1 level 1 level 1 level 1 level 3 level 3 level 3 level 3 level 1 level 4 level 3 level 2 level 2 level 2 level 2 level 2 level 2 level 2 Imbalanced Unbalanced 90 80 80 60 Rebalanced(right rotation) 90 Rebalanced(left rotation) Imbalanced AVL Trees

  4. Examples of re-balanced BST Imbalanced 50 50 50 60 80 20 80 60 80 20 50 20 90 60 level 1 level 1 level 1 level 1 level 3 level 3 level 4 level 3 level 3 level 2 level 2 level 4 level 4 level 2 level 2 90 80 90 20 60 70 70 Right-left double rotation 90 70 AVL Trees

  5. Examples of re-balanced BST Imbalanced 60 60 60 60 60 60 60 80 50 80 80 80 80 80 40 50 50 50 50 80 50 55 90 20 70 55 50 55 55 55 90 90 90 90 90 40 20 20 20 20 70 70 70 70 70 level 1 level 1 level 1 level 1 level 1 level 1 level 1 level 3 level 3 level 3 level 3 level 4 level 4 level 4 level 4 level 3 level 5 level 3 level 3 level 4 level 2 level 2 level 2 level 2 level 4 level 4 level 2 level 5 level 2 level 2 90 20 70 10 55 35 40 40 20 10 10 35 35 Left-right double rotation 10 10 AVL Trees

  6. Summary: cases of imbalanced node 60 h = 0 +2 50 h = 2 -2 h = 0 20 50 50 Case of left rotation Case of right rotation h = 2 80 20 60 20 level 1 level 1 level 1 level 3 level 3 level 4 level 4 level 3 level 1 level 5 level 3 level 2 level 2 level 4 level 2 level 2 60 90 60 - 2 80 80 50 +2 70 90 55 90 20 70 h = 3 h = 3 40 10 Case of left-right rotation h = 1 h = 1 35 Case of right-left rotation AVL Trees

  7. Rebalancing To correct an imbalanced node N in an AVL tree: • right rotation: if addition is in left subtree of N’s left child • left-right rotation: if addition is in right subtree of the N’s left child C C N N C N N C 3) left rotation: if addition is in right subtree of N’s right child 4) right-left rotation: if addition is in left subtree of N’s right child (3) (4) (1) (2) AVL Trees

  8. Right rotation Correct imbalance at given nodeN caused byaddition in the left subtree of nodeN’s left child T3 C N C N T2 rotateRight(TreeNode nodeN){ nodeC = nodeN’s leftchild set nodeN’s left child to nodeC’s right childset nodeC’s right child to nodeN return nodeC } T1 T1 T2 T3 AVL Trees

  9. Left rotation Correct imbalance at given nodeN caused byaddition in the right subtree of nodeN’s right child T3 C N N C T2 rotateLeft(TreeNode nodeN){ nodeC = nodeN’s rightchild set nodeN’s right child to nodeC’s left childset nodeC’s left child to nodeN return nodeC } T1 T1 T3 T2 T1 T2 T3 T1 T2 T3 AVL Trees

  10. Left-Right rotation Correct imbalance at given nodeN caused byaddition in the right subtree of nodeN’s left child C G C N rotateLeftRight(TreeNode nodeN){ nodeC = nodeN’s leftchild; newLeft = rotateLeft(nodeC); set nodeN’s left child to newLeft; return rotateRight(nodeN);} N T4 G T1 T1 T2 T3 T4 T3 T2 AVL Trees

  11. Right-Left rotation Correct imbalance at given nodeN caused byaddition in the left subtree of nodeN’s right child N G C N rotateRightLeft(TreeNode nodeN){ nodeC = nodeN’s rightchild; newRight = rotateRight(nodeC); set nodeN’s right child to newRight; return rotateLeft(nodeN);} C T1 G T1 T2 T3 T4 T4 T2 T3 AVL Trees

  12. …Putting all together reBalance(TreeNode nodeN){ if (nodeN’s left subtree is taller than right subtree by more than 1){ if (nodeN’s leftchild has left subtree taller than its right subtree){ rotateRight(nodeN);else rotateLeftRight(nodeN); else{ if (nodeN’s right subtree is taller than left subtree by more than 1){ if (nodeN’s rightchild has right subtree taller than its left subtree){ rotateLeft(nodeN);else rotateRightLeft(nodeN); } // else nodeN is balanced } return nodeN.} AVL Trees

  13. Computing the height of the subtrees A key operation in the rebalancing algorithm is computing the difference of the heights of the left and right subtrees Define an auxiliary method: private int getHeightDifference( ) //post: Returns a number greater than 1 when the height of the left subtree is //post: more than 1 taller than the height of the right subtree.//post: Returns a number less than -1 when the height of the right subtree is //post: more than 1 taller than the height of the left subtree. //post Returns 0 when the heights of the left and right subtrees are equal • as auxiliary method in AVL tree, or • as auxiliary method of the class TreeNode, but node needs to keep track of its height. Heaps

  14. Dynamic Implementation of AVL trees class AVLTree<K extends Comparable<K>,V>extends LinkedBasedBST<K,V>{public AVLTree(){ super(); } public AVLTree(K key, V, Value){ super(key, value); }} continue…… AVL Trees

  15. Dynamic Implementation of AVL trees ……private TreeNode<K,V> rotateRight(TreeNode<K,V> nodeN){ ……… <algorithm in slide 8>} private TreeNode<K,V> rotateLeft(TreeNode<K,V> nodeN){ ……… <algorithm in slide 9>} private TreeNode<K,V> rotateLeftRight(TreeNode<K,V> nodeN){ ……… <algorithm in slide 10>} private TreeNode<K,V> rotateRightLeft(TreeNode<K,V> nodeN){ ……… <algorithm in slide 11>} private TreeNode<K,V> reBalance(TreeNode<K,V> nodeN){ ……… <algorithm in slide 12>} public void insert(K key, V value){root = insertElem(root, key, value); } public void delete(K key){ root = deleteElem(root, key, value); } } AVL Trees

  16. Implementing insertElem TreeNode<K,V> insertElem(TreeNode<K,V> node, K key, V newValue) if (node is null) {Create a new node and let node reference it; Set the value in the new node to be equal to newValue; Set the references in the new node to null;Set the height of the node to be 1; } else if (key == node.getKey( )){replace the value in node with newValue;} else if (key < node.getKey( )){ TreeNode newLeft = insertElem(node.getLeft( ), key, newValue) node.setLeft(reBalance(newLeft)); } else { TreeNode newRight = insertElem(node.getRight( ), key, newValue) node.setRight(reBalance(newRight)); } return node; } AVL Trees

  17. Implementing deleteElem TreeNode<K,V> deleteElem(TreeNode<K,V> node, K key)// post: deletes the node from the BST, whose key is equal key and returns the // post: root node of the resulting tree after being rebalanced. if (node is null) throw TreeException; else if (node.getKey( ) == key) node = deleteNode(node); return node; else if (node.getKey( ) > key){TreeNode<K,V> newLeft=deleteElem(node.getLeft( ),key)node.setLeft(newLeft); return reBalance(node);} else TreeNode<K,V> newRight=deleteElem(node.getRight( ),key)node.setRight(newRight); return reBalance(node); } AVL Trees

  18. Implementing “deleteNode” TreeNode<K,V> deleteNode(TreeNode<K,V> node) // post: delete the given node and returns the modified tree rebalancedif (node is a leaf)return null; else{if (node has only left child) return node.getLeft( ); else if (node has only right child)return node.getRight( ); else{ replacementNode = findLeftMost(node.getRight( )); newRight = deleteLeftMost(node.getRight( )); replacementNode.setRight(newRight); replacementNode.setLeft(node.getLeft( )); return reBalance(replacementNode); } AVL Trees

  19. Implementing “deleteLeftMost” TreeNode<K,V> deleteLeftMost(TreeNode<K,V> node) {// post: deletes the left most node in the tree rooted at node. // post: returns the root of the modified sub-tree, rebalanced if (node.getLeft( ) is null) return node.getRight( ); else { newChild = deleteLeftMost(node.getLeft( )); node.setleft(newChild); return reBalance(node); } } The auxiliary method findLeftMost is identical to theone for Binary Search Tree. AVL Trees

  20. Summary • AVL trees are special binary search trees that guarantees the tree to be balanced after each insertion and deletion of a node, whilst preserving the ordering over the keys of the nodes in the tree. • Althought the rebalance procedure is called at each level of the recursion (when inserting a new node), the rebalancing of the tree occurs at most once. Most calls to rebalance simply check whether rebalancing is needed. • The computational time of insert in an AVL tree is approximately closed to the computational time of insert in an Binary Search tree. Heaps

More Related