1 of 81

Trees

CSE 332 (Section 4)

10/20/2022

Original slides by: Louis Maliyam Monty Nitschke Kevin Pham

2 of 81

Announcements

  • Project 2 is out, please START EARLY!
    • The P1 Hidden Test rerunner is available for you to fix any bugs from P1
    • P2 is much harder than P1, especially on Checkpoint 2!
  • P2 Checkpoint 1 due Thursday (10/20 11:59 PM)
  • EX07 due 10/24
    • We’re skipping EX05 and EX06 this quarter

3 of 81

AVL Tree

4 of 81

AVL Tree Warm-up Questions!

Are the following statements True or False?

  • Any AVL tree is a BST (binary search tree). True / False
  • Any BST is an AVL tree. True / False
  • AVL tree is preferred over BST because AVL tree can re-balance itself. True / False

An example of AVL tree from wikipedia

5 of 81

Problem 0

Let’s discuss!

6 of 81

(Q0) The ABC’s of AVL Trees

What are the constraints on the data types you can store in an AVL tree? When is an AVL tree preferred over another dictionary implementation, such as HashMap?

7 of 81

(Q0) The ABC’s of AVL Trees

What are the constraints on the data types you can store in an AVL tree? When is an AVL tree preferred over another dictionary implementation, such as HashMap?

  • Data types must be Comparable to be stored in an AVL tree (think TreeMap)

  • We prefer an AVL trees when we want to be able to iterate in sorted order

8 of 81

AVL Tree (Rotations)

9 of 81

Review from lecture

Case 1

Case 2

Case 3

Case 4

We will call them

10 of 81

Rotation Review (Single) - Case 1

  • Imbalance at the red node due to insertion into the left subtree of left child

Note: Actual height and subtrees not shown

6

3

>6

2

>3

<6

<2

6

3

>6

2

>3

<6

<2

6

3

>6

2

>3

<6

<2

rotateWithLeft(6)

11 of 81

Rotation Review (Single) - Case 4

  • Imbalance at the red node due to insertion into the right subtree of right child

Note: Actual height and subtrees not shown

6

8

<6

9

>6

<8

>9

6

8

<6

9

>6

<8

>9

6

8

<6

9

>6

<8

>9

rotateWithRight(6)

12 of 81

Rotation Review (Double) - Case 3

  • Imbalance at the red node due to insertion into the left subtree of right child
  • Still not fixed, will need to apply single rotation afterwards to a right-right tree

Note: Actual height and subtrees not shown

5

9

<5

>9

7

>7

<9

5

9

<5

>9

7

>7

<9

5

9

<5

>9

7

>7

<9

rotateWithLeft(9)

Point to new sub-root

13 of 81

Rotation Review (Double) - Case 3

  • Imbalance at the red node due to insertion into the left subtree of right child, now become a right-right tree (Case 4)

Note: Actual height and subtrees not shown

5

9

<5

>9

7

>7

<9

5

9

<5

>9

7

>7

<9

rotateWithRight(5)

5

9

<5

>9

7

>7

<9

14 of 81

Rotation Review (Double) - Case 2

  • Imbalance at the red node due to insertion into the right subtree of left child
  • Still not fixed, will need to apply single rotation afterwards to a left-left tree

Note: Actual height and subtrees not shown

6

3

>6

<3

5

>5

<6

6

3

>6

<3

5

>5

<6

6

3

>6

<3

5

>5

<6

rotateWithRight(3)

Point to new sub-root

15 of 81

Rotation Review (Double) - Case 2

  • Imbalance at the red node due to insertion into the right subtree of left child, now become a left-left tree (Case 1)

Note: Actual height and subtrees not shown

6

3

>6

<3

5

>5

<6

6

3

>6

<3

5

>5

<6

6

3

>6

<3

5

>5

<6

rotateWithLeft(6)

16 of 81

Problem 1

17 of 81

(Q1) Let’s Plant an AVL Tree

10

4

5

8

9

6

11

3

2

1

14

Insert these values in order into an initially empty AVL Tree

18 of 81

(Q1) Let’s Plant an AVL Tree

10

4

5

8

9

6

11

3

2

1

14

Insert these values in order into an initially empty AVL Tree

insert(10)

No imbalance!

19 of 81

(Q1) Let’s Plant an AVL Tree

10

4

5

8

9

6

11

3

2

1

14

Insert these values in order into an initially empty AVL Tree

insert(4)

No imbalance!

20 of 81

(Q1) Let’s Plant an AVL Tree

10

4

5

8

9

6

11

3

2

1

14

Insert these values in order into an initially empty AVL Tree

insert(5)

Imbalance at 10

Case 2

rotateWithRight(4)

21 of 81

(Q1) Let’s Plant an AVL Tree

10

4

5

8

9

6

11

3

2

1

14

Insert these values in order into an initially empty AVL Tree

insert(5)

Imbalance at 10

Case 2

rotateWithLeft(10)

rotateWithRight(4)

22 of 81

(Q1) Let’s Plant an AVL Tree

10

4

5

8

9

6

11

3

2

1

14

Insert these values in order into an initially empty AVL Tree

insert(5)

Balanced - YAY!

23 of 81

(Q1) Let’s Plant an AVL Tree

10

4

5

8

9

6

11

3

2

1

14

Insert these values in order into an initially empty AVL Tree

insert(8)

No imbalance!

24 of 81

(Q1) Let’s Plant an AVL Tree

10

4

5

8

9

6

11

3

2

1

14

Insert these values in order into an initially empty AVL Tree

insert(9)

Imbalance at 10

Case 2

rotateWithRight(8)

25 of 81

(Q1) Let’s Plant an AVL Tree

10

4

5

8

9

6

11

3

2

1

14

Insert these values in order into an initially empty AVL Tree

insert(9)

Imbalance at 10

Case 2

rotateWithLeft(10)

rotateWithRight(8)

26 of 81

(Q1) Let’s Plant an AVL Tree

10

4

5

8

9

6

11

3

2

1

14

Insert these values in order into an initially empty AVL Tree

insert(9)

Balanced - YAY!

27 of 81

(Q1) Let’s Plant an AVL Tree

10

4

5

8

9

6

11

3

2

1

14

Insert these values in order into an initially empty AVL Tree

insert(6)

Imbalance at 5

Case 3

rotateWithLeft(9)

28 of 81

(Q1) Let’s Plant an AVL Tree

10

4

5

8

9

6

11

3

2

1

14

Insert these values in order into an initially empty AVL Tree

insert(6)

Imbalance at 5

Case 3

rotateWithRight(5)

rotateWithLeft(9)

29 of 81

(Q1) Let’s Plant an AVL Tree

10

4

5

8

9

6

11

3

2

1

14

Insert these values in order into an initially empty AVL Tree

insert(6)

Balanced - YAY!

30 of 81

(Q1) Let’s Plant an AVL Tree

10

4

5

8

9

6

11

3

2

1

14

Insert these values in order into an initially empty AVL Tree

insert(11)

Imbalance at 9

Case 4

rotateWithRight(9)

31 of 81

(Q1) Let’s Plant an AVL Tree

10

4

5

8

9

6

11

3

2

1

14

Insert these values in order into an initially empty AVL Tree

insert(11)

Balanced - YAY!

32 of 81

(Q1) Let’s Plant an AVL Tree

10

4

5

8

9

6

11

3

2

1

14

Insert these values in order into an initially empty AVL Tree

insert(3)

No imbalance!

33 of 81

(Q1) Let’s Plant an AVL Tree

10

4

5

8

9

6

11

3

2

1

14

Insert these values in order into an initially empty AVL Tree

insert(2)

Imbalance at 4

Case 1

rotateWithLeft(4)

34 of 81

(Q1) Let’s Plant an AVL Tree

10

4

5

8

9

6

11

3

2

1

14

Insert these values in order into an initially empty AVL Tree

insert(2)

Balanced - YAY!

35 of 81

(Q1) Let’s Plant an AVL Tree

10

4

5

8

9

6

11

3

2

1

14

Insert these values in order into an initially empty AVL Tree

insert(1)

Imbalance at 5

Case 1

rotateWithLeft(5)

36 of 81

(Q1) Let’s Plant an AVL Tree

10

4

5

8

9

6

11

3

2

1

14

Insert these values in order into an initially empty AVL Tree

insert(1)

Balanced - YAY!

37 of 81

(Q1) Let’s Plant an AVL Tree

10

4

5

8

9

6

11

3

2

1

14

Insert these values in order into an initially empty AVL Tree

insert(14)

No imbalance!

38 of 81

(Q1) Let’s Plant an AVL Tree - Breakout 1

10

4

5

8

9

6

11

3

2

1

14

Insert these values in order into an initially empty AVL Tree

39 of 81

B-Trees

40 of 81

Review from lecture

M=3 and L=3

No. of children

(for internal nodes)

No. of key-value pairs (for leaf nodes)

41 of 81

Problem 4

42 of 81

(Q4) The ABC’s of B-Trees

What properties must a B-tree of n values have with given values for M and L?

43 of 81

(Q4) The ABC’s of B-Trees

What properties must a B-tree of n values have with given values for M and L?

  • Order Property
    • Every subtree between keys a and b contains all data x where
    • The values in the leaves are in key sorted order
    • The keys in the internal nodes are stored in sorted order
  • Structure Property
    • If
      • The root is a leaf with n values
      • Otherwise: Root is an internal node with between 2 and M children
    • All internal nodes are half-full (ie have between and M children)
    • All leaf nodes are half-full (ie have between and L key-value pairs)
    • All leaf nodes must be at the same depth

44 of 81

Problem 7(a)

45 of 81

(Q7a) B-Trees

10

10

Node Bank

(copy and paste to form your own B Tree)

10

10

10

Insert the following into an empty B-Tree with M=3 and L=3: 12, 24, 36, 17, 18, 5, 22, 20.

Structure Properties

Root

  • If n L, root is a leaf
  • Otherwise,

Internal Nodes

  • Must have

Leaf Nodes

  • Must have
  • Must be at the same depth

46 of 81

(Q7a) B-Trees

Insert the following into an empty B-Tree with M=3 and L=3: 12, 24, 36, 17, 18, 5, 22, 20.

Structure Properties

Root

  • If n L, root is a leaf
  • Otherwise,

Internal Nodes

  • Must have

Leaf Nodes

  • Must have
  • Must be at the same depth

12

insert(12)

47 of 81

(Q7a) B-Trees

Insert the following into an empty B-Tree with M=3 and L=3: 12, 24, 36, 17, 18, 5, 22, 20.

Structure Properties

Root

  • If n L, root is a leaf
  • Otherwise,

Internal Nodes

  • Must have

Leaf Nodes

  • Must have
  • Must be at the same depth

12

24

insert(24)

48 of 81

(Q7a) B-Trees

Insert the following into an empty B-Tree with M=3 and L=3: 12, 24, 36, 17, 18, 5, 22, 20.

Structure Properties

Root

  • If n L, root is a leaf
  • Otherwise,

Internal Nodes

  • Must have

Leaf Nodes

  • Must have
  • Must be at the same depth

12

24

36

insert(36)

49 of 81

(Q7a) B-Trees

Insert the following into an empty B-Tree with M=3 and L=3: 12, 24, 36, 17, 18, 5, 22, 20.

Structure Properties

Root

  • If n L, root is a leaf
  • Otherwise,

Internal Nodes

  • Must have

Leaf Nodes

  • Must have
  • Must be at the same depth

insert(17)

Structure issue

12

17

24

36

50 of 81

(Q7a) B-Trees

Insert the following into an empty B-Tree with M=3 and L=3: 12, 24, 36, 17, 18, 5, 22, 20.

Structure Properties

Root

  • If n L, root is a leaf
  • Otherwise,

Internal Nodes

  • Must have

Leaf Nodes

  • Must have
  • Must be at the same depth

12

17

insert(17)

Structure issue

Split leaf node

24

36

51 of 81

(Q7a) B-Trees

Insert the following into an empty B-Tree with M=3 and L=3: 12, 24, 36, 17, 18, 5, 22, 20.

Structure Properties

Root

  • If n L, root is a leaf
  • Otherwise,

Internal Nodes

  • Must have

Leaf Nodes

  • Must have
  • Must be at the same depth

12

17

insert(17)

Structure issue

Attach to new internal node

24

36

24

52 of 81

(Q7a) B-Trees

Insert the following into an empty B-Tree with M=3 and L=3: 12, 24, 36, 17, 18, 5, 22, 20.

Structure Properties

Root

  • If n L, root is a leaf
  • Otherwise,

Internal Nodes

  • Must have

Leaf Nodes

  • Must have
  • Must be at the same depth

12

17

18

insert(18)

24

36

24

53 of 81

(Q7a) B-Trees

Insert the following into an empty B-Tree with M=3 and L=3: 12, 24, 36, 17, 18, 5, 22, 20.

Structure Properties

Root

  • If n L, root is a leaf
  • Otherwise,

Internal Nodes

  • Must have

Leaf Nodes

  • Must have
  • Must be at the same depth

5

12

17

insert(5)

24

36

24

18

Structure issue

54 of 81

(Q7a) B-Trees

Insert the following into an empty B-Tree with M=3 and L=3: 12, 24, 36, 17, 18, 5, 22, 20.

Structure Properties

Root

  • If n L, root is a leaf
  • Otherwise,

Internal Nodes

  • Must have

Leaf Nodes

  • Must have
  • Must be at the same depth

5

12

17

insert(5)

24

36

24

18

Structure issue

Split leaf node

55 of 81

(Q7a) B-Trees

Insert the following into an empty B-Tree with M=3 and L=3: 12, 24, 36, 17, 18, 5, 22, 20.

Structure Properties

Root

  • If n L, root is a leaf
  • Otherwise,

Internal Nodes

  • Must have

Leaf Nodes

  • Must have
  • Must be at the same depth

5

12

17

insert(5)

24

36

17

24

18

Structure issue

Attach to internal node & Update sign post

56 of 81

(Q7a) B-Trees

Insert the following into an empty B-Tree with M=3 and L=3: 12, 24, 36, 17, 18, 5, 22, 20.

Structure Properties

Root

  • If n L, root is a leaf
  • Otherwise,

Internal Nodes

  • Must have

Leaf Nodes

  • Must have
  • Must be at the same depth

5

12

17

insert(22)

24

36

17

24

18

22

57 of 81

(Q7a) B-Trees

Insert the following into an empty B-Tree with M=3 and L=3: 12, 24, 36, 17, 18, 5, 22, 20.

Structure Properties

Root

  • If n L, root is a leaf
  • Otherwise,

Internal Nodes

  • Must have

Leaf Nodes

  • Must have
  • Must be at the same depth

5

12

17

insert(20)

24

36

17

24

18

20

22

Structure issue

58 of 81

(Q7a) B-Trees

Insert the following into an empty B-Tree with M=3 and L=3: 12, 24, 36, 17, 18, 5, 22, 20.

Structure Properties

Root

  • If n L, root is a leaf
  • Otherwise,

Internal Nodes

  • Must have

Leaf Nodes

  • Must have
  • Must be at the same depth

5

12

17

insert(20)

24

36

17

24

18

20

22

Structure issue

Split leaf node

59 of 81

(Q7a) B-Trees

Insert the following into an empty B-Tree with M=3 and L=3: 12, 24, 36, 17, 18, 5, 22, 20.

Structure Properties

Root

  • If n L, root is a leaf
  • Otherwise,

Internal Nodes

  • Must have

Leaf Nodes

  • Must have
  • Must be at the same depth

5

12

17

insert(20)

24

36

17

24

18

20

22

Try attaching to internal node

20

Structure issue

60 of 81

(Q7a) B-Trees

Insert the following into an empty B-Tree with M=3 and L=3: 12, 24, 36, 17, 18, 5, 22, 20.

Structure Properties

Root

  • If n L, root is a leaf
  • Otherwise,

Internal Nodes

  • Must have

Leaf Nodes

  • Must have
  • Must be at the same depth

5

12

17

insert(20)

24

36

17

24

18

20

22

20

Another structure issue

61 of 81

(Q7a) B-Trees

Insert the following into an empty B-Tree with M=3 and L=3: 12, 24, 36, 17, 18, 5, 22, 20.

Structure Properties

Root

  • If n L, root is a leaf
  • Otherwise,

Internal Nodes

  • Must have

Leaf Nodes

  • Must have
  • Must be at the same depth

5

12

17

insert(20)

24

36

17

24

18

20

22

Another structure issue

Split internal node

62 of 81

(Q7a) B-Trees

Insert the following into an empty B-Tree with M=3 and L=3: 12, 24, 36, 17, 18, 5, 22, 20.

Structure Properties

Root

  • If n L, root is a leaf
  • Otherwise,

Internal Nodes

  • Must have

Leaf Nodes

  • Must have
  • Must be at the same depth

5

12

17

insert(20)

24

36

17

24

18

20

22

Another structure issue

Attach to new internal node

20

63 of 81

(Q7a) B-Trees - Breakout 1

10

10

Node Bank

(copy and paste to form your own B+ Tree)

10

10

10

Insert the following into an empty B-Tree with M=3 and L=3: 12, 24, 36, 17, 18, 5, 22, 20.

Structure Properties

Root

  • If n L, root is a leaf
  • Otherwise,

Internal Nodes

  • Must have

Leaf Nodes

  • Must have
  • Must be at the same depth

64 of 81

Problem 7(b)

65 of 81

(Q7b) B-Trees

Delete 17, 12, 22, 5, 36.

Structure Properties

Root

  • If n L, root is a leaf
  • Otherwise,

Internal Nodes

  • Must have

Leaf Nodes

  • Must have
  • Must be at the same depth

5

12

17

24

36

17

24

18

20

22

20

66 of 81

(Q7b) B-Trees

Delete 17, 12, 22, 5, 36.

Structure Properties

Root

  • If n L, root is a leaf
  • Otherwise,

Internal Nodes

  • Must have

Leaf Nodes

  • Must have
  • Must be at the same depth

5

12

18

24

36

18

24

20

22

20

delete(17)

Structure issue

67 of 81

(Q7b) B-Trees

Delete 17, 12, 22, 5, 36.

Structure Properties

Root

  • If n L, root is a leaf
  • Otherwise,

Internal Nodes

  • Must have

Leaf Nodes

  • Must have
  • Must be at the same depth

5

12

24

36

18

24

18

20

22

20

delete(17)

Structure issue

Merge with neighboring leaf node

68 of 81

(Q7b) B-Trees

Delete 17, 12, 22, 5, 36.

Structure Properties

Root

  • If n L, root is a leaf
  • Otherwise,

Internal Nodes

  • Must have

Leaf Nodes

  • Must have
  • Must be at the same depth

5

12

24

36

18

24

18

20

22

20

delete(17)

Structure issue

Another structure issue

69 of 81

(Q7b) B-Trees

Delete 17, 12, 22, 5, 36.

Structure Properties

Root

  • If n L, root is a leaf
  • Otherwise,

Internal Nodes

  • Must have

Leaf Nodes

  • Must have
  • Must be at the same depth

5

12

24

36

18

24

18

20

22

delete(17)

Structure issue

Another structure issue

Delete root internal node

70 of 81

(Q7b) B-Trees

Delete 17, 12, 22, 5, 36.

Structure Properties

Root

  • If n L, root is a leaf
  • Otherwise,

Internal Nodes

  • Must have

Leaf Nodes

  • Must have
  • Must be at the same depth

delete(17)

Structure issue

Another structure issue

Merge internal nodes and update sign post.

5

12

20

24

36

20

24

22

18

71 of 81

(Q7b) B-Trees

Delete 17, 12, 22, 5, 36.

Structure Properties

Root

  • If n L, root is a leaf
  • Otherwise,

Internal Nodes

  • Must have

Leaf Nodes

  • Must have
  • Must be at the same depth

delete(12)

5

18

20

24

36

20

24

22

72 of 81

(Q7b) B-Trees

Delete 17, 12, 22, 5, 36.

Structure Properties

Root

  • If n L, root is a leaf
  • Otherwise,

Internal Nodes

  • Must have

Leaf Nodes

  • Must have
  • Must be at the same depth

delete(22)

5

18

20

24

36

20

24

Structure issue

73 of 81

(Q7b) B-Trees

Delete 17, 12, 22, 5, 36.

Structure Properties

Root

  • If n L, root is a leaf
  • Otherwise,

Internal Nodes

  • Must have

Leaf Nodes

  • Must have
  • Must be at the same depth

delete(22)

5

18

24

36

20

24

20

Structure issue

Merge with neighboring leaf node.

74 of 81

(Q7b) B-Trees

Delete 17, 12, 22, 5, 36.

Structure Properties

Root

  • If n L, root is a leaf
  • Otherwise,

Internal Nodes

  • Must have

Leaf Nodes

  • Must have
  • Must be at the same depth

delete(22)

Structure issue

Update sign post.

5

18

20

24

36

24

75 of 81

(Q7b) B-Trees

Delete 17, 12, 22, 5, 36.

Structure Properties

Root

  • If n L, root is a leaf
  • Otherwise,

Internal Nodes

  • Must have

Leaf Nodes

  • Must have
  • Must be at the same depth

delete(5)

18

20

24

36

24

76 of 81

(Q7b) B-Trees

Delete 17, 12, 22, 5, 36.

Structure Properties

Root

  • If n L, root is a leaf
  • Otherwise,

Internal Nodes

  • Must have

Leaf Nodes

  • Must have
  • Must be at the same depth

delete(36)

18

20

24

24

Structure issue

77 of 81

(Q7b) B-Trees

Delete 17, 12, 22, 5, 36.

Structure Properties

Root

  • If n L, root is a leaf
  • Otherwise,

Internal Nodes

  • Must have

Leaf Nodes

  • Must have
  • Must be at the same depth

delete(36)

18

20

24

Structure issue

Delete internal node.

78 of 81

(Q7b) B-Trees

Delete 17, 12, 22, 5, 36.

Structure Properties

Root

  • If n L, root is a leaf
  • Otherwise,

Internal Nodes

  • Must have

Leaf Nodes

  • Must have
  • Must be at the same depth

delete(36)

Structure issue

Merge leaf nodes.

18

20

24

Final Result!

79 of 81

(Q7b) B-Tree - Breakout 1

Delete 17, 12, 22, 5, 36.

Structure Properties

Root

  • If n L, root is a leaf
  • Otherwise,

Internal Nodes

  • Must have

Leaf Nodes

  • Must have
  • Must be at the same depth

5

12

17

24

36

17

24

18

20

22

20

80 of 81

Math of B-Trees

81 of 81

Fitting a B Tree node in a page!

Assume that

  • 1 page (on disk) size is p bytes
  • Key size is k bytes
  • Pointer size is t bytes
  • Key-value pair size is v bytes

How should we decide parameters M and L for our B tree to efficiently use the memory to the fullest?

Internal Nodes

Leaf Nodes