lecture 1 - ida.liu.setddc32/lectures/tddc32-01.pdf2 binary search trees binary search trees are not...

14
Lecture 1 Course administration Search trees: AVL-trees TDDC32 Lecture notes in Design and implementation of a software module in Java 14 January 2013 Tommy Färnqvist, IDA, Linköping University 1.1 Lecture Topics Contents 1 Course administration 1 2 Binary Search Trees 2 3 AVL Trees 3 1.2 1 Course administration Teachers and Staff Course leader, examiner, lectures: Tommy Färnqvist [email protected] Course assistant, labs, project: Daniel Persson [email protected] Assistant, labs, project: Rebecka Geijer Michaeli [email protected] Course administrator: Annelie Almquist [email protected] 1.3 Examination Written exam (U, 3, 4, 5), 1.5 ECTS credits Lab assignments (Fail, Pass), 1.5 ECTS credits Project (U, 3, 4, 5), 3 ECTS credits Documentation (25-33%) Programming (66-75%) * Amount and quality of code, coding conventions, comments, exception handling Final grade: 33% written exam + 66% project work 1.4 Course Aim According to LiTH Study Guide After completing the course the students shall have good skills in coding software modules in Java using a development environment. The modules shall at least be 300 lines of well- structured code. The students shall have acquaintance with object-oriented analysis and design plus unit testing of their own code. Deepened studies in data structures and algorithms shall prepare students for specialisation courses. 1.5 Lectures Course administration, aims. AVL-trees. Multi way search trees, Splay trees ADT Map, Hash Tables, ADT Dictionary. Skip Lists. Threads. Networking. Java GUI programming. JDBC connections. Three lectures on OOAD, UML, and introduction to project management. 1.6 1

Upload: others

Post on 11-Jul-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 1 - ida.liu.seTDDC32/lectures/tddc32-01.pdf2 Binary Search Trees Binary Search Trees are not Unique The same data can yield different binary search trees insert: 1,2,4,5,8

Lecture 1Course administration Search trees:AVL-treesTDDC32

Lecture notes in Design and implementation of a software module in Java 14 January 2013

Tommy Färnqvist, IDA, Linköping University

1.1

Lecture Topics

Contents

1 Course administration 1

2 Binary Search Trees 2

3 AVL Trees 3 1.2

1 Course administration

Teachers and Staff

• Course leader, examiner, lectures: Tommy Färnqvist [email protected]• Course assistant, labs, project: Daniel Persson [email protected]• Assistant, labs, project: Rebecka Geijer Michaeli [email protected]• Course administrator: Annelie Almquist [email protected]

1.3

Examination

• Written exam (U, 3, 4, 5), 1.5 ECTS credits• Lab assignments (Fail, Pass), 1.5 ECTS credits• Project (U, 3, 4, 5), 3 ECTS credits

– Documentation (25-33%)

– Programming (66-75%)

∗ Amount and quality of code, coding conventions, comments, exception handling

Final grade: 33% written exam + 66% project work1.4

Course Aim According to LiTH Study Guide

After completing the course the students shall have good skills in coding software modulesin Java using a development environment. The modules shall at least be 300 lines of well-structured code. The students shall have acquaintance with object-oriented analysis and designplus unit testing of their own code. Deepened studies in data structures and algorithms shallprepare students for specialisation courses.

1.5

Lectures

• Course administration, aims. AVL-trees.• Multi way search trees, Splay trees• ADT Map, Hash Tables, ADT Dictionary. Skip Lists.• Threads. Networking.• Java GUI programming. JDBC connections.• Three lectures on OOAD, UML, and introduction to project management.

1.6

1

Page 2: Lecture 1 - ida.liu.seTDDC32/lectures/tddc32-01.pdf2 Binary Search Trees Binary Search Trees are not Unique The same data can yield different binary search trees insert: 1,2,4,5,8

Lab assignments

• Lab 1: AVL Tree, Hash Tables• Lab 2: Advanced Java programming• (Optional) Lab 3: UML

• Work in pairs• Please register in WebReg Friday 18 January at the latest!• Group A: SU02, SU03• Group B: SU04, SU06• Note: Only 2/3 of the labs are staffed

Deadline for labs is Wednesday 13 February.1.7

Project

• Work in groups of three• Please hand in project idea and register in WebReg Thursday 14 February at the latest!• Note: Each project member is expected to contribute 80-100 hours to the project

– Weekly report to assistant (outside exam period)

• Deadline for Requirements Specification is Friday 22 February• Deadline for Analysis and Design Document is Tuesday 5 March• Deadline for implementation and demonstration is Tuesday 7 May

1.8

2 Binary Search Trees

Binary Search Trees are not UniqueThe same data can yield different binary search trees

insert: 1,2,4,5,8

insert: 5,2,1,4,8 1.9

Successful look-up

BST in worst case

• BST degenerated into linear sequence• expected number of comparisons is (n+1)/2

Balanced BST

• height is O(log2 n)• O(log2 n) comparisons

1.10

2

Page 3: Lecture 1 - ida.liu.seTDDC32/lectures/tddc32-01.pdf2 Binary Search Trees Binary Search Trees are not Unique The same data can yield different binary search trees insert: 1,2,4,5,8

3 AVL Trees

AVL Tree

• Self balancing BST/height balanced BST• AVL = Adelson-Velskii och Landis, 1962• The idea: Keep updated balance information in each node• AVL propertyFor each internal node v in T , the heights of the children of v differ by at most 1 . . . or

alternatively. . . For each internal node v in T , the following holds:

b(v) ∈ {−1,0,1}, where

b(v) = height(leftChild(v))−height(rightChild(v))1.11

Maximum Height of AVL Tree

Proposition 1. The height of an AVL tree storing n elements is O(logn).

Which has the consequence that. . .

Proposition 2. We can do find, insert, and remove in an AVL tree in time O(logn) while preserving theAVL property.

1.12

Example: an AVL tree

44

17

32

78

50

48 62

88

3

1

0

2

1

0 0

0

1.13

Insertion in AVL trees

• The new node changes the tree height and the tree has to be re-balanced.

– Information about the height of sub trees can be represented in different ways:

∗ Store the height explicitly in each node∗ Store the balance factor in each node

• The change is usually described as a left or right rotation of a sub tree.• One rotation is sufficient to re-balance the tree.

1.14

Insertion in AVL trees (simple cases)

3

Page 4: Lecture 1 - ida.liu.seTDDC32/lectures/tddc32-01.pdf2 Binary Search Trees Binary Search Trees are not Unique The same data can yield different binary search trees insert: 1,2,4,5,8

1.15

Insertion algorithm

• Start from the new node and search upwards until a node x is found, such that its grandparent z isunbalanced. Mark the parent of x with y. Reconstruct the tree as follows:

– Rename x,y,z to a,b,c based on their inorder order.

– Let T0,T1,T2,T3 be an enumeration in inorder of the sub trees of x, y och z. (None of the subtrees can have x, y, or z as root.)

– Exchange z for b, its children are now a and c.

– T0 and T1 are children of a, and T2 and T3 are children of c.1.16

Example: insertion in an AVL tree

44

17 78

50

48 62

88

4

1

0

3

2

0 1

0

54

32

T

T

0

1

3

y/a

T

z/c

x/b

1.17

Example: insertion in an AVL tree

4

Page 5: Lecture 1 - ida.liu.seTDDC32/lectures/tddc32-01.pdf2 Binary Search Trees Binary Search Trees are not Unique The same data can yield different binary search trees insert: 1,2,4,5,8

44

17

50

48

3

1

0

2

1

0 0

132

T0 T1 T2 T3

bca

054 88

78

62

1.18

Four different rotations

T0T1

T2

T3 T0 T1 T2

T3

a=z

c=xa=z

b=y

c=xenkel rotationb=y

If b = y we call it a single rotation.”Rotate y up above z” 1.19

Four different rotations

T0

T1

T2

T3

T0

T1 T2 T3

enkel rotationc=z

a=xb=y

c=z

b=y

a=x

If b = y we call it a single rotation.”Rotate y up above z” 1.20

Four different rotations

T0

T0

T1

T2 T3 T1

T2

T3

a=za=z

b=x

c=ydubbel rotation

b=xc=y

If b = x we call it a double rotation.”Rotate x up above y and then above z” 1.21

5

Page 6: Lecture 1 - ida.liu.seTDDC32/lectures/tddc32-01.pdf2 Binary Search Trees Binary Search Trees are not Unique The same data can yield different binary search trees insert: 1,2,4,5,8

Four different rotations

T0 T3T0 T1

T2

T3 T1

T2

a=y

b=x

c=zdubbel rotationc=z

b=xa=y

If b = x we call it a double.”Rotate x up above y and then above z” 1.22

Another way to describe it

0T

x

y

T1

2T

Assume that we have balance. . . 1.23

Another way to describe it

x

y

T1

2T

T0

. . . and then insert something which destroys it 1.24

Another way to describe it

6

Page 7: Lecture 1 - ida.liu.seTDDC32/lectures/tddc32-01.pdf2 Binary Search Trees Binary Search Trees are not Unique The same data can yield different binary search trees insert: 1,2,4,5,8

x

y

T1

2T

T0

Do a single rotation 1.25

Another way to describe it

x

y

T1

2T

T0

Do a single rotation 1.26

Another way to describe it

x

y

T1

2T

T0

7

Page 8: Lecture 1 - ida.liu.seTDDC32/lectures/tddc32-01.pdf2 Binary Search Trees Binary Search Trees are not Unique The same data can yield different binary search trees insert: 1,2,4,5,8

Do a single rotation 1.27

Another way to describe it

0T

yx

T1

T0

T2

Do a single rotation 1.28

Another way to describe it

0T

y

x

T1

T20

T

Do a single rotation 1.29

Another way to describe it

8

Page 9: Lecture 1 - ida.liu.seTDDC32/lectures/tddc32-01.pdf2 Binary Search Trees Binary Search Trees are not Unique The same data can yield different binary search trees insert: 1,2,4,5,8

0T

y

x

T1

T20

T

Done! 1.30

Another way to describe it

y

z

Another example. . . 1.31

Another way to describe it

z

y

9

Page 10: Lecture 1 - ida.liu.seTDDC32/lectures/tddc32-01.pdf2 Binary Search Trees Binary Search Trees are not Unique The same data can yield different binary search trees insert: 1,2,4,5,8

. . . this time the insertion is in another position 1.32

Another way to describe it

z

y

Try a single rotation again. . . 1.33

Another way to describe it

y

z

. . . hmm, we did not get balance 1.34

Another way to describe it

10

Page 11: Lecture 1 - ida.liu.seTDDC32/lectures/tddc32-01.pdf2 Binary Search Trees Binary Search Trees are not Unique The same data can yield different binary search trees insert: 1,2,4,5,8

z

y

Start over. . . and look at the structure of y 1.35

Another way to describe it

z

y

x

0

1T

2T

3T

T

We have to perform a double rotation 1.36

Another way to describe it

z

y

x

0

1T

2T

3T

T

11

Page 12: Lecture 1 - ida.liu.seTDDC32/lectures/tddc32-01.pdf2 Binary Search Trees Binary Search Trees are not Unique The same data can yield different binary search trees insert: 1,2,4,5,8

We have to perform a double rotation 1.37

Another way to describe it

z

y

x

0

1T

2T

3T

T

We have to perform a double rotation 1.38

Another way to describe it

z

y

x

0

1T

2T

3T

T

We have to perform a double rotation 1.39

Another way to describe it

12

Page 13: Lecture 1 - ida.liu.seTDDC32/lectures/tddc32-01.pdf2 Binary Search Trees Binary Search Trees are not Unique The same data can yield different binary search trees insert: 1,2,4,5,8

z

y

x

0

1T

2T

3T

T

We have to perform a double rotation 1.40

Another way to describe it

x

z

2TT

3TT

0

y

1

We have to perform a double rotation 1.41

Another way to describe it

z

2T

0 3T

x

y

T1

T

13

Page 14: Lecture 1 - ida.liu.seTDDC32/lectures/tddc32-01.pdf2 Binary Search Trees Binary Search Trees are not Unique The same data can yield different binary search trees insert: 1,2,4,5,8

Done! 1.42

Trinode restructuring = rotations. . .Some authors use left and right rotations: Single left rotation:

• left part of subtree (a and j) is lowered• we have ”rotated (up) b above a”

c

b

a

j

k

l m

c

b

a

j k l m1.43

Double rotations. . .Two rotations are needed when the nodes to be re-balanced are placed in a zig-zag pattern.

• Rotate b up above a• Rotate b up above c

c

b

a

j

k l

m

cb

a

j k l m

c

b

a

j k l m

1.44

Deletion in an AVL tree

• find and remove as in an ordinary binary search tree• Update balance information on the way back up to the root• If to unbalanced: Restructure . . . but . . .

– When we restore balance in one position it might incur unbalance in another position

– Have to repeat the re-balancing procedure (or balance control) until the root is reached

– At most O(logn) re-balancing operations1.45

Voluntary Homework ProblemA set K = {k1, . . . ,kn} of n≥ 1 keys are to be stored in an AVL tree. Which of the following statements

are always true?

(A) If ki is the smallest key in K, then the left child of the node storing ki in every AVL tree storing K isa leaf node.

(B) Every AVL tree storing K has exactly the same height, no matter what order was used to insert thekeys into the tree.

(C) A preorder traversal of an AVL tree storing K visits the nodes in increasing key order.(D) In every AVL tree storing K the key stored in the root node is the same, no matter what order was

used to insert the keys into the tree.1.46

14