1 / 19

CS 261 – Data Structures

CS 261 – Data Structures. AVL Trees. Binary Search Tree: Balance. Complexity of BST operations: proportional to the length of the path from the root to the node being manipulated In a well balanced tree, the length of the longest path is roughly log n

Download Presentation

CS 261 – Data Structures

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. CS 261 – Data Structures AVL Trees

  2. Binary Search Tree: Balance • Complexity of BST operations: proportional to the length of the path from the root to the node being manipulated • In a well balanced tree, the length of the longest path is roughly log n • E.g.: 1 million entries  longest path is log2 1,048,576 = 20. • For a thin, unbalanced tree, operations become O(n): • E.g.: elements are added to tree in sorted order • BALANCE IS IMPORTANT!

  3. Requiring Complete Trees (Bad Idea) • Recall that a complete binary tree has the shortest overall path length for any binary tree • The longest path in a complete binary tree with n elements is guaranteed to be no longer than ceiling(log n) • If we can keep our tree complete, we’re set for fast search times • Very costly to maintain a complete binary tree

  4. Height Balanced Trees • Instead, use height-balanced binary trees: for each node, the height difference between the left and right subtrees is at most one • Trees are locally balanced, but globally they can be slightly more unbalanced

  5. Height-Balanced Trees • Mathematically, the longest path in a height-balanced tree is, at worst, 44% longer than log n • This is just a constant multiplier (so it is not significant in terms of asymptotic complexity) • Therefore, algorithms on height-balanced trees that run in time proportional to the path length are still O(log n) • (Proof is sort of cute, ties heights to Fibonocci numbers)

  6. 1(0) 1(2) 2(1) 2(1) 3(0) 3(0) AVL Trees • Named after the inventors’ initials • Maintain the height balanced property of a BST • AVL trees add an integer height field to each node: • null node has a height of –1 • A node is unbalanced when the absolute height difference between the left and right subtrees is greater than one • How does it maintain the height balanced property? • When unbalanced, performs a rotation to balance the tree Node data Height field 1(2) Unbalanced node Rotate left

  7. AVL Implementation: AVLNode - heights struct avlNode { // Inner node class. EleType value; ... int height; }; int h(struct avlNode * current) { if (current == nil) return -1; return current->height; } void setHeight(struct avlNode * current) { int lch = h(current->leftChild); int rch = h(current->rightChild); if (lch < rch) current->height = 1 + rch; else current->height = 1 + lch; }

  8. 1(0) 2(1) 2(1) 3(0) 3(0) AVL Implementation: Rotations struct avlNode * rotateLeft(struct avlNode * n) { // Rotate node left. struct avlNode *newtop = n->right; // Right child becomes new “top” node. n->right = newtop->left; // Old right child’s left child becomes new right child. newtop->left = n; // new top left child is old top. setHeight(n); // Reset the height of the interior node. setHeight(newtop); // Reset the height of the top node. return newtop; } struct avlNode * rotateRight (struct avlNode * n) { .. // is similar } 1(2) Rotate left

  9. 2(3) 2(1) 4(2) 4(2) 1(0) 1(0) 3(0) 3(0) 5(1) 5(1) 6(0) 6(0) AVL Trees: Rotation Pseudocode Pseudocode to rotate current (“top”) node left: • New top node is the current node’s right child • Current node’s new right child is the new top node’s (old right child’s) left child • New top’s left child is the current node • Set height of current node • Set height of new top node • Return new top node New “top” node Current “top” node Rotate left

  10. 1(2) 1(1) 3(1) 3(2) 2(0) 2(0) AVL Trees: Double Rotation • Sometimes a single rotation will not fix the problem: • Happens when an insertion is made on the left (or right) side of a node that is itself a heavy right (or left) child Unbalanced “top” node Doesn’t work!!! “Heavy” right child Rotate left

  11. 1(2) 1(2) 1(0) 3(1) 2(1) 2(1) 2(0) 3(0) 3(0) AVL Trees: Double Rotation • Sometimes a single rotation will not fix the problem: • Happens when an insertion is made on the left (or right) side of a node that is itself a heavy right (or left) child • Fortunately, this case is easily handled by rotating the child before the regular rotation: • First rotate the heavy right (or left) child to the right (or left) • Rotate the “top” node to the left (or right) Unbalanced “top” node “Heavy” right child Rotate top node left Rotate heavy child right

  12. AVL Trees: Balacing Pseudocode Balancing pseudocode (to rebalance an unbalanced node): If left child is tallest: If left child is heavy on the right side: // Double rotation needed. Rotate the left child to the left Rotate unbalanced (“top”) node to the right Else: // Right child is the tallest. If right child is heavy on the left side: // Double rotation needed. Rotate the right child to the right Rotate unbalanced (“top”) node to the left Return new “top” node

  13. 3(3) 3(4) 2(1) 2(1) 8(2) 8(3) 1(0) 1(0) 9(0) 9(0) 5(1) 5(2) 4(0) 4(0) 6(0) 6(1) 7(0) AVL Trees: Double Rotation Example Balanced Tree Unbalanced Tree Unbalanced “top” node Add data: 7 “Heavy” left child Added to right side of heavy left child

  14. 3(4) 3(4) 2(1) 2(1) 8(3) 1(0) 1(0) 9(0) 9(0) 8(2) 5(2) 5(3) 4(0) 4(0) 6(1) 6(1) 7(0) 7(0) AVL Trees: Double Rotation Example Unbalanced Tree Tree Still Unbalanced Unbalanced “top” node (still) Single rotation

  15. 3(4) 3(4) 2(1) 2(1) 8(3) 8(3) 1(0) 1(0) 9(0) 9(0) 5(1) 5(2) 4(0) 4(0) 6(2) 6(1) 7(0) 7(0) AVL Trees: Double Rotation Example Unbalanced Tree Tree Still Unbalanced, but … Rotate heavy child “Heavy” left child

  16. 3(3) 3(4) 2(1) 2(1) 8(3) 1(0) 1(0) 9(0) 9(0) 8(1) 5(1) 5(1) 6(2) 4(0) 4(0) 6(2) 7(0) 7(0) AVL Trees: Double Rotation Example Unbalanced Tree (after 1st rotation) Tree Now Balanced Rotate top node Unbalanced “top” node

  17. AVL Trees head to head with skip lists • How do they compare to skip lists? • Usually faster • And, they use less memory • There are other types of height balanced trees. The JAVA library uses red/black trees (class TreeSet). Similar idea, slightly faster still.

  18. AVL Trees: Tree Sort Deja Vu • An AVL tree can easily implement SortAlgorithm: • Copy the values of the data into the tree • Copy them out using an in-order traversal • Execution time  O(n log n): • Matches that of quick sort in benchmarks • Unlike quick sort, AVL trees don’t have problems if data is already sorted or almost sorted (which degrades quick sort to O(n2)) • However, requires extra storage to maintain both the original data buffer (e.g., a dyArray) and the tree structure

  19. Now Your Turn • Any Questions • Going to do a quick overview of a few other types of trees we won’t be looking at, • Then do worksheet • Start by showing AVL insertions of 1 to 7 into an empty tree

More Related