CS : Data Structures

Similar documents
Binary Search Tree (Part 2 The AVL-tree)

CSS 343 Data Structures, Algorithms, and Discrete Math II. Balanced Search Trees. Yusuf Pisan

Balanced Trees. Balanced Trees Tree. 2-3 Tree. 2 Node. Binary search trees are not guaranteed to be balanced given random inserts and deletes

RBT Operations. The basic algorithm for inserting a node into an RBT is:

Topic 23 Red Black Trees

h = v h 2 = height of the object is negative for inverted image and positive for erect image. is always positive. Direction of incident light

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims. Lecture 17: Heaps and Priority Queues

Loading transformers with non sinusoidal currents

OPTI-502 Optical Design and Instrumentation I John E. Greivenkamp Homework Set 5 Fall, 2018

Indirect Measurement

MITOCW 6. AVL Trees, AVL Sort

CSE 100: RED-BLACK TREES

5 AVL trees: deletion

PRIORITY QUEUES AND HEAPS. Lecture 19 CS2110 Spring 2014

Self-Adjusting Binary Search Trees. Andrei Pârvu

School of Electrical and Computer Engineering, Cornell University. ECE 303: Electromagnetic Fields and Waves. Fall 2007

Random Binary Search Trees. EECS 214, Fall 2017

CSE 100: BST AVERAGE CASE AND HUFFMAN CODES

Unit 5 Waveguides P a g e 1

PRIORITY QUEUES AND HEAPS

11.2 Areas of Trapezoids and Kites

I ve downloaded the app, now where do I tap?

Punctured Binary Turbo-Codes with Optimized Performance

1 Permutations. Example 1. Lecture #2 Sept 26, Chris Piech CS 109 Combinatorics

CS188 Spring 2010 Section 3: Game Trees

MITOCW ocw lec11

Introduction to. Algorithms. Lecture 10. Prof. Constantinos Daskalakis CLRS

Directional Derivative, Gradient and Level Set

Chapter 7: Sorting 7.1. Original

Installation Instructions

1 Permutations. 1.1 Example 1. Lisa Yan CS 109 Combinatorics. Lecture Notes #2 June 27, 2018

Permutations. Example 1. Lecture Notes #2 June 28, Will Monroe CS 109 Combinatorics

CS188 Spring 2010 Section 3: Game Trees

Trigonometric Functions of any Angle

ON TWO-PLANE BALANCING OF SYMMETRIC ROTORS

5.3 Sum and Difference Identities

A REVIEW OF THE NEW AUSTRALIAN HARMONICS STANDARD AS/NZS

IMAGE ILLUMINATION (4F 2 OR 4F 2 +1?)

Spectrum Sharing with Multi-hop Relaying

PRIORITY QUEUES AND HEAPS. Slides of Ken Birman, Cornell University

Grade 10 Mathematics: Question Paper 2

Image Reconstruction Based On Bayer And Implementation On FPGA Sun Chen 1, a, Duan Xiaofeng 2, b and Wu Qijing 3, c

and 6.855J. Network Simplex Animations

DATA STRUCTURE TREES UNIT II

Calculation of Antenna Pattern Influence on Radiated Emission Measurement Uncertainty

Welcome to the world of professional chairs. lab black economy

ON THE IMPACT OF RESIDUAL CFO IN UL MU-MIMO

This study concerns the use of machine learning based

ANALYSIS OF HARMONIC DISTORTION LEVELS ON A DISTRIBUTION NETWORK

Estimation of Dielectric Constant for Various Standard Materials using Microstrip Ring Resonator

ELEC 546 Lecture #9. Orthogonal Frequency Division Multiplexing (OFDM): Basic OFDM System

Homework Assignment #1

Image Feature Extraction and Recognition of Abstractionism and Realism Style of Indonesian Paintings

Abstract 1. INTRODUCTION

Discrete Mathematics and Probability Theory Spring 2014 Anant Sahai Note 11

A Note on Downup Permutations and Increasing Trees DAVID CALLAN. Department of Statistics. Medical Science Center University Ave

Artificial Intelligence Lecture 3

An Efficient Handoff Scheme Using a Minimum Residual Time First Scheme

Multi-Objectivity for Brain-Behavior Evolution of a Physically-Embodied Organism

8.5 Area of Parallelograms

Center for Academic Excellence. Area and Perimeter

YOUR COMPLETE GUIDE TO MASTERING THE SALE

CAPACITY OF MULTIPLE ACCESS CHANNELS WITH CORRELATED JAMMING

Multi-Objectivity for Brain-Behavior Evolution of a Physically-Embodied Organism

Copyright 2005, Favour Education Centre. Mathematics Exercises for Brilliancy Book 3. Applications of trigonometry.

Contour Measuring System CONTRACER CV-1000/2000

Francesc Casanellas C. Sant Ramon, Aiguafreda - Spain NATURAL PERSPECTIVE

Past questions from the last 6 years of exams for programming 101 with answers.

SELF-TAPPING SCREWS AS REINFORCEMENTS IN BEAM SUPPORTS. I Bejtka H J Blaß. Lehrstuhl für Ingenieurholzbau und Baukonstruktionen Universität Karlsruhe

Contour Measuring System CONTRACER CV-1000/2000

Power Quality Analysis Using An Adaptive Decomposition Structure

Channel Estimation Filter Using Sinc-Interpolation for UTRA FDD Downlink

COMP 2804 solutions Assignment 4

Hypercube Networks-III

ON THE USE OF MULTI-HARMONIC LEAST-SQUARES FITTING FOR THD ESTIMATION IN POWER QUALITY ANALYSIS

CS 540-2: Introduction to Artificial Intelligence Homework Assignment #2. Assigned: Monday, February 6 Due: Saturday, February 18

MA/CSSE 473 Day 13. Student Questions. Permutation Generation. HW 6 due Monday, HW 7 next Thursday, Tuesday s exam. Permutation generation

On the relation between radiated and conducted RF emission tests

Published in: Proceedings of 8th Annual IEEE Energy Conversion Congress & Exposition (ECCE 2016)

The deterministic EPQ with partial backordering: A new approach

Examination paper for TDT4120 Algorithms and Data Structures

Divide & conquer. Which works better for multi-cores: insertion sort or merge sort? Why?

MITOCW 7. Counting Sort, Radix Sort, Lower Bounds for Sorting

Effects of Harmonic Pollution on Three-Phase Electrical Motors

ENGINEERING RECOMMENDATION G5/5

Sebrina Sebright and Chicks Pillow

UNIVERSITY of PENNSYLVANIA CIS 391/521: Fundamentals of AI Midterm 1, Spring 2010

Damping Algorithm based on Phasor Estimation

Machine Vision System for Automatic Weeding Strategy in Oil Palm Plantation using Image Filtering Technique

Introduction to. Algorithms. Lecture 10. Prof. Piotr Indyk

FINAL REPORT FOR NCHRP 20-7 (364)

LOADING OF TRANSFORMERS BEYOND NAMEPLATE RATING

Aalborg Universitet. Published in: IET Power Electronics. DOI (link to publication from Publisher): /iet-pel Publication date: 2018

Lecture 14. Questions? Friday, February 10 CS 430 Artificial Intelligence - Lecture 14 1

Two Humanoid Simulators: Comparison and Synthesis

Adversary Search. Ref: Chapter 5

Universiteit Leiden Opleiding Informatica

3D Hole Inspection Using Lens with High Field Curvature

Splay tree concept Splaying operation: double-rotations Splay insertion Splay search Splay deletion Running time of splay tree operations

sketch a simplified small-signal equivalent circuit of a differential amplifier

Transcription:

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?