1 / 18

§4 AVL Trees

Target : Speed up searching (with insertion and deletion). Tool : Binary search trees. root. Problem : Although T p = O( height ), but the height can be as bad as O( N ). small. large. CHAPTER 4 Trees. §4 AVL Trees. 1/18. §4 AVL Trees. Mar. Aug. Oct. Dec. June. Nov.

efia
Download Presentation

§4 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. Target : Speed up searching (with insertion and deletion) Tool : Binary search trees root Problem : Although Tp = O( height ), but the height can be as bad as O( N ). small large CHAPTER 4 Trees §4 AVL Trees 1/18

  2. §4 AVL Trees Mar Aug Oct Dec June Nov Jan Feb Feb Mar Aug Jan July June May Apr Dec July Sept Apr May Oct Nov Sept 〖Example〗 2 binary search trees obtained for the months of the year Average search time = 3.5 Average search time of the skew tree = 6.5 Entered from Jan to Dec Average search time = 3.1 A balanced tree 2/18

  3. §4 AVL Trees 5 7 4 2 8 2 8 3 5 1 4 7 1 4 2 6 3 3 5 1 7 Adelson-Velskii-Landis (AVL) Trees (1962) 【Definition】An empty binary tree is height balanced. IfTis a nonempty binary tree withTLandTRas its left and right subtrees, then T is height balanced iff (1)TLandTRare height balanced, and (2) | hLhR |  1 wherehLandhRare the heights ofTLandTR , respectively. The height of an empty tree is defined to be –1. 【Definition】The balance factorBF( node ) = hLhR. In an AVL tree,BF( node ) = 1, 0, or 1. 3/18

  4. §4 AVL Trees 0 0 0 0 0 0 Mar Mar May Nov Nov May 1 2 1 0 Single rotation 0 0 1 2 0 1 0 A B B B A A RR RR BR AL AL Rotation Insertion AL BL BL BR BL BR 〖Example〗 Input the months Mar May Nov 1 2 1  The trouble maker Nov is in the right subtree’s right subtree of the trouble finder Mar. Hence it is called an RR rotation. A is not necessarily the root of the tree In general: 0 4/18

  5. §4 AVL Trees 0 0 0 0 0 Nov Apr May Mar Aug 2 1 1 1 1 0 Aug Apr 2 2 1 Discussion 1: What can we do now? 5/18

  6. §4 AVL Trees 0 0 0 0 0 0 0 0 0 0 0 0 May Aug Apr Nov Jan Mar Mar Aug Nov Apr May Jan 1 2 2 1 1 0 1 1 1 1 0 1 BL CL CR AR 0 1 1 0 AR AR 1 1 or 0 0 0 or 1 1 0 2 0 1 A B B A B C A C C LR LR LR OR BL BL Rotation Rotation Insertion CL CR CL CR OR Jan 2 Double Rotation 1 1 In general: 6/18

  7. §4 AVL Trees 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 July Dec Nov Apr Feb Aug July Mar Dec Feb May Jan Apr Mar May Nov Jan Aug 1 2 1 2 2 1 1 1 1 1 1 1 1 1 1 BR CR CL AL 0 1 AL AL 1 or 0 0 or 1 1 0 1 2 0 0 1 1 1 0 A B C A B C C A B RL RL RL OR BR BR Rotation Rotation Insertion CR CL CR CL OR Dec July Feb 2 2 1 1 In general: 7/18

  8. §4 AVL Trees 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 0 0 0 0 Feb July Aug Feb May Apr Nov Nov Jan Dec Dec July Aug Mar June June Feb Mar May May Apr June July Sept Nov Oct Mar Aug Apr Oct Dec 2 1 2 1 1 1 1 1 0 1 0 0 1 1 2 1 Jan Jan 1 1 2 1 0 1 1 June Oct Sept 1 2 1 1 1 1 Home work: p.136 4.16 Create an AVL Tree p.136 4.22 Double Rotation 1 2 1 1 1 1 Note: Several bf’s might be changed even if we don’t need to reconstruct the tree. Another option is to keep a height field for each node. Read the declaration and functions in Figures 4.42 – 4.48 8/18

  9. §4 AVL Trees OR A A h2 h2 h1 h1 Fibonacci number theory gives that Let nh be the minimum number of nodes in a height balanced tree of height h. Then the tree must look like One last question: Obviously we have Tp = O( h ) where h is the height of the tree. But h = ?  nh = nh1 + nh2 + 1 Fibonacci numbers: F0 = 0, F1 = 1, Fi = Fi1 + Fi2 for i > 1  nh = Fh+2  1, for h  0 9/18

  10. Target : Any M consecutive tree operations starting from an empty tree take at most O(M log N) time. §5 Splay Trees But if one node takes O(N) time to access, we can keep accessing it for M times, can’t we? Does it mean that every operation takes O(log N) time? So a single operation might still take O(N) time? Then what’s the point? Sure we can – that only means that whenever a node is accessed, it must be moved. The bound is weaker. But the effect is the same: There are no bad input sequences. No. It means that the amortized time is O(log N). Discussion 2: How shall we move the node? Why does your method work? 10/18

  11. §5 Splay Trees k5 k5 k5 k4 F k4 k5 k4 F F k1 k3 E k3 k1 E F E k1 k2 k5 k1 D k2 k2 D k3 k2 k4 k2 B k4 F C k1 A B D B k3 E k3 B E C A B C A C A A C D D Does NOT work! 11/18

  12. §5 Splay Trees 2 3 N N 1 N 3 2 2 3 3 1 2 1 N 2 2 3 1 1 1 2 3 An even worse case: Insert: 1, 2, 3, … N Find: 1 1 Find: 2 …… Find: N T (N) = O ( N2 ) 12/18

  13. §5 Splay Trees Rotate X and P G X Single rotation Double rotation P D P G X A A B C D B C G X P D P A X C G B A B C D Try again -- For any nonroot node X , denote its parent by P and grandparent by G: Case 1:P is the root Case 2:P is not the root Zig-zag Zig-zig 13/18

  14. §5 Splay Trees k5 k5 k4 k4 F F k1 k1 k3 E E k2 k2 k2 k3 D k4 k1 B B D A k3 k5 C D F A A C E C B Splaying not only moves the accessed node to the root, but also roughly halves the depth of most nodes on the path. 14/18

  15. §5 Splay Trees 7 7 6 7 1 6 5 6 6 5 4 1 7 4 4 3 4 2 5 1 2 5 2 3 2 1 3 3 Insert: 1, 2, 3, 4, 5, 6, 7 Find: 1 Read the 32-node example given in Figures 4.52 – 4.60 15/18

  16. §5 Splay Trees Deletions: X will be at the root.  Step 1: Find X ; Home work: p.136 4.23 Access a splay tree There will be two subtrees TL and TR .  Step 2: Remove X ; The largest element will be the root of TL , and has no right child.  Step 3: FindMax ( TL ) ;  Step 4: Make TR the right child of the root of TL . Are splay trees really better than AVL trees? 16/18

  17. Other Operations on Binary Search Trees  Sort: List the elements in increasing order Solution: inorder traversal.  Get Height: Compute the heights of the nodes Solution: postorder traversal.  Get Depth: Compute the depths of the nodes Solution: preorder traversal. 17/18

  18. Research Project 1 Binary Search Trees (30) This project requires you to implement operations on unbalanced binary search trees, AVL trees, and splay trees. You are to analyze and compare the performances of a sequence of insertions and deletions on these search tree structures. Detailed requirements can be downloaded from http://www.cs.zju.edu.cn/ds/ 18/18

More Related