Presentation is loading. Please wait.

Presentation is loading. Please wait.

Course: Programming II - Abstract Data Types AVL TreesSlide Number 1 AVL Trees Performance of Binary Search Trees (BST) depends on how well balanced the.

Similar presentations


Presentation on theme: "Course: Programming II - Abstract Data Types AVL TreesSlide Number 1 AVL Trees Performance of Binary Search Trees (BST) depends on how well balanced the."— Presentation transcript:

1 Course: Programming II - Abstract Data Types AVL TreesSlide Number 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 deletion Ill-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.

2 Course: Programming II - Abstract Data Types Examples of (un)balanced BST AVL TreesSlide Number 2 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. 50 60 20 80 level 1 level 2 level 3 Balanced 50 60 20 80 level 1 level 2 level 3 level 4 90 Imbalanced

3 Course: Programming II - Abstract Data Types AVL TreesSlide Number 3 Examples of re-balanced BST 60 level 1 level 2 60 50 level 1 level 2 60 50 level 1 level 3 20 Unbalanced level 2 50 level 1 level 3 20 60 Imbalanced Rebalanced (right rotation) level 2 20 level 1 60 50 level 2 50 20 level 1 level 3 60 80 level 2 20 level 1 level 3 60 80 level 4 90 50 Imbalanced level 2 20 level 1 level 3 80 9060 50 Rebalanced (left rotation)

4 Course: Programming II - Abstract Data Types AVL TreesSlide Number 4 Examples of re-balanced BST level 2 50 20 level 1 level 3 60 80 90 level 4 70 level 2 60 50 level 1 level 3 80 90 level 4 7020 Right-left double rotation level 2 50 20 level 1 level 3 80 9060 level 2 50 20 level 1 level 3 80 90 60 level 4 70 Imbalanced

5 Course: Programming II - Abstract Data Types AVL TreesSlide Number 5 Examples of re-balanced BST level 2 60 50 level 1 level 3 80 90 level 4 7020 level 2 60 50 level 1 level 3 80 90 level 4 7020 55 level 2 60 50 level 1 level 3 80 90 level 4 7020 55 10 level 2 60 50 level 1 level 3 80 90 level 4 7020 55 10 40 level 2 60 50 level 1 level 3 80 90 level 4 7020 55 10 40 35 level 5 Imbalanced level 2 60 50 level 1 level 3 80 90 level 4 7040 55 20 35 level 5 10 Left-right double rotation level 2 60 40 level 1 level 3 80 90 level 4 70 50 20 35 1055

6 Course: Programming II - Abstract Data Types Summary: cases of imbalanced node AVL TreesSlide Number 6 level 2 50 20 level 1 level 3 60 80 level 4 90 h = 2 h = 0 -2 level 2 60 50 level 1 level 3 20 h = 2 h = 0 +2 level 2 60 50 level 1 level 3 80 90 level 4 7020 55 10 40 35 level 5 h = 1 h = 3 +2 Case of right rotation Case of left rotation Case of left-right rotation level 2 50 20 level 1 level 3 80 9060 level 4 70 h = 1 h = 3 - 2 Case of right-left rotation

7 Course: Programming II - Abstract Data Types AVL TreesSlide Number 7 Rebalancing To correct an imbalanced node N in an AVL tree: C N (1) 1)right rotation: if addition is in left subtree of N’s left child C N (2) 2)left-right rotation: if addition is in right subtree of the N’s left child (3) C N 3) left rotation: if addition is in right subtree of N’s right child C N (4 ) 4) right-left rotation: if addition is in left subtree of N’s right child

8 Course: Programming II - Abstract Data Types Right rotation AVL TreesSlide Number 8 C N Correct imbalance at given nodeN caused by addition in the left subtree of nodeN’s left child rotateRight(TreeNode nodeN){ nodeC = nodeN’s leftchild set nodeN’s left child to nodeC’s right child set nodeC’s right child to nodeN return nodeC } N C T1T1 T2T2 T3T3 T3T3 T2T2 T1T1

9 Course: Programming II - Abstract Data Types Left rotation AVL TreesSlide Number 9 Correct imbalance at given nodeN caused by addition in the right subtree of nodeN’s right child rotateLeft(TreeNode nodeN){ nodeC = nodeN’s rightchild set nodeN’s right child to nodeC’s left child set nodeC’s left child to nodeN return nodeC } T1T1 T2T2 T3T3 T3T3 T2T2 T1T1 C N T1T1 T2T2 T3T3 N C T1T1 T2T2 T3T3

10 Course: Programming II - Abstract Data Types AVL TreesSlide Number 10 Left-Right rotation Correct imbalance at given nodeN caused by addition in the right subtree of nodeN’s left child rotateLeftRight(TreeNode nodeN){ nodeC = nodeN’s leftchild; newLeft = rotateLeft(nodeC); set nodeN’s left child to newLeft; return rotateRight(nodeN); } C G N T1T1 T4T4 T2T2 T3T3 C N G T1T1 T4T4 T2T2 T3T3

11 Course: Programming II - Abstract Data Types AVL TreesSlide Number 11 Right-Left rotation Correct imbalance at given nodeN caused by addition in the left subtree of nodeN’s right child rotateRightLeft(TreeNode nodeN){ nodeC = nodeN’s rightchild; newRight = rotateRight(nodeC); set nodeN’s right child to newRight; return rotateLeft(nodeN); } N G C T1T1 T4T4 T2T2 T3T3 C N G T1T1 T4T4 T2T2 T3T3

12 Course: Programming II - Abstract Data Types AVL TreesSlide Number 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. }

13 Course: Programming II - Abstract Data Types HeapsSlide Number 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  as auxiliary method in AVL tree, or  as auxiliary method of the class TreeNode, but node needs to keep track of its height. 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

14 Course: Programming II - Abstract Data Types Dynamic Implementation of AVL trees AVL TreesSlide Number 14 class AVLTree,V> extends LinkedBasedBST { public AVLTree(){ super(); } public AVLTree(K key, V, Value){ super(key, value); } }continue……

15 Course: Programming II - Abstract Data Types AVL TreesSlide Number 15 …… private TreeNode rotateRight(TreeNode nodeN){ ……… } private TreeNode rotateLeft(TreeNode nodeN){ ……… } private TreeNode rotateLeftRight(TreeNode nodeN){ ……… } private TreeNode rotateRightLeft(TreeNode nodeN){ ……… } private TreeNode reBalance(TreeNode nodeN){ ……… } public void insert(K key, V value){ root = insertElem(root, key, value); } public void delete(K key){ root = deleteElem(root, key, value); } Dynamic Implementation of AVL trees

16 Course: Programming II - Abstract Data Types AVL TreesSlide Number 16 TreeNode insertElem(TreeNode 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; } Implementing insertElem

17 Course: Programming II - Abstract Data Types AVL TreesSlide Number 17 Implementing deleteElem TreeNode deleteElem(TreeNode 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 newLeft=deleteElem(node.getLeft( ),key) node.setLeft(newLeft); return reBalance(node);} else TreeNode newRight=deleteElem(node.getRight( ),key) node.setRight(newRight); return reBalance(node); }

18 Course: Programming II - Abstract Data Types AVL TreesSlide Number 18 Implementing “ deleteNode ” TreeNode deleteNode(TreeNode node) // post: delete the given node and returns the modified tree rebalanced if (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); }

19 Course: Programming II - Abstract Data Types AVL TreesSlide Number 19 Implementing “ deleteLeftMost ” TreeNode deleteLeftMost(TreeNode 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 the one for Binary Search Tree.

20 Course: Programming II - Abstract Data Types HeapsSlide Number 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.


Download ppt "Course: Programming II - Abstract Data Types AVL TreesSlide Number 1 AVL Trees Performance of Binary Search Trees (BST) depends on how well balanced the."

Similar presentations


Ads by Google