CHAPTER 4 Trees §4 AVL Trees Target : Speed up searching (with insertion and deletion) root Tool : Binary search trees small large Problem : Although Tp = O( height ), but the height can be as bad as O( N ). 1/19 §4 AVL Trees 〖Example〗 2 binary search trees obtained for the months of the year Jan Feb Mar Apr June Aug May July Sept Dec Oct Entered from Jan to Dec Nov July Feb Aug Apr May Jan Dec Mar June A balanced tree 2/19 Oct Nov Sept Discussion 1: What are the average search times of these two trees? What if the months are entered in alphabetical order? §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 TL and TR as its left and right subtrees, then T is height balanced iff (1) TL and TR are height balanced, and of an empty tree The height (2) | hL hR | 1 where hL and hisR defined are thetoheights be –1. of TL and TR , respectively. 【Definition】The balance factor BF( node ) = hL hR . In an AVL tree, BF( node ) = 1, 0, or 1. 4 3 2 1 3/19 5 5 2 6 1 7 8 4 7 3 7 2 1 8 4 3 5 §4 AVL Trees 〖Example〗 Input the months Mar May 1 2 0 Mar Nov 1 2 01 0 1 0 May Single rotation May 0 00 00 Mar Nov Nov 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 In general: the root of the tree 1 A RR 0 4/19 1 BR B BL 0 RR BR B A Rotation AL AL BL A Insertion B 0 2 BR AL BL §4 AVL Trees Aug Apr 2 1 01 12 May 210 00 Mar Nov 10 Aug 0 Apr Discussion 2: What can we do now? 5/19 §4 AVL Trees Jan 21 2 1 01 Double Rotation 1 2 01 0 May 1 010 00 Aug 1 010 Nov 0 10 Apr Mar Aug LR 0 Jan Mar 1 0 May 0 0 Apr Jan 0 Nov Rotation In general: 1 0 B A CL Insertion 1 B 0 C BL 2 LR A BL CL 6/19 0 or 1 C 1 or 0 B AR CR OR 0 Rotation 1 C AR CR LR BL A CL CR OR AR §4 AVL Trees Dec July Feb 1 2 01 21 1 2 10 Mar Aug May 0 01 Apr Jan 1 0 1 0 0 0 Dec Nov July 0 Feb Discussion 3: What can we do now? 7/19 June Oct §4 AVL Trees Sept 1 0 1 0 2 1 01 21 Jan Mar Jan 1 1 12 1 0 1 00 02 1 0 1 1 12 1 0 0 1 Dec Mar May Dec Dec Mar 1 10 1 Aug Aug Aug 0 0 0 Apr Apr Apr 0 01 10 1 1 Jan 2 10 1 0 Feb July May Nov Feb0Home July work: Nov 1 00 1 0 1 Feb 0 July June 0 Nov 0 Oct 0 p.136June 4.16 May 0 0 June Oct Create an AVL Tree Sept 0 Note: Several bf’s might be changed if p.136even 4.22 we don’t need to reconstruct Double Rotation 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/19 §4 AVL Trees Let nh be the minimum number of nodes in a height balanced tree of height h. Then the tree must look like A h2 A h1 last question: OROne h1 h2 nh = nh1 + nh2 + 1 Obviously we have Tp = O( h ) where h is the height of the tree. But h = ? Fibonacci numbers: F0 = 0, F1 = 1, Fi = Fi1 + Fi2 for i > 1 nh = Fh+2 1, for h 0 i 1 1 5 Fibonacci number theory gives that Fi 1 1 5 nh 5 2 9/19 5 h 2 1 h O(ln n) 2 §5 Splay Trees Target : Any M consecutive tree operations starting from an empty tree take at most O(M log N) time. ButDoes node takes O(N) time Soif aone single operation might it mean that every Sure we can – that only means The bound is weaker. to access, wetake can keepN) accessing still O(N) time? operation takes O(log time? it No. It means that the that whenever a node is accessed, But the effect is the same: for M times, we? Then what’scan’t the point? amortized time is O(log N). it must be moved. There are no bad input sequences. Discussion 4: How shall we move the node? 10/19 §5 Splay Trees k1 k2 A A k2 A A A k4 k4 k3 k1 k3 k4 B k1 k2 k2 D k1 k3 k2 k3D k1 k3 B C C D B C B C C B k5 k4 k5 E k4 E k5 F E F E E D Does NOT work! 11/19 k5 k5 D F F F §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 §5 Splay Trees Try again -- For any nonroot node X , denote its parent by P and grandparent by G : Case 1: P is the root Rotate X and P Case 2: P is not the root Zig-zag G X Double rotation P D A B B G P X D X D C B C C Zig-zig 13/19 G X A A P P A Single rotation G B C D §5 Splay Trees Splaying not only moves the accessed node to the root, but also roughly halves k5 the depth of most nodes on the path. k4 F k13 E k2 AA B 14/19 D k1 B Ck3 C C k3 k4 D D E k5 F §5 Splay Trees Insert: 1, 2, 3, 4, 5, 6, 7 Find: 1 7 7 6 6 5 5 4 3 2 1 7 6 6 1 4 4 4 1 2 2 1 2 5 7 5 3 3 3 Read the 32-node example given in Figures 4.52 – 4.60 15/19 §5 Splay Trees Deletions: Step 1: Find X ; Home work: p.136 4.23 Discussion 6: Then what must we do? Please Accessdescription a splay tree complete the algorithm for Deletion. Are splay trees really better than AVL trees? 16/19 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 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 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