cs 341 f19 lecture 4 cs 466 fall 2019 lecture 4 - splay trees …cs466/lectures/lecture4.pdf · cs...

10
CS 341 F19 Lecture 4 - 37 - 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 CS 466 Fall 2019 Lecture 4 - Splay Trees Anna Lubiw Univ. Waterloo

Upload: others

Post on 01-Aug-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 341 F19 Lecture 4 CS 466 Fall 2019 Lecture 4 - Splay Trees …cs466/Lectures/Lecture4.pdf · CS 341 F19 Lecture 4 - 37 - 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

CS 341 F19 Lecture 4

- 37 -

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

CS 466 Fall 2019 Lecture 4 - Splay Trees Anna LubiwUniv. Waterloo

Page 2: CS 341 F19 Lecture 4 CS 466 Fall 2019 Lecture 4 - Splay Trees …cs466/Lectures/Lecture4.pdf · CS 341 F19 Lecture 4 - 37 - 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

CS 341 F19 Lecture 4

- 38 -

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

CS 466 Fall 2019 Lecture 4 - Splay Trees Anna LubiwUniv. Waterloo

Page 3: CS 341 F19 Lecture 4 CS 466 Fall 2019 Lecture 4 - Splay Trees …cs466/Lectures/Lecture4.pdf · CS 341 F19 Lecture 4 - 37 - 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

CS 341 F19 Lecture 4

- 39 -

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

CS 466 Fall 2019 Lecture 4 - Splay Trees Anna LubiwUniv. Waterloo

Page 4: CS 341 F19 Lecture 4 CS 466 Fall 2019 Lecture 4 - Splay Trees …cs466/Lectures/Lecture4.pdf · CS 341 F19 Lecture 4 - 37 - 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

CS 341 F19 Lecture 4

- 40 -

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

Algorithms Lecture ��: Scapegoat and Splay Trees [Fa’��]

zw

x

x

z

w x

w

z

Figure 2. A zig-zag at x . The symmetric case is not shown.

x

y

z

x

y

z z

y

x

Figure 3. A right roller-coaster at x and a left roller-coaster at z.

a

b

c

d

f

e

g

h

i

j

k

l

m

n

x

g

a

b

c

d

f

e

k

l

m

n

x

j

h

i

a

b

c

d

m

nx

k

l

f

e

g

j

h

i

a

b

c

d m

n

x

k

l

f

e

g

j

h

i

k

l

a

b m

n

x

c f

e

g

j

h

i

d

Figure 4. Splaying a node. Irrelevant subtrees are omitted for clarity.

• Search: Find the node containing the key using the usual algorithm, or its predecessor orsuccessor if the key is not present. Splay whichever node was found.

• Insert: Insert a new node using the usual algorithm, then splay that node.

• Delete: Find the node x to be deleted, splay it, and then delete it. This splits the tree intotwo subtrees, one with keys less than x , the other with keys bigger than x . Find the nodew in the left subtree with the largest key (the inorder predecessor of x in the original tree),splay it, and finally join it to the right subtree.

x

x

w

ww

Figure 5. Deleting a node in a splay tree.

Each search, insertion, or deletion consists of a constant number of operations of the formwalk down to a node, and then splay it up to the root. Since the walk down is clearly cheaper

CS 466 Fall 2019 Lecture 4 - Splay Trees Anna LubiwUniv. Waterloo

Page 5: CS 341 F19 Lecture 4 CS 466 Fall 2019 Lecture 4 - Splay Trees …cs466/Lectures/Lecture4.pdf · CS 341 F19 Lecture 4 - 37 - 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

CS 341 F19 Lecture 4

- 41 -

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

CS 466 Fall 2019 Lecture 4 - Splay Trees Anna LubiwUniv. Waterloo

Page 6: CS 341 F19 Lecture 4 CS 466 Fall 2019 Lecture 4 - Splay Trees …cs466/Lectures/Lecture4.pdf · CS 341 F19 Lecture 4 - 37 - 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

CS 341 F19 Lecture 4

- 42 -

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

CS 466 Fall 2019 Lecture 4 - Splay Trees Anna LubiwUniv. Waterloo

Page 7: CS 341 F19 Lecture 4 CS 466 Fall 2019 Lecture 4 - Splay Trees …cs466/Lectures/Lecture4.pdf · CS 341 F19 Lecture 4 - 37 - 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

CS 341 F19 Lecture 4

- 43 -

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

CS 466 Fall 2019 Lecture 4 - Splay Trees Anna LubiwUniv. Waterloo

Page 8: CS 341 F19 Lecture 4 CS 466 Fall 2019 Lecture 4 - Splay Trees …cs466/Lectures/Lecture4.pdf · CS 341 F19 Lecture 4 - 37 - 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

CS 341 F19 Lecture 4

- 44 -

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

CS 466 Fall 2019 Lecture 4 - Splay Trees Anna LubiwUniv. Waterloo

Page 9: CS 341 F19 Lecture 4 CS 466 Fall 2019 Lecture 4 - Splay Trees …cs466/Lectures/Lecture4.pdf · CS 341 F19 Lecture 4 - 37 - 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

CS 341 F19 Lecture 4

- 45 -

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

CS 466 Fall 2019 Lecture 4 - Splay Trees Anna LubiwUniv. Waterloo

Page 10: CS 341 F19 Lecture 4 CS 466 Fall 2019 Lecture 4 - Splay Trees …cs466/Lectures/Lecture4.pdf · CS 341 F19 Lecture 4 - 37 - 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

CS 341 F19 Lecture 4

- 46 -

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

CS 466 Fall 2019 Lecture 4 - Splay Trees Anna LubiwUniv. Waterloo