Presentation is loading. Please wait.

Presentation is loading. Please wait.

§4 AVL Trees Target : Speed up searching (with insertion and deletion) Tool : Binary search trees root smalllarge Problem : Although T p = O( height ),

Similar presentations


Presentation on theme: "§4 AVL Trees Target : Speed up searching (with insertion and deletion) Tool : Binary search trees root smalllarge Problem : Although T p = O( height ),"— Presentation transcript:

1

2 §4 AVL Trees Target : Speed up searching (with insertion and deletion) Tool : Binary search trees root smalllarge Problem : Although T p = O( height ), but the height can be as bad as O( N ). CHAPTER 4 Trees 1/19

3 §4 AVL Trees 〖 Example 〗 2 binary search trees obtained for the months of the year Nov Oct Sept May Mar June July Dec Aug Apr Feb JanJuly June Mar May Oct SeptNov Jan Feb Aug AprDec Entered from Jan to Dec A balanced tree Discussion 1: What are the average search times of these two trees? What if the months are entered in alphabetical order? 2/19

4 §4 AVL Trees Adelson-Velskii-Landis (AVL) Trees (1962) 【 Definition 】 An empty binary tree is height balanced. If T is a nonempty binary tree with T L and T R as its left and right subtrees, then T is height balanced iff (1) T L and T R are height balanced, and (2) | h L  h R |  1 where h L and h R are the heights of T L and T R, respectively. 【 Definition 】 The balance factor BF( node ) = h L  h R. In an AVL tree, BF( node ) =  1, 0, or 1. 5 82 14 3 7 7 82 14 35 4 5 6 3 2 17 The height of an empty tree is defined to be –1. 3/19

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

6 §4 AVL Trees Aug May 0 11 Nov 00 22 11 Mar 01 1 Aug 0 Apr 0 1 2 2 5/19 Discussion 2: What can we do now? Discussion 2: What can we do now?

7 §4 AVL Trees May 0 11 Nov 00 22 11 Aug 01 1 11 Mar 0 0 Apr 0 Jan 0 1 11 2 LR Rotation Mar 0 11 May 0 11 22 11 Aug 010 11 Jan 0 0 Apr 0 Nov 0 In general: A 1 B 0 BLBL ARAR C 0 CRCR CLCL LR Insertion A 2 B 11 BLBL ARAR C 11 CRCR CLCL OR LR Rotation BLBL ARAR C 0 A  1 or 0 CRCR B 0 or 1 CLCL OR Double Rotation 6/19

8 §4 AVL Trees DecJuly Mar 0 11 May 0 11 22 11 Aug 01 1 11 Jan 0 Apr 0 Nov 0 July 0 Dec 0 Feb 0 11 1 22 2 Discussion 3: What can we do now? Discussion 3: What can we do now? 7/19

9 §4 AVL Trees July 0 Dec 0 11 Aug 01 22 11 Jan 01 0 11 Feb 0 0 Apr 0 Mar 0 11 May 0 11 22 11 1 Nov 0 JuneOctSept June 0 11 11 11 2 Nov 0 Dec 0 11 Aug 1 22 11 Feb 0 1 July 11 Apr 0 Mar 0 May 11 June 0 Jan 0 Oct 0 11 22 11 11 0 Dec 0 11 Aug 1 22 11 Feb 0 1 July 11 Apr 0 Mar 0 Nov 0 June 0 Jan 0 May 0 Sept 0 11 11 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. 8/19 Read the declaration and functions in Figures 4.42 – 4.48 Home work: p.136 4.16 Create an AVL Tree p.136 4.22 Double Rotation

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

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

12 k5k5 F k4k4 E k3k3 D k2k2 A k1k1 CB §5 Splay Trees k5k5 F k4k4 E k3k3 D k2k2 BA k1k1 C k5k5 F k4k4 E k2k2 BA k1k1 k3k3 DC k5k5 F k4k4 E k2k2 BA k1k1 k3k3 DC k4k4 E k5k5 F k2k2 BA k1k1 k3k3 DC Does NOT work! 11/19

13 §5 Splay Trees An even worse case: Discussion 5: Try to Insert 1, 2,..., N in increasing order, and then Find them in the same order. What will happen? What is the total access time T(N)? 12/19

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

15 §5 Splay Trees k5k5 F k4k4 E k3k3 D k2k2 A k1k1 CB k5k5 F k4k4 E k1k1 k3k3 DC k2k2 BA k1k1 k2k2 BA k4k4 k5k5 FE k3k3 DC Splaying not only moves the accessed node to the root, but also roughly halves the depth of most nodes on the path. 14/19

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

17 Discussion 6: Then what must we do? Please complete the algorithm description for Deletion. §5 Splay Trees Deletions:  Step 1: Find X ; Are splay trees really better than AVL trees? Home work: p.136 4.23 Access a splay tree 16/19

18  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/19

19 Research Project 1 Binary Search Trees (25) 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://acm.zju.edu.cn/dsaa/ 18/19

20 Research Project 2 Population (25) It is always exciting to see people settling in a new continent. As the head of the population management office, you are supposed to know, at any time, how people are distributed in this continent. The continent is divided into square regions, each has a center with integer coordinates (x, y). Hence all the people coming into that region are considered to be settled at the center position. Given the positions of the corners of a rectangle region, you are supposed to count the number of people living in that region. Note: there are up to 32768 different regions and possibly even more queries. Detailed requirements can be downloaded from http://acm.zju.edu.cn/dsaa/ 19/19


Download ppt "§4 AVL Trees Target : Speed up searching (with insertion and deletion) Tool : Binary search trees root smalllarge Problem : Although T p = O( height ),"

Similar presentations


Ads by Google