1 / 38

Balanced Binary Search Tree

Balanced Binary Search Tree. 황승원 Fall 2010 CSE, POSTECH. height is O(log n) , where n is the number of elements in the tree AVL (Adelson-Velsky and Landis) trees red-black trees get, put, and remove take O(log n) time. Balanced Binary Search Trees. Indexed AVL trees

dian
Download Presentation

Balanced Binary Search Tree

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. Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH

  2. height is O(log n), where n is the number of elements in the tree AVL (Adelson-Velsky and Landis) trees red-black trees get, put, and remove take O(log n) time Balanced Binary Search Trees

  3. Indexed AVL trees Indexed red-black trees Indexed operations alsotake O(log n) time Balanced Binary Search Trees

  4. weight balanced binary search trees 2-3 & 2-3-4 trees AA trees B-trees BBST etc. Balanced Search Trees

  5. AVL Tree Definition • Binary tree. • If T is a nonempty binary tree with TL and TR as its left and right subtrees, then T is an AVL tree iff • TL and TR are AVL trees, and • |hL – hR|  1 where hL and hR are the heights of TL and TR, respectively

  6. This is an AVL tree. Balance Factors -1 1 1 -1 0 1 0 0 -1 0 0 0 0

  7. The height of an AVL tree that has n nodes is at most 1.44 log2 (n+2). Nh=Nh-1 + Nh-2+1 – reminds you anything? Fn=Fn-1 + Fn-2 Nh=Fh+2-1 when Height

  8. -1 10 1 1 7 40 -1 0 1 0 45 3 8 30 0 -1 0 0 0 60 35 1 20 5 0 25 AVL Search Tree

  9. put(9) -1 10 0 1 1 7 40 -1 0 1 -1 0 45 3 8 30 0 -1 0 0 0 0 60 35 1 9 20 5 0 25

  10. put(29) -1 10 1 1 7 40 -1 0 1 0 45 3 8 30 0 -1 0 0 0 -2 60 35 1 20 5 0 -1 RR imbalance => new node is in right subtree of right subtree of blue node (node with bf = -2) 25 0 29

  11. put(29) -1 10 1 1 7 40 -1 0 1 0 45 3 8 30 0 0 0 0 0 60 35 1 25 5 0 0 20 29 RR rotation.

  12. RR LL RL LR AVL Rotations

  13. Imbalance Types • After an insertion, when the balance factor of node A is –2 or 2, the node A is one of the following four imbalance types • LL: new node is in the left subtree of the left subtree of A • LR: new node is in the right subtree of the left subtree of A • RR: new node is in the right subtree of the right subtree of A • RL: new node is in the left subtree of the right subtree of A

  14. Rotation Definition • To switch children and parents among two or three adjacent nodes to restore balance of a tree. • A rotation may change the depth of some nodes, but does not change their relative ordering.

  15. AVL Rotations • To balance an unbalanced AVL tree (after an insertion), we may need to perform one of the following rotations: LL, RR, LR, RL

  16. An LL Rotation

  17. An LR Rotation

  18. RR and RL? • Symmetric!

  19. -1 10 1 1 7 40 0 0 1 -1 3 8 30 45 0 0 0 -1 0 1 5 20 35 60 0 25 29 Inserting into an AVL Search Tree Insert(29) • Where is 29 going to be inserted into? • After the insertion, is the tree still an AVL search tree? (i.e., still balanced?)

  20. -1 10 1 1 7 40 0 0 1 -1 3 8 30 45 0 0 0 0 1 5 20 35 60 -2 -1 25 0 29 Inserting into an AVL Search Tree Insert(29) • What are the balance factors for 20, 25, 29? • RR imbalance  new node is in the right subtree of right subtree of node 20 (node with bf = -2)

  21. -1 10 1 1 7 40 0 0 1 -1 3 8 30 45 0 0 0 0 0 1 5 25 35 60 0 0 20 29 After RR Rotation • What would the left subtree of 30 look like after an RR rotation? • After the RR rotation, is the resulting tree an AVL search tree now?

  22. Deletion from an AVL Search Tree • Deletion of a node may also produce an imbalance • Imbalance incurred by deletion is classified intothe types R0, R1, R-1, L0, L1, and L-1 • Rotation is also needed for rebalancing • Read the observations after deleting a node from an AVL search tree • Read Section 11.2.6 for Deletion from an AVL search tree

  23. An R0 Rotation

  24. An R1 Rotation

  25. An R-1 Rotation

  26. Complexity of Dictionary Operations:Search, Insert, and Delete Data Structure Worst Case Expected Hash Table O(n) O(1) Binary SearchTree O(n) O(logn) BalancedBinary Search Tree O(logn) O(logn) • n is the number of elements in dictionary.

  27. Complexity of Other OperationsAscend, Search(rank), and Delete(rank) Data Structure ascend get and remove Hash Table O(D+nlogn) O(D+nlogn) O(n) Indexed BST O(n) IndexedBalanced BST O(n) O(log n) • D is the number of buckets.

  28. Colored Nodes Definition Binary search tree. Each node is colored red or black. Root and all external nodes are black. No root-to-external-node path has two consecutive red nodes. All root-to-external-node paths have the same number of black nodes Red Black Trees

  29. 10 7 40 45 3 8 30 60 35 1 20 5 25 Example Red Black Tree

  30. Properties • The height of a red black tree that has n (internal) nodes is between log2(n+1) and 2log2(n+1).

  31. Lemma 16.2 • r : rank of root

  32. Insert • New pair is placed in a new node, which is inserted into the red-black tree. • New node color options. • Black node => one root-to-external-node path has an extra black node (black pointer). • Hard to remedy. • Red node => one root-to-external-node path may have two consecutive red nodes (pointers). • May be remedied by color flips and/or a rotation.

  33. gp pp d p c a b Classification Of 2 Red Nodes/Pointers • XYz • X => relationship between gp and pp. • pp left child of gp => X = L. • Y => relationship between pp and p. • p right child of pp => Y = R. • z = b (black) if d = null or a black node. • z = r (red) if d is a red node. LLb

  34. XYr • Color flip. gp gp pp pp d d p p c c a b a b • Continue rebalancing if changing gp to red causes problems

  35. LLb • Rotate. gp y z pp x z y d p x a b c d c a b • Done! • Same as LL rotation of AVL tree.

  36. LRb • Rotate. gp y z pp x z x d a b c d a p y b c • Done! • Same as LR rotation of AVL tree. • RRb and RLb are symmetric.

  37. Red-black Tree Animation • http://www.ece.uc.edu/~franco/C321/html/RedBlack/redblack.html • java.util.TreeMap => red black tree

  38. Coming Up Next • READING: Ch 16.1~2 • NEXT: Graphs (Ch 17.1~7)

More Related