Welcome. Balanced search trees. Balanced Search Trees. Inge Li Gørtz

Similar documents
22: Union Find. CS 473u - Algorithms - Spring April 14, We want to maintain a collection of sets, under the operations of:

Inge Li Gørtz. Thank you to Kevin Wayne for inspiration to slides

CSE 332. Sorting. Data Abstractions. CSE 332: Data Abstractions. QuickSort Cutoff 1. Where We Are 2. Bounding The MAXIMUM Problem 4

Algorithms & Data Structures Homework 8 HS 18 Exercise Class (Room & TA): Submitted by: Peer Feedback by: Points:

CS 360 Exam 2 Fall 2014 Name

CS 2204 DIGITAL LOGIC & STATE MACHINE DESIGN SPRING 2014

CISC 320 Introduction to Algorithms Spring 2014

Solutions for HW9. Bipartite: put the red vertices in V 1 and the black in V 2. Not bipartite!

Spacetime and the Quantum World Questions Fall 2010

Algorithm Design and Analysis

Connected-components. Summary of lecture 9. Algorithms and Data Structures Disjoint sets. Example: connected components in graphs

Lecture 3. XML Into RDBMS. XML and Databases. Memory Representations. Memory Representations. Traversals and Pre/Post-Encoding. Memory Representations

Counting Paths Between Vertices. Isomorphism of Graphs. Isomorphism of Graphs. Isomorphism of Graphs. Isomorphism of Graphs. Isomorphism of Graphs

XML and Databases. Exam Preperation Discuss Answers to last year s exam. Sebastian Maneth NICTA and UNSW

CS241 Week 6 Tutorial Solutions

Surds and Indices. Surds and Indices. Curriculum Ready ACMNA: 233,

6. Suppose lim = constant> 0. Which of the following does not hold?

CS 491G Combinatorial Optimization Lecture Notes

AVL Trees. D Oisín Kidney. August 2, 2018

Lecture 6: Coding theory

Balanced binary search trees

Project 6: Minigoals Towards Simplifying and Rewriting Expressions

Algorithm Design and Analysis

Arrow s Impossibility Theorem

18.06 Problem Set 4 Due Wednesday, Oct. 11, 2006 at 4:00 p.m. in 2-106

Factorising FACTORISING.

Lecture 11 Binary Decision Diagrams (BDDs)

Comparing the Pre-image and Image of a Dilation

CARLETON UNIVERSITY. 1.0 Problems and Most Solutions, Sect B, 2005

Intermediate Math Circles Wednesday 17 October 2012 Geometry II: Side Lengths

Equivalent fractions have the same value but they have different denominators. This means they have been divided into a different number of parts.

10. AREAS BETWEEN CURVES

These slides are from 2014 and contain a semi-serious error at one point in the

Connectivity in Graphs. CS311H: Discrete Mathematics. Graph Theory II. Example. Paths. Connectedness. Example

CS 275 Automata and Formal Language Theory

CS 573 Automata Theory and Formal Languages

Probability. b a b. a b 32.

Section 6: Area, Volume, and Average Value

Logic, Set Theory and Computability [M. Coppenbarger]

Parse trees, ambiguity, and Chomsky normal form

, g. Exercise 1. Generator polynomials of a convolutional code, given in binary form, are g. Solution 1.

Analysis of Temporal Interactions with Link Streams and Stream Graphs

1 Nondeterministic Finite Automata

Arrow s Impossibility Theorem

1 PYTHAGORAS THEOREM 1. Given a right angled triangle, the square of the hypotenuse is equal to the sum of the squares of the other two sides.

Data Structures (INE2011)

Chapter 4 State-Space Planning

Graph Theory. Simple Graph G = (V, E). V={a,b,c,d,e,f,g,h,k} E={(a,b),(a,g),( a,h),(a,k),(b,c),(b,k),...,(h,k)}

Global alignment. Genome Rearrangements Finding preserved genes. Lecture 18

Outline Data Structures and Algorithms. Data compression. Data compression. Lossy vs. Lossless. Data Compression

CS261: A Second Course in Algorithms Lecture #5: Minimum-Cost Bipartite Matching

CSC2542 State-Space Planning

Lecture 2: January 27

Implication Graphs and Logic Testing

Necessary and sucient conditions for some two. Abstract. Further we show that the necessary conditions for the existence of an OD(44 s 1 s 2 )

I. Theory of Automata II. Theory of Formal Languages III. Theory of Turing Machines

8 THREE PHASE A.C. CIRCUITS

2.4 Theoretical Foundations

GEOMETRY OF THE CIRCLE TANGENTS & SECANTS

Area and Perimeter. Area and Perimeter. Solutions. Curriculum Ready.

2.4 Linear Inequalities and Interval Notation

I1 = I2 I1 = I2 + I3 I1 + I2 = I3 + I4 I 3

EECS 141 Due 04/19/02, 5pm, in 558 Cory

Lecture 2: Cayley Graphs

NON-DETERMINISTIC FSA

EE 108A Lecture 2 (c) W. J. Dally and P. Levis 2

Section 1.3 Triangles

Mid-Term Examination - Spring 2014 Mathematical Programming with Applications to Economics Total Score: 45; Time: 3 hours

Intermediate Math Circles Wednesday, November 14, 2018 Finite Automata II. Nickolas Rollick a b b. a b 4

Review of Gaussian Quadrature method

Laboratory for Foundations of Computer Science. An Unfolding Approach. University of Edinburgh. Model Checking. Javier Esparza

Homework 3 Solutions

Tries and suffixes trees

Pythagoras Theorem PYTHAGORAS THEOREM.

CS 188: Artificial Intelligence Spring 2007

Algebra: Function Tables - One Step

4. GREEDY ALGORITHMS I

CIT 596 Theory of Computation 1. Graphs and Digraphs

Aperiodic tilings and substitutions

Common intervals of genomes. Mathieu Raffinot CNRS LIAFA

Computing the Quartet Distance between Evolutionary Trees in Time O(n log n)

Graph width-parameters and algorithms

Algebra 2 Semester 1 Practice Final

Data Structures LECTURE 10. Huffman coding. Example. Coding: problem definition

Special Numbers, Factors and Multiples

Data Compression Techniques (Spring 2012) Model Solutions for Exercise 4

Section 6.1 Definite Integral

Suppose we want to find the area under the parabola and above the x axis, between the lines x = 2 and x = -2.

Lesson 2.1 Inductive Reasoning

Consolidation Worksheet

Lesson 2.1 Inductive Reasoning

CMSC 330: Organization of Programming Languages

Tutorial Worksheet. 1. Find all solutions to the linear system by following the given steps. x + 2y + 3z = 2 2x + 3y + z = 4.

Non Right Angled Triangles

Geometry of the Circle - Chords and Angles. Geometry of the Circle. Chord and Angles. Curriculum Ready ACMMG: 272.

Fast index for approximate string matching

Types of Finite Automata. CMSC 330: Organization of Programming Languages. Comparing DFAs and NFAs. NFA for (a b)*abb.

SECTION A STUDENT MATERIAL. Part 1. What and Why.?

GM1 Consolidation Worksheet

Math 61CM - Solutions to homework 9

Transcription:

Welome nge Li Gørt. everse tehing n isussion of exerises: 02110 nge Li Gørt 3 tehing ssistnts 8.00-9.15 Group work 9.15-9.45 isussions of your solutions in lss 10.00-11.15 Leture 11.15-11.45 Work on exerises in the new mteril 11.45-12.00 oun up Weekly ssignments (You hve to get 40 points + pss 2 implementtion exerises in orer to e le to tten the written exm). Prerequisites: 02105/02326 lgorithms n t trutures Thnk you to Kevin Wyne for inspirtion to slies 2 lne serh trees lne erh Trees 2-3-4 trees re-lk trees ynmi sets erh nsert elete Mximum Minimum uessor(x) (fin minimum element x) Preeessor(x) (fin mximum element x) eferenes: L hpter 13, lgorithms in Jv (hnout) This leture: 2-3-4 trees, re-lk trees ext time: ply trees 4

ynmi set implementtions Worst se running times mplementtion serh insert elete minimum mximum suessor preeessor linke lists O(n) O(1) O(1) O(n) O(n) O(n) O(n) orere rry O(log n) O(n) O(n) O(1) O(1) O(log n) O(log n) T O(h) O(h) O(h) O(h) O(h) O(h) O(h) 2-3-4 trees n worst se h=n. n est se h= log n (fully lne inry tree) Toy: ow to keep the trees lne. 5 2-3-4 trees erhing in 2-3-4 tree 2-3-4 trees. llow noes to hve multiple keys. erh. Perfet lne. very pth from root to lef hs sme length. llow 1, 2, or 3 keys per noe 2-noe: one key, 2 hilren 3-noe: 2 keys, 3 hilren 4-noe: 3 keys, 4 hilren ompre serh key ginst keys in noe. in intervl ontining serh key ollow ssoite link (reursively) x. erh for L smller thn K lrger thn etween K n M O X etween K n M O X smller thn M G J L Q V Y Z G J L Q V Y Z foun L 7 8

nsertion in 2-3-4 tree nsertion in 2-3-4 tree nsert. erh to ottom for key. smller thn K M O X x. nsert smller thn M O X G J L Q V Y Z G J L Q V Y Z 9 not foun 10 nsertion in 2-3-4 tree nsert. erh to ottom for key. 2-noe t ottom: onvert to 3-noe nsertion in 2-3-4 tree nsert. erh to ottom for key. 2-noe t ottom: onvert to 3-noe smller thn K lrger thn x. nsert X x. nsert smller thn M O X M O U lrger thn U G J L Q V Y Z G J L Q T Y Z fits here 11 X not foun12

nsertion in 2-3-4 tree nsertion in 2-3-4 tree nsert. nsert. erh to ottom for key. erh to ottom for key. 2-noe t ottom: onvert to 3-noe 3-noe t ottom: onvert to 4-noe 2-noe t ottom: onvert to 3-noe 3-noe t ottom: onvert to 4-noe x. nsert X lrger thn smller thn K M O U lrger thn W x. nsert M O X lrger thn G J L Q T X Y Z G J L Q V Y Z X fits here 13 not foun 14 nsertion in 2-3-4 tree plitting 4-noe in 2-3-4 tree nsert. e: split the 4-noe to mke room G erh to ottom for key. 2-noe t ottom: onvert to 3-noe J 3-noe t ottom: onvert to 4-noe 4-noe t ottom:?? G J oes fit here! smller thn K oes not fit here x. nsert G M O X lrger thn Prolem: oesn t work if prent is 4-noe olution 1: plit the prent (n ontinue splitting J G J L Q V Y Z while neessry). olution 2: plit 4-noes on the wy own. oes not fit here! 15 16

plitting 4-noes in 2-3-4 tree nsertion in 2-3-4 tree e: split 4-noes on the wy own the tree. nsures lst noe is not 4-noe. Trnsformtions to split 4-noes: G J x y v G J x y v nsert. erh to ottom for key. 2-noe t ottom: onvert to 3-noe 3-noe t ottom: onvert to 4-noe not 4-noe G 4-noe t ottom:?? nvrint. urrent noe is not 4-noe. onsequene. nsertion t ottom is esy sine it's not 4-noe. G J x y v root G x y v x y J v G x. nsert not 4-noe M O X 4-noe G J L Q V Y Z x y v 17 18 nsertion in 2-3-4 tree nsertion in 2-3-4 tree nsert. nsert. erh to ottom for key. 2-noe t ottom: onvert to 3-noe erh to ottom for key. 2-noe t ottom: onvert to 3-noe 3-noe t ottom: onvert to 4-noe 3-noe t ottom: onvert to 4-noe 4-noe t ottom:?? 4-noe t ottom:?? x. nsert G M O X x. nsert G M O X J L Q V Y Z J L Q V Y Z 19 20

plitting 4-noes in 2-3-4 tree plitting 4-noes in 2-3-4 tree Lol trnsformtions tht work nywhere in the tree. Lol trnsformtions tht work nywhere in the tree x. plitting 4-noe tthe to 2-noe x. plitting 4-noe tthe to 3-noe Q Q - K Q W - K W - -G K Q W K W - -G -J L-P -V X-Z -J L-P -V X-Z -J L-P -V X-Z -J L-P -V X-Z oul e huge unhnge oul e huge unhnge 21 22 plitting 4-noes in 2-3-4 tree nsertion 2-3-4 trees Lol trnsformtions tht work nywhere in the tree. nsert U nsert G plitting 4-noe tthe to 4-noe never hppens when we split noes on the wy own the tree. U U nvrint. urrent noe is not 4-noe. nsert U nsert G plit nsert G nsert T plit G U G U G T U nsert T 23 24

elete minimum: elete minimum: minimum lwys in leftmost lef minimum lwys in leftmost lef f 3- or 4-noe: elete key f 3- or 4-noe: elete key x. elete minimum x. elete minimum M O X M O X is minimum G J L Q V Y Z elete G J L Q V Y Z 25 26 elete minimum: minimum lwys in leftmost lef e: On the wy own mintin the invrint tht urrent noe is not 2-noe. hil of root n root is 2-noe: f 3- or 4-noe: elete key 2-noe?? x. elete minimum x y on the wy own: w x y w or x y w r s x y w r s M O X G G or elete? G J L Q V Y Z x y w r s x y w r s 27 x y w x y w 28

elete minimum: elete minimum: minimum lwys in leftmost lef minimum lwys in leftmost lef f 3- or 4-noe: elete key f 3- or 4-noe: elete key 2-noe: split/merge on wy own. 2-noe: split/merge on wy own. x. elete minimum x. elete minimum not 2-noe M O X M O X 2-noe G J L Q V Y Z G J L Q V Y Z 29 30 elete minimum: minimum lwys in leftmost lef f 3- or 4-noe: elete key 2-noe: split/merge on wy own. x. elete minimum M O X M O X G J L Q V Y Z 31 G J L Q V Y Z 32

uring serh mintin invrint tht urrent noe is not 2-noe uring serh mintin invrint tht urrent noe is not 2-noe f key is in lef: elete key M O X M O X G J L Q V Y Z 33 G J L Q V Y Z 34 uring serh mintin invrint tht urrent noe is not 2-noe f key is in lef: elete key Key not in lef: reple with suessor (lwys lef in sutree) n elete suessor from lef. uring serh mintin invrint tht urrent noe is not 2-noe f key is in lef: elete key Key not in lef: reple with suessor (lwys lef in sutree) n elete suessor from lef. x. elete K M O X M O X G J L Q V Y Z 35 G J L Q V Y Z 36

uring serh mintin invrint tht urrent noe is not 2-noe uring serh mintin invrint tht urrent noe is not 2-noe f key is in lef: elete key f key is in lef: elete key Key not in lef: reple with suessor (lwys lef in sutree) n elete suessor from lef. x. elete K Key not in lef: reple with suessor (lwys lef in sutree) n elete suessor from lef. x. elete K in suessor in suessor M O X M O X G J L Q V Y Z G J L Q V Y Z 37 38 uring serh mintin invrint tht urrent noe is not 2-noe uring serh mintin invrint tht urrent noe is not 2-noe f key is in lef: elete key f key is in lef: elete key Key not in lef: reple with suessor (lwys lef in sutree) n elete suessor from lef. x. elete K Key not in lef: reple with suessor (lwys lef in sutree) n elete suessor from lef. x. elete K in suessor not 2-noe in suessor not 2-noe M O X M O X 2-noe G J L Q V Y Z G J L Q V Y Z 39 40

uring serh mintin invrint tht urrent noe is not 2-noe f key is in lef: elete key Key not in lef: reple with suessor (lwys lef in sutree) n elete suessor from lef. x. elete K in suessor uring serh mintin invrint tht urrent noe is not 2-noe f key is in lef: elete key Key not in lef: reple with suessor (lwys lef in sutree) n elete suessor from lef. x. elete K in suessor O X elete L from lef O X G J L M Q V Y Z G J L M Q V Y Z 41 42 uring serh mintin invrint tht urrent noe is not 2-noe uring serh mintin invrint tht urrent noe is not 2-noe f key is in lef: elete key f key is in lef: elete key Key not in lef: reple with suessor (lwys lef in sutree) n elete suessor from lef. x. elete K Key not in lef: reple with suessor (lwys lef in sutree) n elete suessor from lef. x. elete K in suessor in suessor elete L from lef O X elete L from lef O X eple K with L G J M Q V Y Z G J M Q V Y Z 43 44

2-3-4 Tree: lne Property. ll pths from root to lef hve sme length. uring serh mintin invrint tht urrent noe is not 2-noe f key is in lef: elete key Key not in lef: reple with suessor (lwys lef in sutree) n elete suessor from lef. x. elete K in suessor elete L from lef eple K with L L O X G J M Q V Y Z 45 Tree height. Worst se: lg [ll 2-noes] est se: log 4 = 1/2 lg [ll 4-noes] etween 10 n 20 for million noes. etween 15 n 30 for illion noes. 46 ynmi set implementtions Worst se running times mplementtion serh insert elete minimum mximum suessor preeessor linke lists O(n) O(1) O(1) O(n) O(n) O(n) O(n) orere rry O(log n) O(n) O(n) O(1) O(1) O(log n) O(log n) T O(h) O(h) O(h) O(h) O(h) O(h) O(h) e-lk trees 2-3-4 tree O(log n) O(log n) O(log n) O(log n) O(log n) O(log n) O(log n) 47

e-lk tree (Guis-egewik, 1979) e-lk tree (Guis-egewik, 1979) epresent 2-3-4 tree s inry serh tree Use olors on noes to represent 3- n 4-noes. epresent 2-3-4 tree s inry serh tree Use olors on noes to represent 3- n 4-noes. G K O G K O or onnetion etween 2-3-4 trees n re-lk trees: L L or L 49 50 e-lk tree (Guis-egewik, 1979) e-lk tree epresent 2-3-4 tree s inry serh tree Use olors on noes to represent 3- n 4-noes. onnetion etween 2-3-4 trees n re-lk trees: or Properties of re-lk trees: The root is lwys lk ll root-to-lef pths hve the sme numer of lk noes. e noes o not hve re hilren L: ll leves (L) re lk 51 52

e-lk tree onnetion etween 2-3-4 trees n re-lk trees: e-lk tree onnetion etween 2-3-4 trees n re-lk trees: 53 54 e-lk tree onnetion etween 2-3-4 trees n re-lk trees: nsertion in re-lk trees nsertion in 2-noe: nsert or nsertion in 3-noe: nsert 55 56

e-lk tree: plitting 4-noes nsertion in re-lk trees Two esy ses: M M M M nsertion in 3-noe (ontinue): nsert M M M M nsert nsert???? 57 58 e-lk trees: plitting of 4-noes xmple of hr se: ottions in re-lk trees Two types of rottions L K L K K?? olution: ottions! 60

ottions in re-lk trees Two types of rottions: nsertion in re-lk trees nsertion in 3-noe: nsert?? nsert?? 61 62 nsertion in re-lk trees nsertion in 3-noe: nsertion in re-lk trees nsertion in 3-noe: nsert nsert nsert?? nsert 63 64

nsert x: erh to ottom fter key (x) nsert re lef lne: 3 ses (+ symmetri) nsertion in re-lk tree 65 Keep lning with 66 ksempel U nsert U nsert V U V V U otte U 67 xmple U V nsert G U V G U V G U V G U V G otte otte unning times in re-lk trees Time for insertion: erh to ottom fter key: nsert re lef: Perform reoloring n rottions on wy up: n reolor mny times (ut t most h) t most 2 rottions. Totl O(h). Time for serh me s T: O(h) eight: O(log n) 68 O(h) O(1) O(h)

ynmi set implementtions lne trees: implementtions Worst se running times mplementtion serh insert elete minimum mximum suessor preeessor linke lists O(n) O(1) O(1) O(n) O(n) O(n) O(n) orere rry O(log n) O(n) O(n) O(1) O(1) O(log n) O(log n) T O(h) O(h) O(h) O(h) O(h) O(h) O(h) elk trees: Jv: jv.util.treemp, jv.util.treeet. ++ TL: mp, multimp, multiset. Linux kernel: linux/rtree.h. 2-3-4 tree O(log n) O(log n) O(log n) O(log n) O(log n) O(log n) O(log n) re-lk tree O(log n) O(log n) O(log n) O(log n) O(log n) O(log n) O(log n) 69 70 xerise time trt working on the exerises on weekpln T. 11.45 tlk out solutions to the first 2-3 exerises + info out the mntory exerises. Mntory exerise To e hne in efore 8PM the following uny on TU Lern. This week: inry tries from weekpln wrmup. 71 72

Gring of mntory ssignments You n get up to 20 points for n exerise: 5 points for the exmple (rw tree/run lgorithm et) 15 points for esign n lgorithm exerise: 4 points for the esription of your lgorithm 4 points for the qulity of your lgorithm 4 points for the omplexity nlysis 3 points for the orretness rguments 73