CS 600.226: Data Structures Micael Scatz Nov 4, 2016 Lecture 27: Treaps
ttps:www.nsf.govcrssprgmreureu_searc.jsp
Assignment 8: Due Tursday Nov 10 @ 10pm Remember: javac Xlint:all & cecstyle *.java & JUnit Solutions sould be independently written!
Part 1: AVL Trees
Constructing 7 5 8 7 1 3 4 9 5 8 empty 7 7 5 7 5 8 1 7 5 8 1 \ 3 7 5 8 1 \ 3 \ 4 7 5 8 1 9 \ 3 \ 4 Note te sape of a general BST will depend on te order of insertions We ope for O(lg(n)) eigt, but can end up being O(n) for nearly-sorted values We (probably) cant cange te order tat we see data, but wat can we cange?
Binary Searc Wat if we miss te median and do a 9010 split instead? n < = > < = > n10 + 9n10... + 81n100 < = >... + 729n1000 < = > + 6561n10000 < = > + 59049n100000 < = > + 531441n1000000 < = > + 4782969n10000000 [How many times can we cut 10% off a list?] + 9 i n10 i
Random Tree Heigt ## Trying all permutations of 3 distinct eys $ cat eigts.3.log tree[0]: 1 2 3 maxeigt: 3 tree[1]: 1 3 2 maxeigt: 3 tree[2]: 2 1 3 maxeigt: 2 tree[3]: 2 3 1 maxeigt: 2 tree[4]: 3 2 1 maxeigt: 3 tree[5]: 3 1 2 maxeigt: 3 numtrees: 6 average eigt: 2.66 maxeigts[0]: 0 0.00% maxeigts[1]: 0 0.00% maxeigts[2]: 2 33.33% maxeigts[3]: 4 66.67%
Random Tree Heigt $ cat eigts.4.log tree[0]: 1 2 3 4 maxeigt: 4 tree[1]: 1 2 4 3 maxeigt: 4 tree[2]: 1 3 2 4 maxeigt: 3 tree[3]: 1 3 4 2 maxeigt: 3 tree[4]: 1 4 3 2 maxeigt: 4 tree[5]: 1 4 2 3 maxeigt: 4 tree[6]: 2 1 3 4 maxeigt: 3 tree[7]: 2 1 4 3 maxeigt: 3 tree[8]: 2 3 1 4 maxeigt: 3 tree[9]: 2 3 4 1 maxeigt: 3 tree[10]: 2 4 3 1 maxeigt: 3 tree[11]: 2 4 1 3 maxeigt: 3 tree[12]: 3 2 1 4 maxeigt: 3 tree[13]: 3 2 4 1 maxeigt: 3 tree[14]: 3 1 2 4 maxeigt: 3 tree[15]: 3 1 4 2 maxeigt: 3 tree[16]: 3 4 1 2 maxeigt: 3 tree[17]: 3 4 2 1 maxeigt: 3 tree[18]: 4 2 3 1 maxeigt: 3 tree[19]: 4 2 1 3 maxeigt: 3 tree[20]: 4 3 2 1 maxeigt: 4 tree[21]: 4 3 1 2 maxeigt: 4 tree[22]: 4 1 3 2 maxeigt: 4 tree[23]: 4 1 2 3 maxeigt: 4 ## Trying all permutations of 4 distinct eys numtrees: 24 average eigt: 3.33 maxeigts[0]: 0 0.00% maxeigts[1]: 0 0.00% maxeigts[2]: 0 0.00% maxeigts[3]: 16 66.67% maxeigts[4]: 8 33.33%
Random Tree Heigt ## Trying all permutations of 5 distinct eys numtrees: 120 average eigt: 3.80 maxeigts[0]: 0 0.00% maxeigts[1]: 0 0.00% maxeigts[2]: 0 0.00% maxeigts[3]: 40 33.33% maxeigts[4]: 64 53.33% maxeigts[5]: 16 13.33%
Random Tree Heigt ## Trying all permutations of 10 distinct eys numtrees: 3,628,800 average eigt: 5.64 maxeigts[0]: 0 0.00% maxeigts[1]: 0 0.00% maxeigts[2]: 0 0.00% maxeigts[3]: 0 0.00% maxeigts[4]: 253440 6.98% maxeigts[5]: 1508032 41.56% maxeigts[6]: 1277568 35.21% maxeigts[7]: 479040 13.20% maxeigts[8]: 99200 2.73% maxeigts[9]: 11008 0.30% maxeigts[10]: 512 0.01%
Random Tree Heigt ## Trying all permutations of 11 distinct eys numtrees: 39,916,800 average eigt: 5.91 maxeigts[0]: 0 0.00% maxeigts[1]: 0 0.00% maxeigts[2]: 0 0.00% maxeigts[3]: 0 0.00% maxeigts[4]: 1056000 2.65% maxeigts[5]: 13501312 33.82% maxeigts[6]: 15727232 39.40% maxeigts[7]: 7345536 18.40% maxeigts[8]: 1950080 4.89% maxeigts[9]: 308480 0.77% maxeigts[10]: 27136 0.07% maxeigts[11]: 1024 0.00%
Random Tree Heigt ## Trying all permutations of 12 distinct eys numtrees: 479,001,600 average eigt: 6.17 maxeigts[0]: 0 0.00% maxeigts[1]: 0 0.00% maxeigts[2]: 0 0.00% maxeigts[3]: 0 0.00% maxeigts[4]: 3801600 0.79% maxeigts[5]: 121362560 25.34% maxeigts[6]: 197163648 41.16% maxeigts[7]: 112255360 23.44% maxeigts[8]: 36141952 7.55% maxeigts[9]: 7293440 1.52% maxeigts[10]: 915456 0.19% maxeigts[11]: 65536 0.01% maxeigts[12]: 2048 0.00%
Random Tree Heigt ## Trying all permutations of 13 distinct eys numtrees: 6,227,020,800 average eigt: 6.40 maxeigts[0]: 0 0.00% maxeigts[1]: 0 0.00% maxeigts[2]: 0 0.00% maxeigts[3]: 0 0.00% maxeigts[4]: 10982400 0.18% maxeigts[5]: 1099169280 17.65% maxeigts[6]: 1764912384 28.34% maxeigts[7]: 1740445440 27.95% maxeigts[8]: 658214144 10.57% maxeigts[9]: 159805184 2.57% maxeigts[10]: 25572352 0.41% maxeigts[11]: 2617344 0.04% maxeigts[12]: 155648 0.00% maxeigts[13]: 4096 0.00%
Random Tree Heigt ## Trying all permutations of 14 distinct eys numtrees: 87,178,291,200 average eigt: 6.63 maxeigts[0]: 0 0.00% maxeigts[1]: 0 0.00% maxeigts[2]: 0 0.00% maxeigts[3]: 0 0.00% maxeigts[4]: 21964800 0.03% maxeigts[5]: 10049994240 11.53% maxeigts[6]: 33305510656 38.20% maxeigts[7]: 27624399104 31.69% maxeigts[8]: 12037674752 13.81% maxeigts[9]: 3393895680 3.89% maxeigts[10]: 652050944 0.75% maxeigts[11]: 85170176 0.10% maxeigts[12]: 7258112 0.01% maxeigts[13]: 364544 0.00% maxeigts[14]: 8192 0.00%
Random Tree Heigt ## Trying all permutations of 15 distinct eys tree[44218000000]: 1 9 4 7 13 8 15 12 5 3 6 14 10 11 2 maxeigt: 6 tree[44219000000]: 1 9 4 7 13 11 5 6 2 14 10 3 8 12 15 maxeigt: 6 tree[44253000000]: 1 9 4 8 11 15 10 13 7 2 5 12 3 14 6 maxeigt: 7 tree[44254000000]: 1 9 4 8 12 3 6 11 5 15 2 14 10 7 13 maxeigt: 6 tree[44255000000]: 1 9 4 8 12 10 15 2 7 14 6 13 11 5 3 maxeigt: 7 tree[44256000000]: 1 9 4 8 12 13 6 3 11 14 2 5 7 10 15 maxeigt: 6 tree[44257000000]: 1 9 4 8 13 6 10 14 3 5 12 2 15 11 7 maxeigt: 6 tree[44258000000]: 1 9 4 8 13 2 3 5 10 12 14 7 11 6 15 maxeigt: 7 tree[44259000000]: 1 9 4 8 13 11 15 6 5 12 3 10 2 14 7 maxeigt: 6 tree[44260000000]: 1 9 4 8 13 14 5 10 2 11 12 6 15 3 7 maxeigt: 7 tree[44261000000]: 1 9 4 8 14 7 11 3 12 10 5 13 6 2 15 maxeigt: 7 tree[44262000000]: 1 9 4 8 14 10 3 5 15 6 11 12 13 7 2 maxeigt: 7 tree[44263000000]: 1 9 4 8 14 12 15 13 10 7 6 5 2 11 3 maxeigt: 7 tree[44264000000]: 1 9 4 8 14 15 13 11 7 5 10 2 3 12 6 maxeigt: 7 tree[44265000000]: 1 9 4 8 15 3 11 10 2 14 7 12 13 6 5 maxeigt: 7
AVL Tree 7 5 8 7 1 3 4 9 5 8 empty 7 7 5 7 5 8 1 5 3 7 1 8 5 3 7 \ 1 4 8 5 3 8 1 4 7 9 *not an actual AVL tree construction Self-balancing binary searc tree Named after te two Russian inventors: Adelson-Velsii and Landis First publised in 1962, one of te first efficient data structures
Balanced Trees Note tat we cannot require a BST to be perfectly balanced: 1 2 1 1 \ 2 3 2 1 3 1 \ 2 2 1 3 1 \ 3 2 1 \ 2 \ 3 Balanced Not Balanced Not Balanced Not Balanced Not Balanced Balanced Not Balanced Not Balanced Since neiter tree wit 2 eys is perfectly balanced we cannot insist on it AVL Condition: For every node n, te eigt of n s left and rigt subtree s differ by at most 1
Maintaining Balance Assume tat te tree starts in a sligtly unbalanced state: < > +1 < > +1 Inserting a new value can maintain te subtree eigts or 1 of 4 possible outcomes: +2 < > < > +1 +1 < > +1 +1 < > +2 Insert left, AVL Violation! Insert rigt, Rebalanced Insert left, Rebalanced Insert rigt, AVL Violation! Woooo! AVL Violations ;-)
Maintaining Balance Case 1a Case 4a +2 +2 x y +1 +1 Lets first assume te extra element goes into te leftmost or rigtmost subtree How can we restore balance to te trees?
Tree Rotations rigt-rotation(b) left-rotation(a) B A A B <A <B >A <B >A >B <A <B >A <B >A >B Note: Rotating a BST maintains te BST condition! Green < A < Brown < B < Purple
Restoring Balance Case 1a Case 4a +2 +2 x y +1 +1 rigt-rotate() left-rotate() x y +1 +1
Restoring Balance Case 1a Case 4a +2 +2 x y +1 +1 rigt-rotate() left-rotate() x y +1 Hooray! AVL Condition restored by rotating around! +1
Corner cases Case 1b Case 4b x y +1 +1 Will rotating around fix te problem? Wat else can we do?
Corner cases Case 1b Case 4b x a b y * * Heigt is * because could be or -1 * * left-rotate(x) rigt-rotate(y) a b x * * y * *
Case 1b Corner cases Case 4b a b x * * y * * rigt-rotate() left-rotate() a b x y * * * *
Case 1b Corner cases Case 4b a b x Hooray! Two rotations restored te balance to te tree * * Note tat it olds even if * = or * = -1 y * * rigt-rotate() left-rotate() a b x y * * * *
Complete Example Insert tese values: 4 5 7 2 1 3 ins(5) ins(2) 4 4 \ 5 ins(7) 4 \ 5 \ 7 5 4 7 l(4) ins(1) 5 4 7 2 5 4 7 2 1 Note: AVL Violations are fixed bottom up r(4) 5 2 7 1 4 ins(3) l(2) 5 2 7 1 4 3 5 4 7 2 1 3 4 2 5 \ 1 3 7 r(5)
Removing 6 3 8 1 4 7 9 7 6 8 6 6 3 8 3 9 \ 1 4 9 1 4 4 3 9 1 or 9 3 1 4 Remove leaf is easy Remove wit only one cild Is easy Remove Internal Node: Swap wit predecessor successor Remove from AVL just lie removing from regular BST: Find successor Swap wit tat element, Remove te node tat you just swapped. Mae sure to update te eigt fields, and rebalance if necessary
Implementation Notes Rotations can be applied in constant time! Inserting a node into an AVL tree requires O(lg n) time and guarantees O(lg(n)) eigt Trac te eigt of eac node as a separate field Te alternative is to trac wen te tree is lopsided, but just as ard and more error prone Don t recompute te eigts from scratc, it is easy to compute but requires O(n) time! Since we are guaranteeing te tree will ave eigt lg(n), just use an integer Only update te affected nodes Cec out Appendix B for some very useful tips on acing AVL trees!
Sample Application ttps:visualgo.netbst
Part 2: Treaps
BSTs versus Heaps p p < > p p BST All eys in left subtree of are <, all eys in rigt are > Tricy to balance, but fast to find Heap All cildren of te node wit priority p ave priority p Easy to balance, but ard to find (except minmax)
BSTs versus Heaps p p < > p p BST All eys in left subtree of are <, all eys in rigt are > Tricy to balance, but fast to find Heap All cildren of te node wit priority p ave priority p Easy to balance, but ard to find (except minmax)
Treap p < p > p A treap is a binary searc tree were te nodes ave bot user specified eys () and internally assigned priorities (p). Wen inserting, use standard BST insertion algoritm, but ten use rotations to iteratively move up te node wile it as lower priority tan its parent (analogous to a eap, but wit rotations)
A (boring) example Insert te following pairs: 71, 22, 13, 84, 35, 56, 47 71 22 71 13 71 22 13 71 22 84 71 22 84 13 35 71 22 84 13 35 \ 56 71 22 84 13 35 \ 56 47 Te priorities were always increasing, so we never ad to apply any of te rotations. boooooring and unbalanced
A (better) example 73 ins(20) 73! 20 ins(82) r(7) 20 \ 73 Insert te following pairs: 73, 20, 17, 82, 39, 51, 47 20 17 73 \! l(7) 82 20 17 82 73 ins(17) ins(39) 20 17 73 20 17 82 73 39 20 17 82 73 39 ins(51) \! 51 l(3) r(7) r(8) 20 17 82 73! 51 39 20 17 82! 51 39 73 20 17 51 39 82 ins(47) \! 47 73 20 l(3) 20 17 51 17 51 39 82 73 47 39 82 73 Notice tat we inserted te same eys, but wit different priorities Just by canging te priorities, we can improve te balance!
Treap Reflections Insert te following pairs: 71, 22, 13, 84, 35, 56, 47 71 Since te priorities are in sorted order, becomes a standard BST and may ave O(n) eigt 22 84 13 35 \ 56 47 Insert te following pairs: 73, 20, 17, 82, 39, 51, 47 Wit a standard BST, for 2 to be root it would ave to be te first ey inserted, and 5 would ave to proceed all te oter eys except 1, It is as if we saw te sequence: 2,5,8,7,1,4,3 Note priorities in sorted order: 20, 51, 82, 73, 17, 47, 39 20 17 51 47 82 39 73
Wat priorities sould we assign to maintain a balanced tree? Mat.random() Using random priorities essentially suffles te input data (wic migt ave bad linear eigt) into a random permutation tat we expect to ave O(log n) eigt J It is possible tat we could randomly suffle into a poor tree configuration, but tat is extremely rare. In most practical applications, a treap will perform just fine, and will often outperform an AVL tree tat guarantees O(log n) eigt but as iger constants
Self Balancing Trees Understanding te distinction between different inds of balanced searc trees: AVL trees guarantee worst case O(log n) operations by carefully accounting for te tree eigt splay trees guarantee amortized O(log n) operations by periodically applying a certain set of rotations (see lecture notes) treaps guarantee expected O(log n) operations by selecting a random permutation of te input data So if you ave to play it safe and don t trust your random numbers, AVL trees are te way to go. If you can live wit te occasional O(n) operation every now and ten and you still don t trust your random numbers, splay trees are te way to go. And if you trust your random numbers, treaps are te way to go.
Next Steps 1. Reflect on te magic and power of Binary Searc Trees! 2. Assignment 8 due Tursday November 10 @ 10pm
Welcome to CS 600.226 ttp:www.cs.ju.edu~cs226 Questions?