75
Module 4: Dictionaries and Balanced Search Trees CS 240 - Data Structures and Data Management Reza Dorrigiv, Daniel Roche School of Computer Science, University of Waterloo Winter 2010 Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 1 / 29

Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

Module 4: Dictionaries and Balanced Search Trees

CS 240 - Data Structures and Data Management

Reza Dorrigiv, Daniel Roche

School of Computer Science, University of Waterloo

Winter 2010

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 1 / 29

Page 2: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

Dictionary ADT

A dictionary is a collection of items,each of which contains a key and some dataand is called a key-value pair (KVP).Keys can be compared and are typically unique.

Operations:

search(k)

insert(k , v)

delete(k)

optional: join, isEmpty, size, etc.

Examples: symbol table, license plate database

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 2 / 29

Page 3: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

Elementary Implementations

Common assumptions:

Dictionary has n KVPs

Each KVP uses constant space(if not, the “value” could be a pointer)

Comparing keys takes constant time

Unordered array or linked list

search Θ(n)

insert Θ(1)

delete Θ(1) (after a search)

Ordered array or linked list

search Θ(log n)

insert Θ(n)

delete Θ(n)

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29

Page 4: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

Binary Search Trees (review)

Structure A BST is either empty or contains a KVP,left child BST, and right child BST.

Ordering Every key k in T .left is less than the root key.Every key k in T .right is greater than the root key.

15

6

10

8 14

25

23 29

27 50

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 4 / 29

Page 5: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

BST Search and Insert

search(k) Compare k to current node, stop if found,else recurse on subtree unless it’s empty

insert(k , v) Search for k , then insert (k, v) as new node

Example: search(24)

15

6

10

8 14

25

23 29

27 50

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 5 / 29

Page 6: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

BST Search and Insert

search(k) Compare k to current node, stop if found,else recurse on subtree unless it’s empty

insert(k , v) Search for k , then insert (k, v) as new node

Example: search(24)

15

6

10

8 14

25

23 29

27 50

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 5 / 29

Page 7: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

BST Search and Insert

search(k) Compare k to current node, stop if found,else recurse on subtree unless it’s empty

insert(k , v) Search for k , then insert (k, v) as new node

Example: search(24)

15

6

10

8 14

25

23 29

27 50

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 5 / 29

Page 8: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

BST Search and Insert

search(k) Compare k to current node, stop if found,else recurse on subtree unless it’s empty

insert(k , v) Search for k , then insert (k, v) as new node

Example: search(24)

15

6

10

8 14

25

23 29

27 50

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 5 / 29

Page 9: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

BST Search and Insert

search(k) Compare k to current node, stop if found,else recurse on subtree unless it’s empty

insert(k , v) Search for k , then insert (k, v) as new node

Example: insert(24, . . .)

15

6

10

8 14

25

23

24

29

27 50

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 5 / 29

Page 10: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

BST Delete

If node is a leaf, just delete it.

If node has one child, move child up

Else, swap with successor node and then delete

15

6

10

8 14

25

23

24

29

27 50

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 6 / 29

Page 11: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

BST Delete

If node is a leaf, just delete it.

If node has one child, move child up

Else, swap with successor node and then delete

15

6

10

8 14

25

23

24

29

50

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 6 / 29

Page 12: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

BST Delete

If node is a leaf, just delete it.

If node has one child, move child up

Else, swap with successor node and then delete

15

6

10

8 14

25

23

24

29

50

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 6 / 29

Page 13: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

BST Delete

If node is a leaf, just delete it.

If node has one child, move child up

Else, swap with successor node and then delete

15

10

8 14

25

23

24

29

50

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 6 / 29

Page 14: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

BST Delete

If node is a leaf, just delete it.

If node has one child, move child up

Else, swap with successor node and then delete

15

10

8 14

25

23

24

29

50

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 6 / 29

Page 15: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

BST Delete

If node is a leaf, just delete it.

If node has one child, move child up

Else, swap with successor node and then delete

23

10

8 14

25

15

24

29

50

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 6 / 29

Page 16: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

BST Delete

If node is a leaf, just delete it.

If node has one child, move child up

Else, swap with successor node and then delete

23

10

8 14

25

24 29

50

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 6 / 29

Page 17: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

Height of a BST

search, insert, delete all have cost Θ(h), whereh = height of the tree = max. path length from root to leaf

If n items are inserted one-at-a-time, how big is h?

Worst-case:

n − 1 = Θ(n)

Best-case: lg(n + 1)− 1 = Θ(log n)

Average-case: Θ(log n)(just like recursion depth in quick-sort1)

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 7 / 29

Page 18: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

Height of a BST

search, insert, delete all have cost Θ(h), whereh = height of the tree = max. path length from root to leaf

If n items are inserted one-at-a-time, how big is h?

Worst-case: n − 1 = Θ(n)

Best-case:

lg(n + 1)− 1 = Θ(log n)

Average-case: Θ(log n)(just like recursion depth in quick-sort1)

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 7 / 29

Page 19: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

Height of a BST

search, insert, delete all have cost Θ(h), whereh = height of the tree = max. path length from root to leaf

If n items are inserted one-at-a-time, how big is h?

Worst-case: n − 1 = Θ(n)

Best-case: lg(n + 1)− 1 = Θ(log n)

Average-case:

Θ(log n)(just like recursion depth in quick-sort1)

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 7 / 29

Page 20: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

Height of a BST

search, insert, delete all have cost Θ(h), whereh = height of the tree = max. path length from root to leaf

If n items are inserted one-at-a-time, how big is h?

Worst-case: n − 1 = Θ(n)

Best-case: lg(n + 1)− 1 = Θ(log n)

Average-case: Θ(log n)(just like recursion depth in quick-sort1)

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 7 / 29

Page 21: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

AVL Trees

Introduced by Adel’son-Vel’skiı and Landis in 1962,an AVL Tree is a BST with an additional structural property:The heights of the left and right subtree differ by at most 1.

(The height of an empty tree is defined to be −1.)

At each non-empty node, we store height(R)− height(L) ∈ {−1, 0, 1}:−1 means the tree is left-heavy

0 means the tree is balanced

1 means the tree is right-heavy

Why not just store the actual height?It would take Θ(n log log n) space.

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 8 / 29

Page 22: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

AVL Trees

Introduced by Adel’son-Vel’skiı and Landis in 1962,an AVL Tree is a BST with an additional structural property:The heights of the left and right subtree differ by at most 1.

(The height of an empty tree is defined to be −1.)

At each non-empty node, we store height(R)− height(L) ∈ {−1, 0, 1}:−1 means the tree is left-heavy

0 means the tree is balanced

1 means the tree is right-heavy

Why not just store the actual height?

It would take Θ(n log log n) space.

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 8 / 29

Page 23: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

AVL Trees

Introduced by Adel’son-Vel’skiı and Landis in 1962,an AVL Tree is a BST with an additional structural property:The heights of the left and right subtree differ by at most 1.

(The height of an empty tree is defined to be −1.)

At each non-empty node, we store height(R)− height(L) ∈ {−1, 0, 1}:−1 means the tree is left-heavy

0 means the tree is balanced

1 means the tree is right-heavy

Why not just store the actual height?It would take Θ(n log log n) space.

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 8 / 29

Page 24: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

AVL insertion

To perform insert(T , k, v):

First, insert (k , v) into T using usual BST insertion

Then, move up the tree from the new leaf, updating balance factors.

If the balance factor is −1, 0, or 1, then keep going.

If the balance factor is ±2, then call the fix algorithmto “rebalance” at that node.

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 9 / 29

Page 25: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

How to “fix” an unbalanced AVL tree

Goal: change the structure without changing the order

y

x

A B

z

C D

Notice that if heights of A, B, C , D differ by at most 1,then the tree is a proper AVL tree.

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 10 / 29

Page 26: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

Right Rotation

This is a right rotation on node z :

z

y

x

A B

C

D

y

x

A B

z

C D

Note: Only two edges need to be moved, and two balances updated.

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 11 / 29

Page 27: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

Right Rotation

This is a right rotation on node z :

z

y

x

A B

C

D

y

x

A B

z

C D

Note: Only two edges need to be moved, and two balances updated.

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 11 / 29

Page 28: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

Left Rotation

This is a left rotation on node x :

x

A

y

B

z

C D

y

x

A B

z

C D

Again, only two edges need to be moved and two balances updated.

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 12 / 29

Page 29: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

Double Right Rotation

This is a double right rotation on node z :

z

x

A

y

B C

D

z

y

x

A B

C

D

y

x

A B

z

C D

First, a left rotation on the left subtree (x).

Second, a right rotation on the whole tree (z).

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 13 / 29

Page 30: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

Double Right Rotation

This is a double right rotation on node z :

z

x

A

y

B C

D

z

y

x

A B

C

D

y

x

A B

z

C D

First, a left rotation on the left subtree (x).Second, a right rotation on the whole tree (z).

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 13 / 29

Page 31: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

Double Left Rotation

This is a double left rotation on node x :

x

A

z

y

B C

D

y

x

A B

z

C D

Right rotation on right subtree (z),followed by left rotation on the whole tree (x).

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 14 / 29

Page 32: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

Fixing a slightly-unbalanced AVL tree

Idea: Identify one of the previous 4 situations, apply rotations

fix(T )T : AVL tree with T .balance = ±21. if T .balance = −2 then2. if T .left.balance = 1 then3. rotate-left(T .left)4. rotate-right(T )5. else if T .balance = 2 then6. if T .right.balance = −1 then7. rotate-right(T .right)8. rotate-left(T )

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 15 / 29

Page 33: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

AVL Tree Operations

search: Just like in BSTs, costs Θ(height)

insert: Shown already, total cost Θ(height)fix will be called at most once.

delete: First search, then swap with successor (as with BSTs),then move up the tree and apply fix (as with insert).fix may be called Θ(height) times.Total cost is Θ(height).

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 16 / 29

Page 34: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

AVL tree examples

Example: insert(8) 22

-1

10

1

4

1

6

0

14

1

13

0

18

-1

16

0

31

1

28

0

37

1

46

0

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 17 / 29

Page 35: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

AVL tree examples

Example: insert(8) 22

-1

10

1

4

1

6

0

8

0

14

1

13

0

18

-1

16

0

31

1

28

0

37

1

46

0

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 17 / 29

Page 36: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

AVL tree examples

Example: insert(8) 22

-1

10

1

4

1

6

1

8

0

14

1

13

0

18

-1

16

0

31

1

28

0

37

1

46

0

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 17 / 29

Page 37: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

AVL tree examples

Example: insert(8) 22

-1

10

1

4

2

6

1

8

0

14

1

13

0

18

-1

16

0

31

1

28

0

37

1

46

0

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 17 / 29

Page 38: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

AVL tree examples

Example: insert(8) 22

-1

10

1

6

0

4

0

8

0

14

1

13

0

18

-1

16

0

31

1

28

0

37

1

46

0

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 17 / 29

Page 39: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

AVL tree examples

Example: delete(22) 22

-1

10

1

6

0

4

0

8

0

14

1

13

0

18

-1

16

0

31

1

28

0

37

1

46

0

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 17 / 29

Page 40: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

AVL tree examples

Example: delete(22) 28

-1

10

1

6

0

4

0

8

0

14

1

13

0

18

-1

16

0

31

1

37

1

46

0

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 17 / 29

Page 41: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

AVL tree examples

Example: delete(22) 28

-1

10

1

6

0

4

0

8

0

14

1

13

0

18

-1

16

0

31

2

37

1

46

0

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 17 / 29

Page 42: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

AVL tree examples

Example: delete(22) 28

-2

10

1

6

0

4

0

8

0

14

1

13

0

18

-1

16

0

37

0

31

0

46

0

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 17 / 29

Page 43: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

AVL tree examples

Example: delete(22) 14

0

10

-1

6

0

4

0

8

0

13

0

28

0

18

-1

16

0

37

0

31

0

46

0

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 17 / 29

Page 44: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

Height of an AVL tree

Define N(h) to be the least number of nodes in a height-h AVL tree.

One subtree must have height at least h − 1, the other at least h − 2:

N(h) =

1 + N(h − 1) + N(h − 2), h ≥ 11, h = 00, h = −1

What sequence does this look like?

The Fibonacci sequence!

N(h) = Fh+3 − 1 =

⌈ϕh+3

√5

⌋− 1, where ϕ =

1 +√

5

2

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 18 / 29

Page 45: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

Height of an AVL tree

Define N(h) to be the least number of nodes in a height-h AVL tree.

One subtree must have height at least h − 1, the other at least h − 2:

N(h) =

1 + N(h − 1) + N(h − 2), h ≥ 11, h = 00, h = −1

What sequence does this look like? The Fibonacci sequence!

N(h) = Fh+3 − 1 =

⌈ϕh+3

√5

⌋− 1, where ϕ =

1 +√

5

2

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 18 / 29

Page 46: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

AVL Tree Analysis

Easier lower bound on N(h):

N(h) > 2N(h− 2) > 4N(h− 4) > 8N(h− 6) > · · · > 2iN(h− 2i) ≥ 2bh/2c

Since n > 2bh/2c, h ≤ 2 lg n,and an AVL tree with n nodes has height O(log n).Also, n ≤ 2h+1 − 1, so the height is Θ(log n).

⇒ search, insert, delete all cost Θ(log n).

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 19 / 29

Page 47: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

AVL Tree Analysis

Easier lower bound on N(h):

N(h) > 2N(h− 2) > 4N(h− 4) > 8N(h− 6) > · · · > 2iN(h− 2i) ≥ 2bh/2c

Since n > 2bh/2c, h ≤ 2 lg n,and an AVL tree with n nodes has height O(log n).Also, n ≤ 2h+1 − 1, so the height is Θ(log n).

⇒ search, insert, delete all cost Θ(log n).

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 19 / 29

Page 48: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

2-3 Trees

A 2-3 Tree is like a BST with additional structual properties:

Every node either contains one KVP and two children,or two KVPs and three children.

All the leaves are at the same level.(A leaf is a node with empty children.)

Searching through a 1-node is just like in a BST.For a 2-node, we must examine both keys and follow the appropriate path.

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 20 / 29

Page 49: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

Insertion in a 2-3 tree

First, we search to find the leaf where the new key belongs.

If the leaf has only 1 KVP, just add the new one to make a 2-node.

Otherwise, order the three keys as a < b < c .Split the leaf into two 1-nodes, containing a and c ,and (recursively) insert b into the parent along with the new link.

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 21 / 29

Page 50: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

2-3 Tree Insertion

Example: insert(19)

25 43

18

12 21 24

31 36

28 33 39 42

51

48 56 62

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 22 / 29

Page 51: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

2-3 Tree Insertion

Example: insert(19)

25 43

18

12 21 24

31 36

28 33 39 42

51

48 56 62

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 22 / 29

Page 52: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

2-3 Tree Insertion

Example: insert(19)

25 43

18

12 19 21 24

31 36

28 33 39 42

51

48 56 62

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 22 / 29

Page 53: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

2-3 Tree Insertion

Example: insert(19)

25 43

18 21

12 19 24

31 36

28 33 39 42

51

48 56 62

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 22 / 29

Page 54: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

2-3 Tree Insertion

Example: insert(41)

25 43

18 21

12 19 24

31 36

28 33 39 42

51

48 56 62

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 22 / 29

Page 55: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

2-3 Tree Insertion

Example: insert(41)

25 43

18 21

12 19 24

31 36

28 33 39 41 42

51

48 56 62

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 22 / 29

Page 56: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

2-3 Tree Insertion

Example: insert(41)

25 43

18 21

12 19 24

31 36 41

28 33 39 42

51

48 56 62

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 22 / 29

Page 57: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

2-3 Tree Insertion

Example: insert(41)

25 36 43

18 21

12 19 24

31

28 33

41

39 42

51

48 56 62

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 22 / 29

Page 58: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

2-3 Tree Insertion

Example: insert(41)

36

25

18 21

12 19 24

31

28 33

43

41

39 42

51

48 56 62

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 22 / 29

Page 59: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

Deletion from a 2-3 Tree

As with BSTs and AVL trees, we first swap the KVP with its successor,so that we always delete from a leaf.

Say we’re deleting KVP x from a node V :

If X is a 2-node, just delete x .

ElseIf X has a 2-node sibling U, perform a transfer :Put the “intermediate” KVP in the parent between V and U into V ,and replace it with the adjacent KVP from U.

Otherwise, we merge V and a 1-node sibling U:Remove V and (recursively) delete the “intermediate” KVPfrom the parent, adding it to U.

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 23 / 29

Page 60: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

2-3 Tree Deletion

Example: delete(43)

36

25

18 21

12 19 24

31

28 33

43

41

39 42

51

48 56 62

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 24 / 29

Page 61: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

2-3 Tree Deletion

Example: delete(43)

36

25

18 21

12 19 24

31

28 33

48

41

39 42

51

56 62

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 24 / 29

Page 62: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

2-3 Tree Deletion

Example: delete(43)

36

25

18 21

12 19 24

31

28 33

48

41

39 42

56

51 62

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 24 / 29

Page 63: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

2-3 Tree Deletion

Example: delete(19)

36

25

18 21

12 19 24

31

28 33

48

41

39 42

56

51 62

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 24 / 29

Page 64: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

2-3 Tree Deletion

Example: delete(19)

36

25

18

12 21 24

31

28 33

48

41

39 42

56

51 62

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 24 / 29

Page 65: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

2-3 Tree Deletion

Example: delete(19)

36

25

18

12 21 24

31

28 33

48

41

39 42

56

51 62

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 24 / 29

Page 66: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

2-3 Tree Deletion

Example: delete(42)

36

25

18

12 21 24

31

28 33

48

41

39 42

56

51 62

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 24 / 29

Page 67: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

2-3 Tree Deletion

Example: delete(42)

36

25

18

12 21 24

31

28 33

48

39 41

56

51 62

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 24 / 29

Page 68: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

2-3 Tree Deletion

Example: delete(42)

36

25

18

12 21 24

31

28 33

48 56

39 41 51 62

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 24 / 29

Page 69: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

2-3 Tree Deletion

Example: delete(42)

25 36

18

12 21 24

31

28 33

48 56

39 41 51 62

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 24 / 29

Page 70: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

2-3 Tree Deletion

Example: delete(42)

25 36

18

12 21 24

31

28 33

48 56

39 41 51 62

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 24 / 29

Page 71: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

B-Trees

The 2-3 Tree is a specific type of B-tree:

A B-tree of minsize d is a search tree satisfying:

Each node contains at most 2d KVPs.Each non-root node contains at least d KVPs.

All the leaves are at the same level.

Some people call this a B-tree of order (2d + 1), or a (d + 1, 2d + 1)-tree.A 2-3 tree has d = 1.

search, insert, delete work just like for 2-3 trees.

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 25 / 29

Page 72: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

Height of a B-treeWhat is the least number of KVPs in a height-h B-tree?

Level Nodes Node size KVPs0 1 1 11 2 d 2d2 2(d + 1) d 2d(d + 1)3 2(d + 1)2 d 2d(d + 1)2

· · · · · · · · · · · ·h 2(d + 1)h−1 d 2d(d + 1)h−1

Total: 1 +h−1∑i=0

2d(d + 1)i = 2(d + 1)h − 1

Therefore height of tree with n nodes is Θ((log n)/(log d)

).

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 26 / 29

Page 73: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

Analysis of B-tree operations

Assume each node stores its KVPs and child-pointers in a dictionarythat supports O(log d) search, insert, and delete.

Then search, insert, and delete work just like for 2-3 trees, and eachrequire Θ(height) node operations.

Total cost is O

(log n

log d· (log d)

)= O(log n).

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 27 / 29

Page 74: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

Dictionaries in external memory

Tree-based data structures have poor memory locality :If an operation accesses m nodes, then it must accessm spaced-out memory locations.

Observation: Accessing a single location in external memory(e.g. hard disk) automatically loads a whole block (or “page”).

In an AVL tree or 2-3 tree, Θ(log n) pages are loaded in the worst case.

If d is small enough so a 2d-node fits into a single page,then a B-tree of minsize d only loads Θ

((log n)/(log d)

)pages.

This can result in a huge savings:memory access is often the largest time cost in a computation.

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 28 / 29

Page 75: Module 4: Dictionaries and Balanced Search Treesdroche/teaching/240slides/module04.pdf · Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 3 / 29. Binary Search Trees

B-tree variations

Max size 2d + 1: Permitting one additional KVP in each nodeallows insert and delete to avoid backtracking viapre-emptive splitting and pre-emptive merging .

Red-black trees: Identical to a B-tree with minsize 1 and maxsize 3,but each 2-node or 3-node is represented by 2 or 3 binary nodes,and each node holds a “color” value of red or black.

B+-trees: All KVPs are stored at the leaves(interior nodes just have keys),and the leaves are linked sequentially.

Reza Dorrigiv, Daniel Roche (CS, UW) CS240 - Module 4 Winter 2010 29 / 29