Top Banner
1 Binary Search Trees • Implementing Balancing Operations – AVL Trees – Red/Black Trees • Reading: 11.5-11.6
22

1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading: 11.5-11.6.

Dec 20, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading: 11.5-11.6.

1

Binary Search Trees

• Implementing Balancing Operations– AVL Trees– Red/Black Trees

• Reading: 11.5-11.6

Page 2: 1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading: 11.5-11.6.

2

Implementing Balancing Operations• Knowing rotations, we must know how to detect that the

tree needs to be rebalanced and which way• There are 2 ways for tree to become unbalanced

– By insertion of a node– By deletion of a node

• There are two mechanisms for detecting if a rotation is needed and which rotation to perform:– AVL Trees– Red/Black Trees

• It is best for both to have a parent reference in each child node to backtrack up the tree easily

Page 3: 1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading: 11.5-11.6.

3

Implementing Balancing Operations• AVL trees (after Adel’son-Vel’ski and Landis)

keep a balance factor attribute in each node that equals the height of the right sub-tree minus the height of the left sub-tree

• Each time an insertion or deletion occurs:– The balance factors must be updated– The balance of the tree must be checked from the point

of change up to and including the root

• If a node’s balance factor is > +1 or < -1, the sub-tree with that node as root must be rebalanced

Page 4: 1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading: 11.5-11.6.

4

• If the balance factor of a node is -2– If the balance factor of its left child is -1

• Perform a right rotation

– If the balance factor of its left child is +1• Perform a leftright rotation

• If the balance factor of a node is +2– If the balance factor of its right child is +1

• Perform a left rotation

– If the balance factor of its right child is -1• Perform a rightleft rotation

Implementing Balancing Operations

Page 5: 1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading: 11.5-11.6.

5

AVL Tree Right Rotation

7 (-1)

5 (0) 9 (0)

6 (0)3 (0)

7 (-2)

5 (-1) 9 (0)

6 (0)3 (-1)

1 (0)

Initial Tree After Add 1

Node is -2

Left Child is -1

Page 6: 1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading: 11.5-11.6.

6

AVL Tree Right Rotation

5 (0)

3 (-1) 7 (0)

6 (0)1 (0) 9 (0)

After Right Rotation

Page 7: 1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading: 11.5-11.6.

7

AVL Tree Rightleft Rotation

10 (1)

5 (-1) 15 (-1)

13 (-1)3 (0) 17 (0)

Initial Tree After Remove 3

11 (0)

10 (2)

5 (0) 15 (-1)

13 (-1) 17 (0)

11 (0)

Node is +2

Right Child is -1

Remove

Page 8: 1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading: 11.5-11.6.

8

AVL Tree Rightleft Rotation

After Right Rotation After Left Rotation

10 (2)

5 (0) 13 (1)

11 (0) 15 (1)

17 (0)

13 (0)

10 (0) 15 (1)

11 (0) 17 (0)5 (0)

Page 9: 1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading: 11.5-11.6.

9

Red/Black Trees

• Red/Black Trees (developed by Bayer and extended by Guibas and Sedgewick) keep a “color” red or black for each node in the tree– The root is black– All children of a red node are black– Every path from the root to a leaf contains the

same number of black nodes

• The maximum height of a Red/Black tree is roughly 2*log n (not as well controlled as an AVL tree), but the longest path is still O(log n)

Page 10: 1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading: 11.5-11.6.

10

Red/Black Tree Insertion

• When inserting a new node, it is put in the normal place by its value and its color is always set to red

• At the end of insertion, the root is always set to black• While current node’s parent is not the root and is red:• If the color of the parent’s sibling is red, no rotation is

needed, but re-coloring is needed from current node– Parent’s color is set to black

– Parent’s sibling’s color is set to black

– Grandparent’s color is set to red

– Current node is set to point to the grandparent for loop

Page 11: 1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading: 11.5-11.6.

Red/Black Tree Insertion

• Insertion – No Rebalancing

11

7

4 10

8

7

4 10

7

4 10

8

Initial Tree After Insertion of an 8After no rebalancing -just re-coloring the tree

Current Current

Page 12: 1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading: 11.5-11.6.

Red/Black Tree Insertion

• Insertion – No Re-balancing

12

5

7

4 10

Initial Tree After Insertion of a 5

15

20 7

4 10

15

20

5

7

4 10

15

20

After no rebalancing -just re-coloring the tree

Current

Current

Note that AVL tree logicwould have rebalanced!

Page 13: 1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading: 11.5-11.6.

Red/Black Tree Insertion

• (Note that we only continue if parent is red)

• If the color of the parent’s sibling is black (or null), the process gets more complex

• It is symmetric based on current’s parent’s sibling being the grandparent’s left or right child and current being its parent’s left or right child

13

Page 14: 1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading: 11.5-11.6.

Red/Black Tree Insertion

• If sibling is left child– If current is also left child, set current to parent

and rotate current right around parent– Recolor and rotate left around grandparent

• If sibling is right child– If current is also right child, set current to parent

and rotate current right around parent– Recolor and rotate right around grandparent

14

Page 15: 1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading: 11.5-11.6.

Red/Black Tree Insertion

• Insertion with Rebalancing (Example 1)

15

5

7

4 10

15

20

After rebalancing(Left rotation around 4)Initial Tree

After insertion of a 6and re-coloring sub-tree

5

7

4 10

15

20

6

5

7

4

10

15

20

6

Current

Current

Parent of Current is blackso that terminates the loop

Page 16: 1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading: 11.5-11.6.

Red/Black Tree Insertion

16

• Insertion with Rebalancing (Example 2)

6

7

4 10

15

20

Initial Tree After insertion of a 5

6

7

4 10

15

20

5 Current

After right rotationand re-coloring sub-tree

6

7

4 10

15

20

5

Page 17: 1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading: 11.5-11.6.

Red/Black Tree Insertion

• Insertion with Rebalancing (Example 2)

17

After left rotation around 4Move current to grandparent

5

7

4

10

15

20

6

Current

Parent of Current is blackso that terminates the loop

Page 18: 1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading: 11.5-11.6.

Red/Black Tree Removal

• Start loop with current equal to found node

• Terminate loop when current is equal to root or color of current is red– If current’s sibling is red, rotate the sibling right

around current’s parent– If both of sibling’s children are black, recolor– Else if sibling’s left child is black, rotate right

child left around sibling– Rotate sibling right around current’s parent

18

Page 19: 1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading: 11.5-11.6.

Red/Black Tree Removal

• Removal with Rotations

19

5

7

4 10

15

20

Initial Tree

12

Element to be removed

Siblingis Red

5

7

4 10

15

20

Intermediate Step 1Recolor for right rotation

12

Current

Sibling

Corresponds to upper right of Figure 11.19

Page 20: 1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading: 11.5-11.6.

Red/Black Tree Removal

• Removal with Rotations

20

5

7

4

10

15

20

Intermediate Step 2After right rotation

12 CurrentSibling

5

7

4

10

15

20

Intermediate Step 3Sibling’s left child is null/Black

but both children were not black

12 CurrentSibling

Corresponds to lower left of Figure 11.19

Page 21: 1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading: 11.5-11.6.

Red/Black Tree Removal

• Removal with Rotations

21

Intermediate Step 5Recolor for right rotation

5

7

4

10

15

20

Intermediate Step 4After left rotation around sibling

12

CurrentSibling

5

7

4

10

15

2012

CurrentSibling

Page 22: 1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading: 11.5-11.6.

Red/Black Tree Removal

• Removal with Rotations

22

5

7

4

10 15

Remove 20 to get to Final Tree

12

Current

5

7

4

10 15

Intermediate Step 6After right rotation

Set current to root to end loop

12

20