AVL TreesBinary Tree IssueSlide 3Balanced TreeAVL TreeSlide 6AVL Tree NodeSlide 8Searching AVL TreesSearching an AVL TreeInserting in AVL TreeSlide 12Slide 13Re-Balancing a TreeSlide 15Case 1Slide 17Case 2Rotating a NodeRotate LeftSlide 21Slide 22Re-Balancing the TreeInserting a NodeAVL TreesCSCI 2720Fall 2005KraemerBinary Tree IssueOne major problem with the binary trees we have discussed thus far:they can become extremely unbalancedthis will lead to long search timesin the worst case scenario, inserts are done in orderthis leads to a linked list structurepossible O(n) performancethis is not likely but it’s performance will tend to be worse than O(log n)Binary Tree IssueBinaryTree tree = new BinaryTree();tree.insert(A);tree.insert(C);tree.insert(F);tree.insert(M);tree.insert(Z);rootACFMZBalanced TreeIn a perfectly balanced treeall leaves are at one leveleach non-leaf node has two childrenrootMCEJ OSWWorst case search time is log nAVL TreeAVL tree definitiona binary tree in which the maximum difference in the height of any node’s right and left sub-trees is 1 (called the balance factor)balance factor = height(right) – height(left)AVL trees are usually not perfectly balancedhowever, the biggest difference in any two branch lengths will be no more than one levelAVL Tree-100000-1001-21-100AVL TreeAVL Tree0 Not an AVL TreeAVL Tree NodeVery similar to regular binary tree nodemust add a balance factor fieldFor this discussion, we will consider the key field to also be the datathis will make things look slightly simplerthey will be confusing enough as it is AVL Tree Nodeclass TreeNode {public Comparable key;public TreeNode left;public TreeNode right;public int balFactor;public TreeNode(Comparable key) {this.key = key;left = right = null;balFactor = 0;}}Searching AVL TreesSearching an AVL tree is exactly the same as searching a regular binary treeall descendants to the right of a node are greater than the nodeall descendants to the left of a node are less than the nodeSearching an AVL TreeObject search(Comparable key, TreeNode subRoot) { I) if subRoot is null (empty tree or key not found)A) return nullII) compare subRoot’s key (Kr) to search key (Ks)A) if Kr < Ks -> recursively call search with right subTreeB) if Kr > Ks -> recursively call search with left subTree C) if Kr == Ks-> found it! return data in subRoot}Inserting in AVL TreeInsertion is similar to regular binary treekeep going left (or right) in the tree until a null child is reachedinsert a new node in this positionan inserted node is always a leaf to start withMajor difference from binary treemust check if any of the sub-trees in the tree have become too unbalancedsearch from inserted node to root looking for any node with a balance factor of ±2Inserting in AVL TreeA few points about tree insertsthe insert will be done recursivelythe insert call will return true if the height of the sub-tree has changedsince we are doing an insert, the height of the sub-tree can only increaseif inse rt() returns true, balance factor of current node needs to be adjustedbalance factor = height(right) – height(left)left sub-tree increases, balance factor decreases by 1right sub-tree increases, balance factor increases by 1if balance factor equals ±2 for any node, the sub-tree must be rebalancedInserting in AVL TreeM(-1)insert(V)E(1)J(0)P(0)M(0)E(1)J(0)P(1)V(0)M(-1)insert(L)E(1)J(0)P(0)M(-2)E(-2)J(1)P(0)L(0)This tree needs to be fixed!Re-Balancing a TreeTo check if a tree needs to be rebalancedstart at the parent of the inserted node and journey up the tree to the rootif a node’s balance factor becomes ±2 need to do a rotation in the sub-tree rooted at the nodeonce sub-tree has been re-balanced, guaranteed that the rest of the tree is balanced as wellcan just return false from the insert() method4 possible cases for re-balancingonly 2 of them need to be consideredother 2 are identical but in the opposite directionRe-Balancing a TreeCase 1a node, N, has a balance factor of 2this means it’s right sub-tree is too longinserted node was placed in the right sub-tree of N’s right child, NrightN’s right child have a balance factor of 1to balance this tree, need to replace N with it’s right child and make N the left child of NrightCase 1M(1)insert(Z)E(0)V(0)R(1)M(2)E(0)V(1)R(2)Z(0)rotate(R, V)M(1)E(0)Z(0)V(0)R(0)Re-Balancing a TreeCase 2a node, N, has a balance factor of 2this means it’s right sub-tree is too longinserted node was placed in the left sub-tree of N’s right child, NrightN’s right child have a balance factor of -1to balance this tree takes two stepsreplace Nright with its left child, Ngrandchildreplace N with it’s grandchild, NgrandchildCase 2M(1)insert(T)E(0)V(0)R(1)M(2)E(0)V(-1)R(2)rotate(V, T)T(0)M(2)E(0)T(1)R(2)V(0)rotate(T, R)M(1)E(0)V(0)T(0)R(0)Rotating a NodeIt can be seen from the previous examples that moving nodes really means rotating them aroundrotating lefta node, N, has its right child, Nright, replace itN becomes the left child of NrightNright’s left sub-tree becomes the right sub-tree of Nrotating righta node, N, has its left child, Nleft, replace itN becomes the right child of NleftNleft’s right sub-tree becomes the left sub-tree of NRotate LeftV(1)R(2)X(1)T(0)N(0)rotateLeft(R)X(1)V(0)Z(0)T(0)R(0)N(0)Z(0)Re-Balancing a TreeNotice that Case 1 can be handled by a single rotation leftor in the case of a -2 node, a single rotation rightCase 2 can be handled by a single rotation right and then leftor in the case of a -2 node, a rotation left and then rightRotate Leftvoid rotateLeft(TreeNode subRoot, TreeNode prev) {I) set tmp equal to subRoot.rightA) not necessary but makes things look nicerII) set prev equal to tmpA) caution: must consider rotating around rootIII) set subRoot’s right child to tmp’s left childIV) set tmp’s left child equal to subRootV) adjust balance factorsubRoot.balFactor -= (1 + max(tmp.balFactor, 0));tmp.balFactor -= (1 – min(subRoot.balFactor, 0)); }Re-Balancing the Treevoid balance(TreeNode subRoot, TreeNode prev) {I) if the right sub-tree is out of balance (subRoot.factor = 2)A) if subRoot’s right child’s balance factor is -1-> do a rotate right and then leftB) otherwise (if child’s balance factor is 1 or 0)-> do a rotate
View Full Document