AVL/Splay Trees

AVL/Splay Trees

AVL/Splay Trees S. Thiel Self-Organizing Lists Balanced Trees AVL Trees AVL/Splay Trees Splay Trees References S. Thiel1 1Department of Computer Science & Software Engineering Concordia University July 17, 2019 1/43 Outline AVL/Splay Trees S. Thiel Self-Organizing Lists Balanced Trees AVL Trees Self-Organizing Lists Splay Trees References Balanced Trees AVL Trees Splay Trees References 2/43 Self-Organizing Lists [1, p.307] AVL/Splay Trees S. Thiel Self-Organizing Lists Balanced Trees AVL Trees Splay Trees References I We have looked at organizing list by value I That's not the only option I If we want to find something quickly, are there faster options? 3/43 Organizing by Frequency 1 [1, p.307] AVL/Splay Trees S. Thiel Self-Organizing Lists If we just want a fast access to what we're looking for Balanced Trees I AVL Trees Splay Trees I We can organize by frequency, as this is often cheap References I This applies when we know about the frequency: I e.g.: for ki elements where each value has probability pi I When sorting linearly, assuming the above frequencies were ordered for convenience: I Total comparisons to find each value once is: Cn = 1p0 + 2p1 + ::: + npn−1 2 I In general, Cn is a function of n , and thus each find is Θ(n) in the average case 1 1being the case where the probabilities are followed 4/43 Organizing by Frequency 2 [1, p.307-308] AVL/Splay Trees S. Thiel Self-Organizing Lists Balanced Trees AVL Trees Splay Trees I What if pn are geometric series? References 1 I What if pi = 2i+1 ? I Cn = 1p0 + 2p1 + ::: + npn−1 I which is just: n−1 X i n + 1 = 2 − 2i 2n−1 i=1 I and as n approaches 1 it is 2 5/43 Access Patterns [1, p.309] AVL/Splay Trees S. Thiel Self-Organizing Lists Balanced Trees AVL Trees Splay Trees References I 80/20 rule for data access. I It turns out this is common. I When 80/20 applies, sorting by frequency generally yields good results 6/43 How Else To Self-Organize? 1/2 [1, p.310-311] AVL/Splay Trees S. Thiel Self-Organizing Lists I Store access counts, move to front of list if count Balanced Trees exceeds count of preceeding value AVL Trees Splay Trees I must store counts References I doesn't deal well with changing frequency of access I Move-to-front: If something is accessed, move it to the front I Does not work well in arrays I responds well to localized frequency adjustment for short periods of time I responds poorly when things are accessed sequentially, or repeatedly sequentially I bounded: when searching for n values, the worst case is twice as long as searching for those n values if it were ordered conveniently. (doesn't make it much worse) 7/43 How Else To Self-Organize? 2/2 [1, p.311] AVL/Splay Trees S. Thiel Self-Organizing Lists Balanced Trees AVL Trees Splay Trees I Transpose: swap records found with the record beforehand References I good with both arrays and lists I frequently used records tend towards the front. I records that stop being accessed frequently will move to the back. I pathological cases can lead to all long accesses for bad sequences without repeats, but rare. I A common variation swaps with something a few steps ahead. 8/43 Self-Organizing List Performance [1, p.312] AVL/Splay Trees S. Thiel Self-Organizing Lists Balanced Trees AVL Trees Splay Trees References I They don't beat Θ (log n) performance in general I They do well in certain cases, so should not be overlooked I It can often behave like Θ (1) because of access patterns 9/43 Self-Organizing List Example [1, p.312] AVL/Splay Trees S. Thiel Message passing example: I Self-Organizing Lists I Sender and Receiver, sending text messages Balanced Trees I For each word, AVL Trees Splay Trees I it has never been seen, put it on the front of the list References and send it I if it has been seen before, send the index, move it to the front of the list I In this way, both sender and receiver keep matching lists and can decode properly I The order things are sent in matters (out of order sends would break this) I The car on the left hit the car I left. I The car on 3 left hit 3 5 I 5. I In general, while the above seems trivial, variants on this are common file/text encoding strategies where duplication is expected. 10/43 More Reliable Performance AVL/Splay Trees S. Thiel Self-Organizing Lists Balanced Trees AVL Trees Splay Trees I Self-Organizing lists are useful in special cases, but have References problem areas I BSTs have problem areas too, namely when they become unbalanced I Can we apply self-organizing approaches? I First let's consider what the implications of balancing are. 11/43 Balanced Trees AVL/Splay Trees S. Thiel Self-Organizing Lists Balanced Trees AVL Trees Splay Trees References I BSTs can become unbalanced I makes search expensive I adjust access, improve performance I ensuring complete binary tree too expensive I relax requirement, good enough (can we cheaply mostly fix it?) 12/43 AVL Tree vs. Splay Tree AVL/Splay Trees S. Thiel Self-Organizing Lists Balanced Trees AVL Trees Splay Trees References I AVL constrains difference between depth of sub{trees I SPLAY improves balance on each access I both have advantages 13/43 AVL Tree AVL/Splay Trees S. Thiel Self-Organizing Lists Balanced Trees AVL Trees Splay Trees I named for its inventors Adelson-Velskii and Landis) References I left and right sub{trees differ in depth by at most 1 I max depth is O (logn) I search is therefore O (logn) I updates can be done in time proportional to search I must be cautious with duplicate values, as rotations may break the BST property [1, p.454 E13.8] 14/43 AVL Tree Balance AVL/Splay Trees S. Thiel Self-Organizing Lists Balanced Trees AVL Trees Splay Trees References I Each node knows its level of unbalance I −1, 0 or 1 I tree starts balanced according to this I changes propagate up to root I when can we stop early? I propagation stops logically 15/43 AVL Tree Insertion [1, p.435] AVL/Splay Trees S. Thiel Self-Organizing Lists Balanced Trees AVL Trees Splay Trees References Figure: 24=-2, 7=-2: Unbalanced AVL with an insertion [1, p.435]. 16/43 AVL Tree Rotations AVL/Splay Trees S. Thiel Self-Organizing Lists Balanced Trees AVL Trees I what are these changes? Splay Trees References I we call them rotations I AVL needs a single rotation or double rotation I rotation about node with bad balance I if sign of imbalance is the same, single I if sign of imbalance differs, double I double rotation starts by making sign of imbalance the same I rotate on node with awkward balance 17/43 Single Rotation Example [1, p.436] AVL/Splay Trees S. Thiel Self-Organizing Lists Balanced Trees AVL Trees Splay Trees References Figure: X = -1, S = -2: Single Rotation Fix [1, p.436]. 18/43 Double Rotation Example [1, p.436] AVL/Splay Trees S. Thiel Self-Organizing Lists Balanced Trees AVL Trees Splay Trees References Figure: X = +2, S = -2: Double Rotation Fix [1, p.436]. 19/43 Single Rotation 1 AVL/Splay Trees S. Thiel Self-Organizing Lists Balanced Trees -1 AVL Trees 37 Splay Trees References -2 1 24 42 -2 0 0 1 7 32 40 43 -1 0 2 120 0 1 20/43 Single Rotation 2 AVL/Splay Trees S. Thiel Self-Organizing Lists Balanced Trees AVL Trees 0 Splay Trees 37 References -1 1 24 42 0 0 0 1 2 32 40 43 0 0 0 1 7 120 21/43 Double Rotation 1 AVL/Splay Trees S. Thiel Self-Organizing Lists Balanced Trees -1 AVL Trees 37 Splay Trees References -2 1 24 42 -2 0 0 1 7 32 40 43 1 0 2 120 0 5 22/43 Double Rotation 2 AVL/Splay Trees S. Thiel Self-Organizing Lists Balanced Trees AVL Trees 0 Splay Trees 37 References -1 1 24 42 0 0 0 1 5 32 40 43 0 0 0 2 7 120 23/43 But Shafer's Example has a Duplicate node? AVL/Splay Trees S. Thiel Self-Organizing Lists Balanced Trees AVL Trees Splay Trees References I Duplicate nodes are problematic I Rotation can break BST ≥ rule 24/43 Duplicate Doom 1 AVL/Splay Trees S. Thiel Self-Organizing Lists -1 Balanced Trees 37 AVL Trees Splay Trees References -2 1 24 42 2 0 0 1 5 32 40 43 1 0 5 120 0 5 Is this a BST? 25/43 Duplicate Doom 2 AVL/Splay Trees S. Thiel Self-Organizing Lists Balanced Trees AVL Trees -1 37 Splay Trees References -1 1 24 42 0 0 0 1 5 32 40 43 0 0 0 5 5 120 Is this a BST? 26/43 AVL Tree Deletion AVL/Splay Trees S. Thiel Self-Organizing Lists Balanced Trees AVL Trees Splay Trees I addition looks easy. it is! References I deletion is only slightly trickier I can we optimize? I if balance is 1 grab smallest in right subtree? I if balance is −1 grab biggest in left subtree? I at least it cannot hurt I work up from secondary deletion 27/43 AVL Deletion 1 try 1 AVL/Splay Trees S.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    43 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us