+ All Categories
Home > Documents > TCSS 342, Winter 2006 Lecture Notes

TCSS 342, Winter 2006 Lecture Notes

Date post: 04-Jan-2016
Category:
Upload: chastity-preston
View: 32 times
Download: 2 times
Share this document with a friend
Description:
TCSS 342, Winter 2006 Lecture Notes. Balanced Binary Search Trees. Objectives. Learn about balanced binary search trees AVL trees red-black trees Talk about why they work Talk about their run-time performance. AVL Trees. In 1960 two Russian mathematicians - PowerPoint PPT Presentation
83
TCSS 342B v1.0 1 TCSS 342, Winter 2006 Lecture Notes Balanced Binary Search Trees
Transcript
Page 1: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

1

TCSS 342, Winter 2006Lecture Notes

Balanced Binary Search Trees

Page 2: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

2

Objectives• Learn about balanced binary search trees

– AVL trees– red-black trees

• Talk about why they work

• Talk about their run-time performance.

Page 3: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

3

AVL Trees

• In 1960 two Russian mathematicians

• Georgii Maksimovich Adel'son-Vel'skii

• Evgenii Mikhailovich Landis

• developed a technique for keeping a binary search tree balanced as items are inserted into it.

• called AVL trees.

Page 4: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

4

Balanced AVL Tree

Page 5: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

5

extremely unbalanced tree

Page 6: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

6

AVL Trees• The balance factor of node x =

• height of x's right subtree minus height of x's left subtree – define height of empty tree to be -1.

• binary search tree is an AVL tree if– balance factor of each node is 0, 1 or -1

• Are AVL trees always completely balanced?

Page 7: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

7

Which are AVL Trees?

Page 8: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

8

Examples of AVL Trees

0

0 0 -1

-1 0

0

1

-1 0

0 -1 1

0 0

Page 9: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

9

Not AVL Trees

-2

-1

-2

2 0

0

-1

-1 2

0 1

0

0 -1

Page 10: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

10

AVL Tree Data Structure

• extension of binary search tree where trees are always AVL trees.

• Maintain AVL property using rotations• Rotations occur when tree becomes

unbalanced from insertion or deletion• At each node, keep track of height of

subtree rooted at node (for balance factor computations)

Page 11: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

11

balancing a tree with a right rotation

Page 12: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

12

A right rotation in an AVL tree

Page 13: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

13

FIGURE 13.11 Unbalanced tree and balanced tree after a left rotation

Page 14: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

14

figure 10.12 A right-left rotation

Page 15: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

15

figure 10.13 A left-right rotation

Page 16: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

16

AVL insertion• After normal BST insert, update heights from new leaf up

towards root. If balance factor changes to +2 or -2, then use rotation(s) to rebalance.

• Let A be the first unbalanced node found. (This is the current lowest depth node that is a non-AVL subtree). Four cases: 1. A has balance factor -2 and A’s left child has balance factor –1.

Then perform right rotation around A. (right rotation)

2. A has and balance factor -2 and A’s left child has balance factor 1. Then perform left rotation around A’s left child, followed by right rotation around A. (left-right rotation)

Page 17: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

17

AVL insertion (2)• Let A be the unbalanced node. Remaining 2 cases are

mirror image of the two before. 3. A has balance factor 2 and A’s right child has balance factor –1.

Then perform right rotation around A’s right child, followed by left rotation around A. (right-left rotation)

4. A has balance factor 2 and A’s right child has balance factor 1. Then perform left rotation around A. (left rotation)

• After rebalancing, continue up the tree updating heights. (and checking for imbalances in balance factor)

• Are all cases handled?

• What if new item added was A’s left child or right child?

• What if A’s child has balance factor 0?

Page 18: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

18

right rotation (clockwise): left child becomes parent; original parent demoted to right

Right rotation to fix Case 1

Page 19: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

19

left rotation (counter-clockwise): right child becomes parent; original parent demoted to left

Left rotation to fix Case 4

Page 20: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

20

Problem: Cases 2, 3 a single right rotation does not fix Case 2! a single left rotation also does not fix Case 3

Page 21: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

21

Left-right rotation to fix Case 2 left-right double rotation: a left rotation of

the left child, followed by a right rotation at the parent

Page 22: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

22

Right-left rotation to fix Case 3 right-left double rotation: a right rotation of

the right child, followed by a left rotation at the parent

Page 23: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

23

AVL rotation notes• right-left and left-right rotation sometimes called double

rotation• If subtree rooted at A was an AVL tree before the

insertion, then– subtree rooted at A is always an AVL tree after applying the

rotation(s) specified for each case. [Correctness]– no further rotations will be made for ancestors of A. – If A has balance factor –2, then

• A’s left child cannot have balance factor 0. • A’s left child has balance factor is –1 if and only if new node was

added to subtree rooted at A’s leftmost grandchild.• A’s left child has balance factor is 1 if and only if new node was

added to subtree rooted at A’s left child’s right child. • new node must be added at grandchild level of A or deeper.

Page 24: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

24

a right-left rotationafter removal

Page 25: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

25

AVL deletion• Perform normal BST remove (with replacement of node to

be removed with its successor). Then update heights from replacement node location upwards towards root. If balance factor changes to +2 or -2, then use rotation(s) to rebalance.

• Let A be the first unbalanced node found. (This is the current lowest depth node that is a non-AVL subtree). At least four cases: 1. A has balance factor -2 and A’s left child has balance factor –1.

Then perform right rotation around A. (right rotation)2. A has and balance factor -2 and A’s left child has balance factor 1.

Then perform left rotation around A’s left child, followed by right rotation around A. (left-right rotation)

Page 26: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

26

AVL deletion (2)• Let A be the unbalanced node. Additional 2 mirror image

cases:3. A has balance factor 2 and A’s right child has balance factor –1.

Then perform right rotation around A’s right child, followed by left rotation around A. (right-left rotation)

4. A has balance factor 2 and A’s right child has balance factor 1. Then perform left rotation around A. (left rotation)

5. Additional cases?

• After rebalancing, continue up the tree updating heights. Must continue checking for imbalances in balance factor, and rebalancing if necessary.

• Are all cases handled? • What if item removed was A’s left child or right child? • What if A’s child has balance factor 0?

Page 27: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

27

Red-Black Trees

• Root property: Root is BLACK.• External Property: Every external node is BLACK

(external nodes: null nodes) – external nodes not drawn in pictures..

• Internal property: Children of a RED node are BLACK.

• Depth property: All external nodes have the same BLACK depth. – (BLACK depth = depth counting just black nodes)

Page 28: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

28

A RedBlack tree.Black depth 3.

30

15 70

20 8510

5

60

6550

40

9080

55

Page 29: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

29

figure 10.16 Valid red/black trees

Page 30: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

30

red/black tree after insertion

Page 31: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

31

red/black tree after removal

Page 32: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

32

RedBlack

Insertion

Page 33: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

33

Red Black Trees, Insertion

1. Find proper external node.2. Insert and color node red.3. No black depth violation but may violate the

red-black parent-child relationship.4. Fix red-red violation on current level, then move

upwards and repeatLet: z be the current red node with a red parent v. (z may

have been just inserted). Let u be its grandparent. u must be black.Two cases, discussed next

5. Color root black.

Page 34: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

34

Fixing Tree, Case one• Red child, red parent. Parent has a black

sibling

a

b u

v w

zVl

ZlZr

Page 35: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

35

Case I: Rotate and recolor• Z-middle key. Black height does not

change! No more red-red.

a

b z

v

w

u

Vl Zl Zr

Page 36: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

36

Still Case One

a

b u

v w

z

Vr

ZlZr

Page 37: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

37

Case I: Rotate and recolor• v-middle key a

b v

z u

ZrZlw

Vr

Page 38: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

38

Case II

• Red child, red parent. Parent has a red sibling.

a

b u

v w

zVl

Zr

Page 39: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

39

Case II: Recolor

• Red-red may move up…

a

b u

v w

zVl

ZrZl

Page 40: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

40

Red Black Trees, Insertion

4. Fix red-red violation on current level, then move upwards (by two levels) and repeat

Let: z, v, u be current red node, red parent, and black grandparent,

Case one: v has black sibling. Then rotate middle key of (z,v,u) up to the grandparent position. Color middle key (in former grandparent position) black, and its children red.

Case two: v has red sibling. Then recolor grandparent red and its children black.

Page 41: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

41

Red Black Tree

• Insert 10 – root

10

Page 42: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

42

Red Black Tree

• Insert 10 – root (external nodes not shown)

10

Page 43: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

43

Red Black Tree

• Insert 85

10

85

Page 44: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

44

Red Black Tree

• Insert 15

10

85

15

Page 45: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

45

Red Black Tree

• Rotate – Change colors

15

10 85

Page 46: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

46

Red Black Tree

• Insert 70

15

10 85

70

Page 47: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

47

Red Black Tree

• Change Color

15

10 85

70

Page 48: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

48

Red Black Tree

• Insert 20 (sibling of parent is black)

20

15

10 85

70

Page 49: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

49

Red Black Tree

• Rotate

15

10 70

20 85

Page 50: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

50

Red Black Tree

• Insert 60 (sibling of parent is red)

15

10 70

20 85

60

Page 51: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

51

Red Black Tree

• Change Color

15

10 70

20 85

60

Page 52: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

52

Red Black Tree

• Insert 30 (sibling of parent is black)

15

10 70

20 85

60

30

Page 53: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

53

Red Black Tree

• Rotate

15

10 70

30 85

6020

Page 54: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

54

Red Black Tree

• Insert 50 (sibling ?)

15

10 70

30 85

6020

50

Page 55: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

55

Red Black Tree

• Insert 50 (sibling of 70 is black!)

15

10 70

30 85

6020

50Oops, red-red. ROTATE!

Child 30

Parent 70

gramps 15

Page 56: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

56

Red Black Tree

• Double Rotate – Adjust colors

30

15 70

20 8510 60

50Child-Parent-Gramps

Middle goes to “top”

Previous top becomes child. Its right child is middles left child.

Page 57: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

57

Red Black Tree

• Insert 65

30

15 70

20 8510 60

6550

Page 58: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

58

Red Black Tree

• Insert 80

30

15 70

20 8510 60

6550 80

Page 59: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

59

Red Black Tree

• Insert 90

30

15 70

20 8510 60

6550 9080

Page 60: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

60

Red Black Tree

• Insert 40

30

15 70

20 8510 60

6550

40

9080

Page 61: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

61

Red Black Tree

• Adjust color

30

15 70

20 8510 60

6550

40

9080

Page 62: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

62

Red Black Tree

• Adjust color

30

15 70

20 8510 60

6550

40

9080

Page 63: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

63

Red Black Tree

• Insert 5

30

15 70

20 8510

5

60

6550

40

9080

Page 64: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

64

Red Black Tree

• Insert 55

30

15 70

20 8510

5

60

6550

40

9080

55

Page 65: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

65

Delete

• We first note that a red node is either a leaf or must have two children.

• Also, if a black node has a single child it must be a red leaf.

• Swap X with inorder successor.• If inorder successor is red, (must be a leaf) delete.

If it is a single child parent, delete and change its child color to black. In both cases the resulting tree is a legit red-black tree.

Page 66: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

66

Delete demo

• Delete 30: Swap with 40 and delete red leaf.

30

15 70

20 8510

5

60

6550

40

9080

55

Page 67: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

67

40

15 70

20 8510

5

60

6550 9080

55

Page 68: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

68

Inorder successor is BlackChange colors along the traverse path so that the leaf to be deleted is RED.

Delete 15.30

15 70

20 8510

5

60

6550

40

9080

55

Page 69: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

69

General strategy

• As you traverse the tree to locate the inorder successor let X be the current node, T its sibling and P the parent.

• Color the root red.

• Retain: “the color of P is red.”

• If all children of X and T are black:

• P Black, X Red, T Red

Page 70: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

70

X T

P

Both children of X and T are black:

P Black X Red, T Red

A B

Page 71: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

71

X T

P

If X is a leaf we are done. Recall: x is the inorder successor!

A B

Page 72: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

72

X T

P

C1

B C

D

Zig-Zag, C1 Middle key.

A

Even though we want to proceed with X we have a red-red violation that needs to be fixed.

T has a red child.

Even though we want to proceed with X we have a red-red violation that needs to be fixed.

T has a red child.

Page 73: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

73

P T

C1

B C D

X

A

Note: black depth remains unchanged!

Note: black depth remains unchanged!

Page 74: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

74

Third case

X T

P

C1

B

C D

A

T middle key.

B will become P’s right child. No change in depth.

B will become P’s right child. No change in depth.

Page 75: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

75

P C1

T

B C DA

X

Page 76: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

76

• If both children of T are red select one of the two rotations.

• If the right child of X is red make it the new parent (it is on the inorder-successor path).

• If the left child of X is red:

Page 77: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

77

T

P

X

EC1

B

Y

C

AB

Root of C is black

Otherwise, continue

X has a red child

Root of C is black

Otherwise, continue

X has a red child

Page 78: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

78

T

P

XE

C1

Y

CA

B

Page 79: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

79

30

15 70

20 8510

5

60

6550

40

9080

55

Delete 15

Page 80: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

80

30

20

70

10

8515

5

60

6550

40 908055

Delete 15

30

15

70

10

8520

5

60

6550

40 908055

Swap (15, 20)

Page 81: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

81

30

15

70

10

8520

5

60

6550

40 908055

Delete 15

Third case: (mirror image) X (15) has two black children (Nulls)

Sibling has one red and one black child.

Page 82: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

82

30 70

10 85

205

60

6550

40 908055

Delete 15

Page 83: TCSS 342, Winter 2006 Lecture Notes

TCSS 342B v1.0

83

References

• Lewis & Chase book, Chapter 13.


Recommended