148
Data Structures & Algorithms Subodh Kumar ( [email protected] , Bharti 422) Dept of Computer Sc. & Engg.

Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

Data Structures &

Algorithms

Subodh Kumar([email protected], Bharti 422)

Dept of Computer Sc. & Engg.

Page 2: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

Binar-izing a 2-4 Tree

!71

Page 3: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

Binar-izing a 2-4 Tree

!71

10 15

Page 4: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

Binar-izing a 2-4 Tree

!71

10 15

lg3n = .63 lg2n

Page 5: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

Binar-izing a 2-4 Tree

!71

10 1510

15

lg3n = .63 lg2n

Page 6: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

Binar-izing a 2-4 Tree

!71

10 1510

15 10

15

lg3n = .63 lg2n

Page 7: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

Binar-izing a 2-4 Tree

!71

10 15

21 28 49

10

15 10

15

lg3n = .63 lg2n

Page 8: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

Binar-izing a 2-4 Tree

!71

28

4921

10 15

21 28 49

10

15 10

15

lg3n = .63 lg2n

Page 9: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

Binar-izing a 2-4 Tree

!71

28

4921

10 15

21 28 49

10

15 10

15

10

lg3n = .63 lg2n

Page 10: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

Binar-izing a 2-4 Tree

!71

28

4921

10 15

21 28 49

10

15 10

15

10

lg3n = .63 lg2n

Page 11: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

Binar-izing a 2-4 Tree

!71

28

4921

10 15

21 28 49

10

15 10

15

10

lg3n = .63 lg2n

Page 12: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

Binar-izing a 2-4 Tree

!71

28

4921

10 15

21 28 49

10

15 10

15

10

lg3n = .63 lg2n

Page 13: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

Binar-izing a 2-4 Tree

!71

28

4921

10 15

21 28 49

10

15 10

15

10

lg3n = .63 lg2n

Page 14: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

Binar-izing a 2-4 Tree

!71

28

4921

10 15

21 28 49

10

15 10

15

10

lg3n = .63 lg2n

Page 15: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

Binar-izing a 2-4 Tree

!71

28

4921

10 15

21 28 49

10

15 10

15

10

lg3n = .63 lg2n

Page 16: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

Binar-izing a 2-4 Tree

!71

28

4921

10 15

21 28 49

10

15 10

15

10

lg3n = .63 lg2n

Page 17: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

Binar-izing a 2-4 Tree

!71

28

4921

10 15

21 28 49

10

15 10

15

10

lg3n = .63 lg2n

Page 18: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

Binar-izing a 2-4 Tree

!71

28

4921

10 15

21 28 49

10

15 10

15

10

Red Black Tree

lg3n = .63 lg2n

Page 19: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

Red Black Tree! No red colored node has a red colored child! The number of black colored node on the

path from the root to each null reference is the same

!72

Page 20: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

Red Black Tree! No red colored node has a red colored child! The number of black colored node on the

path from the root to each null reference is the same

!72

=> height ≤ 2 log (n)

Page 21: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

Red Black Tree! No red colored node has a red colored child! The number of black colored node on the

path from the root to each null reference is the same

!72

=> height ≤ 2 log (n)

• Null references are assumed black• Root is always black

Page 22: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

Binar-ized 2-4 Tree

!73

10 15

21 28 49

30 50251 4 7 11 1214 20

2 5 13

Page 23: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

Binar-ized 2-4 Tree

!73

10

15

21 28 49

30 50251 4 7 11 1214 20

2 5 13

Page 24: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

Binar-ized 2-4 Tree

!73

10

15

2

521 28 49

30 50251 4 7 11 1214 20

13

Page 25: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

Binar-ized 2-4 Tree

!73

10

15

2

513 21 28 49

30 50251 4 7 11 1214 20

Page 26: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

Binar-ized 2-4 Tree

!73

10

15

2

513 21

28

49

30 50251 4 7 11 1214 20

Page 27: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

Binar-ized 2-4 Tree

!73

10

15

2

513 21

28

49

1 4 7

11

1214 20 25 5030

Page 28: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

49

Red-Black Tree Insert

!74

10

15

2

5

1 4 7

13

12

11

14

21

28

20 25 5030

Page 29: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

49

Red-Black Tree Insert

!74

10

15

2

5

1 4 7

13

12

11

14

21

28

20 25 5030

Page 30: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

49

Red-Black Tree Insert

!74

10

15

2

5

1 4 7

13

12

11

14

21

28

20 25 5030

Insert

Page 31: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

49

Red-Black Tree Insert

!74

10

15

2

5

1 4 7

13

12

11

14

21

28

20 25 5030

Insert @null in node n

Page 32: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

49

Red-Black Tree Insert

!74

10

15

2

5

1 4 7

13

12

11

14

21

28

20 25 5030

Insert 24

Insert @null in node n

Page 33: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

49

Red-Black Tree Insert

!74

10

15

2

5

1 4 7

13

12

11

14

21

28

20 25 5030

24

Insert 24

Insert @null in node n

Page 34: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

49

Red-Black Tree Insert

!74

10

15

2

5

1 4 7

13

12

11

14

21

28

20 25 5030

24

Insert 24Insert 22

Insert @null in node n

Page 35: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

49

Red-Black Tree Insert

!74

10

15

2

5

1 4 7

13

12

11

14

21

28

20 25 5030

24

22

Insert 24Insert 22

Insert @null in node n

Page 36: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

49

Red-Black Tree Insert

!74

10

15

2

5

1 4 7

13

12

11

14

21

28

20 25 5030

24

22

Insert 24Insert 22

Insert @null in node n

Page 37: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

49

Red-Black Tree Insert

!74

10

15

2

5

1 4 7

13

12

11

14

21

28

20 25 5030

24

22

Insert 24Insert 22

Insert @null in node n

Page 38: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

49

Red-Black Tree Insert

!74

10

15

2

5

1 4 7

13

12

11

14

21

28

20 25 5030

24

22

Insert 24Insert 22

Insert @null in node n

Page 39: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

49

Red-Black Tree Insert

!74

10

15

2

5

1 4 7

13

12

11

14

21

28

20

25

503024

22

Insert 24Insert 22

Insert @null in node n

Page 40: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

49

Red-Black Tree Insert

!74

10

15

2

5

1 4 7

13

12

11

14

21

28

20

25

503024

22

Insert 24Insert 22

Insert @null in node n

Page 41: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

49

Red-Black Tree Insert

!74

10

15

2

5

1 4 7

13

12

11

14

21

28

20

25

503024

22

Insert 24Insert 22Insert 23

Insert @null in node n

Page 42: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

49

Red-Black Tree Insert

!74

10

15

2

5

1 4 7

13

12

11

14

21

28

20

25

503024

22

23

Insert 24Insert 22Insert 23

Insert @null in node n

Page 43: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

49

Red-Black Tree Insert

!74

10

15

2

5

1 4 7

13

12

11

14

21

28

20

25

503024

22

23

Insert 24Insert 22Insert 23

Insert @null in node n

Page 44: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

49

Red-Black Tree Insert

!74

10

15

2

5

1 4 7

13

12

11

14

21

28

20

25

503024

22

23

Insert 24Insert 22Insert 23

Insert @null in node n

Red uncle

Page 45: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

49

Red-Black Tree Insert

!74

10

15

2

5

1 4 7

13

12

11

14

21

28

20

25

503024

22

23

Insert 24Insert 22Insert 23

Insert @null in node n

Red uncle

Page 46: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

49

Red-Black Tree Insert

!74

10

15

2

5

1 4 7

13

12

11

14

21

28

20

25

503024

22

23

Insert 24Insert 22Insert 23

Insert @null in node n

Red uncle

Page 47: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

49

Red-Black Tree Insert

!74

10

15

2

5

1 4 7

13

12

11

14

21

28

20

25

503024

22

23

Insert 24Insert 22Insert 23

Insert @null in node n

Red uncle

Page 48: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

49

Red-Black Tree Insert

!74

10

15

2

5

1 4 7

13

12

11

14

21

28

20

25

503024

22

23

Insert 24Insert 22Insert 23

Insert @null in node n

Red uncle

Page 49: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

49

Red-Black Tree Insert

!74

10

15

2

5

1 4 7

13

12

11

14

21

28

20

25

503024

22

23

Insert 24Insert 22Insert 23

Insert @null in node n

Red uncle

Page 50: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

49

Red-Black Tree Insert

!74

10

15

2

5

1 4 7

13

12

11

14

21

28

20

25

503024

22

23

Insert 24Insert 22Insert 23

Insert @null in node n

Red uncle

Page 51: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

49

Red-Black Tree Insert

!74

10

15

2

5

1 4 7

13

12

11

14

21

28

20

25

503024

22

23

Insert 24Insert 22Insert 23

Insert @null in node n

Red uncle

Page 52: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

49

Red-Black Tree Insert

!74

10

15

2

5

1 4 7

13

12

11

14

21

28

20

25

503024

22

23

Insert 24Insert 22Insert 23

Insert @null in node n

Red uncle

Page 53: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

49

Red-Black Tree Insert

!74

10

15

2

5

1 4 7

13

12

11

14

21

28

20

25

503024

22

23

Insert 24Insert 22Insert 23

Insert @null in node n

Red uncle

Page 54: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

49

Red-Black Tree Insert

!74

10

15

2

5

1 4 7

13

12

11

14

21

28

20

25

503024

22

23

Insert 24Insert 22Insert 23

Insert @null in node n

Red uncle

Page 55: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

49

Red-Black Tree Insert

!74

10

15

2

5

1 4 7

13

12

11

14

21

28

20

25

503024

22

23

Insert 24Insert 22Insert 23

Insert @null in node n

Red uncle

Color new node RedBlack parent ⇒ DoneRed parent ⇒ Black Uncle: Restructure

Red Uncle: Recolor + Recur up

Page 56: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

R-B Tree Deletion

!75

10

15

5

2

1 4 7

13

12

11

14

21 49

28

20 25 5030

24

Page 57: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

R-B Tree Deletion

!75

10

15

5

2

1 4 7

13

12

11

14

21 49

28

20 25 5030

Delete 10

24

Page 58: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

R-B Tree Deletion

!75

10

15

5

2

1 4 7

13

12

11

14

21 49

28

20 25 5030

Delete 10

24

Page 59: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

R-B Tree Deletion

!75

10

15

5

2

1 4 7

13

12

11

14

21 49

28

20 25 5030

Delete 10

24

Page 60: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

R-B Tree Deletion

!75

10

15

5

2

1 4 7

13

12

11

14

21 49

28

20 25 5030

Delete 10

24

Page 61: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

R-B Tree Deletion

!75

10

15

5

2

1 4 7

13

12

11

14

21 49

28

20 25 5030

Delete 10

24

Page 62: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

R-B Tree Deletion

!75

10

15

5

2

1 4 7

13

12

11

14

21 49

28

20 25 5030

Delete 10

24

Page 63: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

R-B Tree Deletion

!75

10

15

5

2

1 4 7

13

12

11

14

21 49

28

20 25 5030

Delete 10

24

Page 64: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

R-B Tree Deletion

!75

10

15

5

2

1 4 7

13

12

11

14

21 49

28

20 25 5030

Delete 10

24

Page 65: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

R-B Tree Deletion

!75

10

15

5

2

1 4 7

13

12

11

14

21 49

28

20 25 5030

Delete 10

24

Page 66: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

R-B Tree Deletion

!75

10

15

5

2

1 4 7

13

12

11

14

21 49

28

20 25 5030

Delete 10

24

Page 67: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

R-B Tree Deletion

!75

10

15

5

2

1 4 7

13

12

11

14

21 49

28

20 25 5030

Delete 10

24

Page 68: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

R-B Tree Deletion

!75

10

15

5

2

1 4 7

13

12

11

14

21 49

28

20 25 5030

Delete 10

24

Page 69: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

R-B Tree Deletion

!75

10

15

5

2

1 4 7

13

12

11

14

21 49

28

20 25 5030

Delete 10

24

Expunge node n with @null in child.

Page 70: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

R-B Tree Deletion

!75

10

15

5

2

1 4 7

13

12

11

14

21 49

28

20 25 5030

Delete 10

24

Expunge node n with @null in child.

Page 71: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

R-B Tree Deletion

!75

10

15

5

2

1 4 7

13

12

11

14

21 49

28

20 25 5030

Delete 10

24

Expunge node n with @null in child.

Page 72: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

R-B Tree Deletion

!75

10

15

5

2

1 4 7

13

12

11

14

21 49

28

20 25 5030

Delete 10

24

Expunge node n with @null in child.

Page 73: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

R-B Tree Deletion

!75

10

15

5

2

1 4 7

13

12

11

14

21 49

28

20 25 5030

Delete 10

24

Expunge node n with @null in child. Simple if n is red.

Page 74: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

R-B Tree Deletion

!75

1015

5

2

1 4 7

13

12

11

14

21 49

28

20 25 5030

Delete 10

24

Expunge node n with @null in child. Simple if n is red.

Page 75: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

R-B Tree Deletion

!75

1015

5

2

1 4 7

13

12

11

14

21 49

28

20 25 5030

Delete 10

24

Expunge node n with @null in child. Simple if n is red.

Page 76: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

R-B Tree Deletion

!75

1015

5

2

1 4 7

13

12

11

14

21 49

28

20 25 5030

Delete 10

24

Expunge node n with @null in child. Simple if n is red.

Page 77: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

R-B Tree Deletion

!75

1015

5

2

1 4 7

13

12

11

14

21 49

28

20 25 5030

Delete 10

24

Expunge node n with @null in child. Simple if n is red.

Page 78: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

R-B Tree Deletion

!75

1015

5

2

1 4 7

13

12

11

14

21 49

28

20 25 5030

Delete 10

24

Expunge node n with @null in child. Simple if n is red.

Page 79: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

R-B Tree Deletion

!75

1015

5

2

1 4 7

13

12

11

14

21 49

28

20 25 5030

Delete 10

24

Expunge node n with @null in child. Simple if n is red.

Page 80: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

R-B Tree Deletion

!75

1015

5

2

1 4 7

13

12

11

14

21 49

28

20 25 5030

Delete 10

24

Expunge node n with @null in child. Simple if n is red.

Page 81: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

R-B Tree Deletion

!75

1015

5

2

1 4 7

13

12

11

14

21 49

28

20 25 5030

Delete 10

24

Expunge node n with @null in child. Simple if n is red.

Page 82: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

R-B Tree Deletion

!75

1015

5

2

1 4 7

13

12

11

14

21 49

28

20 25 5030

Delete 10

24

Expunge node n with @null in child. Simple if n is red.

Page 83: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

R-B Tree Deletion

!75

1015

5

2

1 4 7

13

12

11

14

21 49

28

20 25 5030

Delete 10

24

Expunge node n with @null in child. Simple if n is red.Also simple if n ’s child is red.

Page 84: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

R-B Tree Deletion

!75

1015

5

2

1 4 7

13

12

11

14

21 49

28

20 25 5030

Delete 10

24

Expunge node n with @null in child. Simple if n is red.Also simple if n ’s child is red.

Page 85: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

R-B Tree Deletion

!75

1015

5

2

1 4 7

13

12

11

14

21 49

28

20 25 5030

Delete 10

24

Expunge node n with @null in child. Simple if n is red.Also simple if n ’s child is red.

Page 86: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

R-B Tree Deletion

!75

1015

5

2

1 4 7

13

12

11

14

21 49

28

20 25 5030

Delete 10

24

Expunge node n with @null in child. Simple if n is red.Also simple if n ’s child is red.

Page 87: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

R-B Tree Deletion

!75

1015

5

2

1 4 7

13

12

11

14

21 49

28

20 25 5030

Delete 10

24

Expunge node n with @null in child. Simple if n is red.Also simple if n ’s child is red.

Page 88: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

R-B Tree Deletion

!75

1015

5

2

1 4 7

13

12

11

14

21 49

28

20 25 5030

Delete 10

24

Expunge node n with @null in child. Simple if n is red.Also simple if n ’s child is red.

Page 89: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

R-B Tree Deletion

!75

1015

5

2

1 4 7

13

12

11

14

21 49

28

20 25 5030

Delete 10

24

Expunge node n with @null in child.

Delete 20

Simple if n is red.Also simple if n ’s child is red.

Page 90: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

R-B Tree Deletion

!75

1015

5

2

1 4 7

13

12

11

14

21 49

28

20

25 5030

Delete 10

24

Expunge node n with @null in child.

Delete 20

Simple if n is red.Also simple if n ’s child is red.

Page 91: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

R-B Tree Deletion

!75

1015

5

2

1 4 7

13

12

11

14

21 49

28

20

25 5030

Delete 10

24

Expunge node n with @null in child.

Delete 20

Simple if n is red.Also simple if n ’s child is red.

Page 92: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

R-B Tree Deletion

!75

1015

5

2

1 4 7

13

12

11

14

21 49

28

20

25 5030

Delete 10

24

Expunge node n with @null in child.

Delete 20

Simple if n is red.Also simple if n ’s child is red.

Page 93: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

R-B Tree Deletion

!75

1015

5

2

1 4 7

13

12

11

14

21 49

28

20

25 5030

Delete 10

24

Expunge node n with @null in child.

Delete 20

Simple if n is red.Also simple if n ’s child is red.

Page 94: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

R-B Tree Deletion

!75

1015

5

2

1 4 7

13

12

11

14

21 49

28

20

25 5030

Delete 10

24

Expunge node n with @null in child.

Delete 20

Simple if n is red.Also simple if n ’s child is red.

Page 95: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

Complex Delete Cases

!76

Page 96: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

Complex Delete Cases

!76

Page 97: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

Complex Delete Cases

!76

P

d

d-1

Page 98: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

Complex Delete Cases

!76

S

P

d

d-1d

Page 99: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

S

Complex Delete Cases

!76

S

P P

d

d-1d

d

d-1

Page 100: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

S

Complex Delete Cases

!76

S

P P

d

d-1

n1 n2d d

d

d

d-1

Page 101: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

S

Complex Delete Cases

!76

S

P P

d

d-1

n1 n2d d

d

d

d-1

Page 102: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

S

Complex Delete Cases

!76

S

P P

d

d-1

n1 n2d d

d

d

n2n1

d-1

Page 103: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

S

Complex Delete Cases

!76

S

P P

d

d-1

n1 n2d d

d

d

n2n1

d-1

Count +1

Page 104: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

S

Complex Delete Cases

!76

S

P P

d

d-1

n1 n2d d

d

d

n2n1

d-1

Count +1

Page 105: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

S

Complex Delete Cases

!76

S

P P

d

d-1

n1 n2d d

d

d

n2n1

d-1

Page 106: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

S

Complex Delete Cases

!76

S

P P

d

d-1

n1 n2d d

d

d

n2n1

d-1

Page 107: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

S

S

P

n1

n2

Complex Delete Cases

!76

S

P P

d

d-1

n1 n2d d

d

d

n2n1

d-1

Page 108: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

S

S

P

n1

n2

Complex Delete Cases

!76

S

P P

d

d-1

n1 n2d d

d

d

n2n1

d-1

Page 109: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

S

S

P

n1

n2

Complex Delete Cases

!76

S

P P

d

d-1

n1 n2d d

d

d

n2n1

d-1

Page 110: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

S

S

P

n1

n2

Complex Delete Cases

!76

S

P P

d

d-1

n1 n2d d

d

d

n2n1

d

d-1

Page 111: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

S

S

P

n1

n2

Complex Delete Cases

!76

S

P P

d

d-1

n1 n2d d

d

d

n2n1

d

d

d -1

-1

Page 112: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

S

S

P

n1

n2

Complex Delete Cases

!76

S

P P

d

d-1

n1 n2d d

d

d

n2n1

d

d

d -1

-1

Page 113: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

S

S

P

n1

n2

Complex Delete Cases

!76

S

P P

d

d-1

n1 n2d d

d

d

n2n1

d

d

d

-1

Page 114: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

S

S

P

n1

n2

Complex Delete Cases

!76

S

P P

d

d-1

n1 n2d d

d

d

n2n1

d

d

d

-1

Page 115: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

S

S

P

n1

n2

Complex Delete Cases

!76

S

P P

d

d-1

n1 n2d d

d

d

n2n1

d

d-1

d

d

-1

Page 116: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

S

S

P

n1

n2

Complex Delete Cases

!76

S

P P

d

d-1

n1 n2d d

d

d

d

d-1

d

d

S

P

d

n2n1

d-1-1

Page 117: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

S

S

P

n1

n2

Complex Delete Cases

!76

S

P P

d

d-1

n1 n2d d

d

d

d

d-1

d

d

S

P

d

n2n1

d-1-1

Page 118: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

S

S

P

n1

n2

Complex Delete Cases

!76

S

P P

d

d-1

n1 n2d d

d

d

d

d-1

d

d

S

P

d

n2n1

d-1-1

Page 119: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

S

S

P

n1

n2

Complex Delete Cases

!76

S

P P

d

d-1

n1 n2d d

d

d

d

d-1

d

d

S

P

d

n2n1

d-1

Page 120: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

S

P

d

d

S

S

P

n1

n2

Complex Delete Cases

!76

S

P P

d

d-1

n1 n2d d

d

d

d

d-1

d

d

S

P

d

n2n1

d-1-1

Page 121: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

S

P

d

d

S

S

P

n1

n2

Complex Delete Cases

!76

S

P P

d

d-1

n1 n2d d

d

d

d

d-1

d

d

S

P

d

n2n1

d-1

ns

-1

Page 122: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

S

P

d

d

S

S

P

n1

n2

Complex Delete Cases

!76

S

P P

d

d-1

n1 n2d d

d

d

d

d-1

d

d

S

P

d

n2n1

d-1

ns

n1 n2

-1

Page 123: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

S

P

d

d

S

S

P

n1

n2

Complex Delete Cases

!76

S

P P

d

d-1

n1 n2d d

d

d

d

d-1

d

d

S

P

d

n2n1

d-1

ns

n1 n2

-1

Page 124: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

S

P

d

d

S

n1

SP

n2

S

P

n1

n2

Complex Delete Cases

!76

S

P P

d

d-1

n1 n2d d

d

d

d

d-1

d

d

S

P

d

n2n1

d-1

ns

n1 n2

-1

Page 125: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

S

P

d

d

S

n1

SP

n2

S

P

n1

n2

Complex Delete Cases

!76

S

P P

d

d-1

n1 n2d d

d

d

d

d-1

d

d

S

P

d

n2n1

d-1

ns

n1 n2

-1

Page 126: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

S

P

d

d

S

n1

SP

n2

S

P

n1

n2

Complex Delete Cases

!76

S

P P

d

d-1

n1 n2d d

d

d

d

d-1

d

d

S

P

d

n2n1

d-1

ns

n1 n2

-1

Page 127: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

S

P

d

d

S

n1

SP

n2

S

P

n1

n2

Complex Delete Cases

!76

S

P P

d

d-1

n1 n2d d

d

d

d

d-1

d

d

S

P

d

n2n1

d-1

d-1ns

n1 n2

-1

Page 128: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

S

P

d

d

S

n1

SP

n2

S

P

n1

n2

Complex Delete Cases

!76

S

P P

d

d-1

n1 n2d d

d

d

d

d-1

d

d

S

P

d

n2n1

d-1

d-1ns

n1 n2

-1

Page 129: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

S

P

d

d

S

n1

SP

n2

S

P

n1

n2

Complex Delete Cases

!76

S

P P

d

d-1

n1 n2d d

d

d

d

d-1

d

d

S

P

d

n2n1

d-1

d-1ns

n1 n2 d

-1

Page 130: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

S

P

d

d

S

n1

SP

n2

S

P

n1

n2

Complex Delete Cases

!76

S

P P

d

d-1

n1 n2d d

d

d

d

d-1

d

d

S

P

d

n2n1

d-1

d-1ns

n1 n2 d d

-1

Page 131: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

S

P

d

d

S

n1

SP

n2

S

P

n1

n2

Complex Delete Cases

!76

S

P P

d

d-1

n1 n2d d

d

d

d

d-1

d

d

S

P

d

n2n1

d-1

d-1ns

n1 n2 d d

-1

Page 132: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

S

P

d

d

S

n1

SP

n2

S

P

n1

n2

Complex Delete Cases

!76

S

P P

d

d-1

n1 n2d d

d

d

d

d-1

d

d

S

P

d

n2n1

d-1

d-1ns

n1 n2 d d

-1

if color == Red: "if singlechild().color == Red: singlechild.setcolor(black) "putblack();

putblack(): if root(): " if color == Red: setcolor(Black) " s = sibling(); if s.color == Red: Restructure s.child; s.swapcolor(); # if nephew() == !Red: // i.e., null or Black sibling().setcolor(Red); parent.putblack(); else: Restructure sibling.child() "

Page 133: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

49

Red-Black Tree

!77

10

15

2

5

1 4 7

13

12

11

14

28

20 25 5030

Expunge node with @null in node n

24

Delete 20

26

21

Page 134: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

49

Red-Black Tree

!77

10

15

2

5

1 4 7

13

12

11

14

28

20

25 5030

Expunge node with @null in node n

24

Delete 20

26

21

Page 135: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

49

Red-Black Tree

!77

10

15

2

5

1 4 7

13

12

11

14

28

20

25 5030

Expunge node with @null in node n

24

Delete 20

26

21d = 3

Page 136: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

49

Red-Black Tree

!77

10

15

2

5

1 4 7

13

12

11

14

28

20

25 5030

Expunge node with @null in node n

24

Delete 20

26

21d = 3

Page 137: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

49

Red-Black Tree

!77

10

15

2

5

1 4 7

13

12

11

14

28

20

25 5030

Expunge node with @null in node n

24

Delete 20

26

21d = 3

Red Sibling ⇒ Restructure P,S,n

P←Red; S←Black

Page 138: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

49

Red-Black Tree

!77

10

15

2

5

1 4 7

13

12

11

14

28

20

25 5030

Expunge node with @null in node n

24

Delete 20

26

21d = 3

Red Sibling ⇒ Restructure P,S,n

P←Red; S←Black

Page 139: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

49

Red-Black Tree

!77

10

15

2

5

1 4 7

13

12

11

14

28

20

25

5030

Expunge node with @null in node n

24

Delete 20

2621

d = 3

Red Sibling ⇒ Restructure P,S,n

P←Red; S←Black

Page 140: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

49

Red-Black Tree

!77

10

15

2

5

1 4 7

13

12

11

14

28

20

25

5030

Expunge node with @null in node n

24

Delete 20

2621

d = 3

Red Sibling ⇒ Restructure P,S,n

P←Red; S←Black

Page 141: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

49

Red-Black Tree

!77

10

15

2

5

1 4 7

13

12

11

14

28

20

25

5030

Expunge node with @null in node n

24

Delete 20

2621

d = 3

Red Sibling ⇒ Restructure P,S,n

P←Red; S←Black

Page 142: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

49

Red-Black Tree

!77

10

15

2

5

1 4 7

13

12

11

14

28

20

25

5030

Expunge node with @null in node n

24

Delete 20

2621

d = 3

Red Sibling ⇒ Restructure P,S,n

P←Red; S←Black

Page 143: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

49

Red-Black Tree

!77

10

15

2

5

1 4 7

13

12

11

14

28

20

25

5030

Expunge node with @null in node n

24

Delete 20

2621

d = 3

Red Sibling ⇒ Restructure P,S,n

P←Red; S←Black

Page 144: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

49

Red-Black Tree

!77

10

15

2

5

1 4 7

13

12

11

14

28

20

25

5030

Expunge node with @null in node n

24

Delete 20

2621

d = 3

Red Sibling ⇒ Restructure P,S,n

P←Red; S←Black

Black Sibling ⇒ No red nephew: Recolor S,P; Fix P. Red nephew: Restructure P,S,n; Done.

Page 145: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

49

Red-Black Tree

!77

10

15

2

5

1 4 7

13

12

11

14

28

20

25

5030

Expunge node with @null in node n

24

Delete 20

2621

d = 3

Red Sibling ⇒ Restructure P,S,n

P←Red; S←Black

Black Sibling ⇒ No red nephew: Recolor S,P; Fix P. Red nephew: Restructure P,S,n; Done.

Page 146: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

49

Red-Black Tree

!77

10

15

2

5

1 4 7

13

12

11

14

28

20

25

5030

Expunge node with @null in node n

24

Delete 20

2621

d = 3

Red Sibling ⇒ Restructure P,S,n

P←Red; S←Black

Black Sibling ⇒ No red nephew: Recolor S,P; Fix P. Red nephew: Restructure P,S,n; Done.

Page 147: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

49

Red-Black Tree

!77

10

15

2

5

1 4 7

13

12

11

14

28

20

25

5030

Expunge node with @null in node n

24

Delete 20

2621

d = 3

Red Sibling ⇒ Restructure P,S,n

P←Red; S←Black

Black Sibling ⇒ No red nephew: Recolor S,P; Fix P. Red nephew: Restructure P,S,n; Done.

Page 148: Data Structures Algorithmssubodh/courses/CSL201/lectures/rbtreeanim.pdf · Binar-izing a 2-4 Tree!71 28 21 49 10 15 21 28 49 10 15 10 15 10 lg 3n = .63 lg 2n. Binar-izing a 2-4 Tree!71

49

Red-Black Tree

!77

10

15

2

5

1 4 7

13

12

11

14

28

20

25

5030

Expunge node with @null in node n

24

Delete 20

2621

d = 3

Red Sibling ⇒ Restructure P,S,n

P←Red; S←Black

Black Sibling ⇒ No red nephew: Recolor S,P; Fix P. Red nephew: Restructure P,S,n; Done.