Balanced Trees (AVL and RedBlack)

66
Balanced Trees (AVL and RedBlack)

description

Balanced Trees (AVL and RedBlack). Binary Search Trees. Optimal Behavior O(log 2 N) – perfectly balanced tree (e.g. complete tree with all levels filled) Degenerate Behavior O(N) – tree becomes list How to avoid degenerate trees? Keep them perfectly balanced Impossible - PowerPoint PPT Presentation

Transcript of Balanced Trees (AVL and RedBlack)

Page 1: Balanced Trees     (AVL and RedBlack)

Balanced Trees (AVL and RedBlack)

Page 2: Balanced Trees     (AVL and RedBlack)

Binary Search Trees

•Optimal Behavior▫O(log2N) – perfectly balanced tree (e.g. complete tree with all

levels filled)•Degenerate Behavior

▫O(N) – tree becomes list•How to avoid degenerate trees?

▫Keep them perfectly balanced Impossible

▫Keep them balanced within some criteria Maintain balance over insertions and deletions

Page 3: Balanced Trees     (AVL and RedBlack)

Balanced Trees

•Balanced binary trees▫AVL Tree▫Red-Black Tree

•Balanced N-ary trees▫B-Trees▫B+-Trees

Page 4: Balanced Trees     (AVL and RedBlack)

Height of BST

•A BST is not balanced•E.g.,

5

12

23

35

85

65

Page 5: Balanced Trees     (AVL and RedBlack)

AVL TreesDiscovered by two Russian mathematicians, Adelson-Velskii and Landis, in 1962.

Mathematical Definition:If T is a a nonempty binary search tree, then T is an AVL tree if and only if its left and right subtrees are AVL trees and|left height – right height| < 1.

“In English” Definition:A binary search tree that is somewhat balanced, but not necessarily complete or full.

Page 6: Balanced Trees     (AVL and RedBlack)

AVL Trees?45

10

20 70

30

50

60

10

3

7 15

9

11

13 30

Page 7: Balanced Trees     (AVL and RedBlack)

AVL Trees

Representation:Use the linked representation, with an addition to the node class. The addition is a balance factor (bf), which is defined as the following for a node x.

bf(x) = height of x’s left subtree – height of x’s right subtree

The possible balance factors are –1, 0, and 1.

Page 8: Balanced Trees     (AVL and RedBlack)

AVL Trees

Insertion:Using the standard insertion logic for a binary search tree could cause problems with an AVL tree because it’s possible that some of the properties could be violated.

In other words, it’s possible that the tree could become unbalanced.

Page 9: Balanced Trees     (AVL and RedBlack)

AVL TreesJFK

GCM

DCA ORD

+1

0

-1 0

Adding the node “DUS”

results in the tree below:

JFK

GCM

DCA ORD

+2

+1

-2 0

DUS 0

Page 10: Balanced Trees     (AVL and RedBlack)

AVL Trees

•When an insertion is performed, the balancing information for ALL of the nodes on the path back to the root must be updated.

•If the AVL properties were destroyed, they can be fixed by a simple modification to the tree, which is known as a rotation.

Page 11: Balanced Trees     (AVL and RedBlack)

AVL Trees

To fix the tree, start at the inserted node and trace its path back to the root, stopping at the first node that violates the AVL property (call this node A).

There are four possible violations that could occur:1. Inserted into the left subtree of A’s left child2. Inserted into the right subtree of A’s right child3. Inserted into the right subtree of A’s left child4. Inserted into the left subtree of A’s right child

Page 12: Balanced Trees     (AVL and RedBlack)

AVL Trees1. Inserted into the left subtree of A’s left child (lets call this B).

This problem is fixed by applying a single right rotation.- Change the parent of A to point to B- Make A’s left link equal to B’s right link- Make B’s right link point to A

10

3

7

A

B10 A3

7B

Page 13: Balanced Trees     (AVL and RedBlack)

AVL Trees2. Inserted into the right subtree of A’s right child (lets call this B).

This problem is fixed by applying a single left rotation.- Change the parent of A to point to B- Make A’s right link equal to B’s left link- Make B’s left link point to A

45

65

55

A

B65A 45

55B

Page 14: Balanced Trees     (AVL and RedBlack)

AVL TreesLet’s build a tree using the nodes 3, 2, 1, 4, 5, 6, 7.

3

2

1

3

2

1 3

2

1

4

Page 15: Balanced Trees     (AVL and RedBlack)

AVL TreesLet’s build a tree using the nodes 3, 2, 1, 4, 5, 6, 7.

3

2

1

4

5

4

2

1

53

Page 16: Balanced Trees     (AVL and RedBlack)

AVL TreesLet’s build a tree using the nodes 3, 2, 1, 4, 5, 6, 7.

4

2

1

53

6

2

1

4

53

6

After step 1 of a

single left rotation

Page 17: Balanced Trees     (AVL and RedBlack)

AVL TreesLet’s build a tree using the nodes 3, 2, 1, 4, 5, 6, 7.

2

1

4

53

6

2

1 3

4

5

6

After step 2 of a

single left rotation

Page 18: Balanced Trees     (AVL and RedBlack)

AVL TreesLet’s build a tree using the nodes 3, 2, 1, 4, 5, 6, 7.

After step 3 of a

single left rotation2

1 3

4

5

6

2

1 3

4

5

6

Page 19: Balanced Trees     (AVL and RedBlack)

AVL TreesLet’s build a tree using the nodes 3, 2, 1, 4, 5, 6, 7.

2

1 3

4

5

6

7

2

1 3

4

6

5 7

Page 20: Balanced Trees     (AVL and RedBlack)

AVL Trees3. Inserted into the right subtree of A’s left child (lets call this B).

This problem is fixed by applying a left-right rotation.- Make A’s left link equal to B’s right link (call this C)- Make B’s right link equal to C’s left link- Make C’s left link equal to B- Change the parent of A to point to C- Make A’s left link equal to C’s right link- Make C’s right link point to A

15A

B 7

12C

15A

B

12

7

C 15 AB

12

7

C

Page 21: Balanced Trees     (AVL and RedBlack)

AVL Trees4. Inserted into the left subtree of A’s right child (lets call this B).

This problem is fixed by applying a right-left rotation.- Make A’s right link equal to B’s left link (call this C)- Make B’s left link equal to C’s right link- Make C’s right link equal to B- Change the parent of A to point to C- Make A’s right link equal to C’s left link- Make C’s left link point to A

15A

B 32

21C

15A

B

21

32

C 32 BA

21

15

C

Page 22: Balanced Trees     (AVL and RedBlack)

AVL TreesLet’s build onto the previous example by adding the nodes:17, 16, 15, 13, 14.

2

1 3

4

6

5 7

17

16

2

1 3

4

6

5 7

1716

Right-left rotation

Page 23: Balanced Trees     (AVL and RedBlack)

AVL TreesLet’s build onto the previous example by adding the nodes:17, 16, 15, 13, 14.

Right-left rotation2

1 3

4

6

5 7

1716

2

1 3

4

6

5 7

17

16

Page 24: Balanced Trees     (AVL and RedBlack)

AVL TreesLet’s build onto the previous example by adding the nodes:17, 16, 15, 13, 14.

Right-left rotation 2

1 3

4

6

5

717

16

2

1 3

4

6

5 7

17

16

Page 25: Balanced Trees     (AVL and RedBlack)

AVL TreesLet’s build onto the previous example by adding the nodes:17, 16, 15, 13, 14.

Right-left rotation2

1 3

4

6

5

717

16

2

1 3

4

6

5

7 17

16

Page 26: Balanced Trees     (AVL and RedBlack)

AVL TreesLet’s build onto the previous example by adding the nodes:17, 16, 15, 13, 14.

Right-left rotation2

1 3

4

6

5

7 17

16

15

2

1 3

4

6

5 7

16

1715

Page 27: Balanced Trees     (AVL and RedBlack)

AVL TreesLet’s build onto the previous example by adding the nodes:17, 16, 15, 13, 14.

Right-left rotation 2

1 3

4

6

5

7

16

1715

2

1 3

4

6

5 7

16

1715

Page 28: Balanced Trees     (AVL and RedBlack)

AVL TreesLet’s build onto the previous example by adding the nodes:17, 16, 15, 13, 14.

single left rotation

2

1 3

4

6

5

7

16

1715

13

2

1 3

4

6

5

7

16

1715

13

Page 29: Balanced Trees     (AVL and RedBlack)

AVL TreesLet’s build onto the previous example by adding the nodes:17, 16, 15, 13, 14.

Left - right rotation

2

1 3

4

6

5

7

16

1714

13

14

2

1 3

4

6

5

7

16

1715

13

15

Page 30: Balanced Trees     (AVL and RedBlack)

Red-Black Trees

•Similar idea to AVL Trees•Similar worse case time complexity•Keep tree somewhat balanced

▫not as rigidly balanced as AVL▫A Red-Black tree with n nodes has height at most 2log(n+1),

i.e., most of the common operations are performed in O(log n).

▫Longest path is no more than twice as long as the shortest branch

Page 31: Balanced Trees     (AVL and RedBlack)

Red-Black Trees•A red-black tree is an extended binary tree (every node has

two children or is a leaf) which satisfies the following properties: ▫The root is black (if non-empty)

Root property▫ If a node is red, it’s parent must be black

Red property▫Every path from the root to a leaf/single-child node contains the

same number of black nodes. This number is called the black-height of the tree. Path property

Page 32: Balanced Trees     (AVL and RedBlack)

Red-Black?

Page 33: Balanced Trees     (AVL and RedBlack)

Red-Black?

no yesyes

Page 34: Balanced Trees     (AVL and RedBlack)

Red-Black?

Page 35: Balanced Trees     (AVL and RedBlack)

Red-Black?

no

Page 36: Balanced Trees     (AVL and RedBlack)

Properties Revisited▫The root is black (if non-empty)

Root property▫ If a node is red, it’s parent must be black

Red property▫Every path from the root to a leaf/single-child node contains the

same number of black nodes. This number is called the black-height of the tree. Path property

Page 37: Balanced Trees     (AVL and RedBlack)

Adding null leaf nodes•Every node has 2 children

▫Don’t include null nodes in the black-height of the tree

Page 38: Balanced Trees     (AVL and RedBlack)

Properties Revisited

•Objective:▫Restore red property violation while preserving other

properties (i.e. path property).▫Techniques: re-color and/or rotate.

Page 39: Balanced Trees     (AVL and RedBlack)

Property Restoration

• Invariant:▫There is exactly one red node x in the tree whose parent may be

red. When a new node is added, it is always added as a red node

•Strategy:▫Fix the violation of red property at x.▫This may violate the condition at some ancestor.▫Continue fixing the property at the ancestor (ancestor becomes x)

Page 40: Balanced Trees     (AVL and RedBlack)

Property Restoration•Terminology:

▫x is the current node in violation of the red property It is red, and its parent is also red

▫parent(x) is its parent▫parent(parent(x) ) is its grandparent (i.e. grandparent(x))▫The other sibling of parent(x) is its uncle (i.e. uncle(x))

Page 41: Balanced Trees     (AVL and RedBlack)

Property Restoration• There are several cases:

▫ Case 1: X is the root

▫ Case 2: Parent(x) is black

▫ Case 3: Parent(x) and Uncle(x) are both red

▫ Case 4: Parent(x) is red, but Uncle(x) is black

Multiple parts of this case…

Page 42: Balanced Trees     (AVL and RedBlack)

Property RestorationCase 1:

X is the root

Change its color to black.

x x

Page 43: Balanced Trees     (AVL and RedBlack)

Property RestorationCase 2: Parent(x) is black

Don’t do anything

p

x

p

x

Page 44: Balanced Trees     (AVL and RedBlack)

Property RestorationCase 3: Parent(x) and Uncle(x) are both red

Change the color of the parent and its uncle to black. Change the color of the grandparent node to red, and repeat with x = grandparent(x)

g

up

x

g

up

x

Page 45: Balanced Trees     (AVL and RedBlack)

Property RestorationCase 4: Parent(x) is red, but Uncle(x) is black

There are 4 different cases, much like the 4 different AVL cases

left-left left-right right-left right-right

g

up

x

g

up

x

g

pu

x

g

pu

x

Page 46: Balanced Trees     (AVL and RedBlack)

Property RestorationCase 4 cont:

left-left and right-right are symmetricleft-right and right-left are symmetric

left-left left-right right-left right-right

g

up

x

g

up

x

g

pu

x

g

pu

x

Page 47: Balanced Trees     (AVL and RedBlack)

Property RestorationCase 4 left-left:

First rotate the parent RIGHT Then recolor by switching colors of P and GCase 4 right-right is symmetric – simply rotate LEFT instead

g

up

3x

p

gx

u1

1 2

4 5 2 3

4 5

p

gx

u1 2 3

4 5

Page 48: Balanced Trees     (AVL and RedBlack)

Property RestorationCase 4 left-right:

First rotate x LEFT (shown below) then rotate the parent RIGHT and recolor by switching colors of P and GCase 4 right-left is symmetric – simply rotate RIGHT then LEFT

g

up

1 x

g

ux

p

1 2

4 5 3 4 5

Page 49: Balanced Trees     (AVL and RedBlack)

Property RestorationCase 4 left-right:

First rotate x LEFT (shown previously)then rotate the parent RIGHT and recolor by switching colors of X and G (just like left-left case but where p and x switch rolls)

Case 4 right-left is symmetric – simply rotate RIGHT then LEFT

g

ux

3p

x

gp

u1

1 2

4 5 2 3

4 5

x

gp

u1 2 3

4 5

Page 50: Balanced Trees     (AVL and RedBlack)

Insertion Complexity•Observations:

▫Every one of the three cases take constant time.▫Case 3 brings x two steps closer to the root and only re-colors

nodes without any rotations.▫For the multiple cases in 4, one or two rotations plus a re-coloring

are required and we are done.

•Lemma:▫An insertion into a red-black tree with n nodes takes O(log2 n) time

and performs at most two rotations.

Page 51: Balanced Trees     (AVL and RedBlack)

Insertion Example•Snap-shots taken from:

http://www.ececs.uc.edu/~franco/C321/html/RedBlack/redblack.html Note their rules are numbered differently than our

▫ Insert 50, 30, 70 (recall new nodes always insert as red): Rule 1 for 50, Rule 2 for 30 and 70

▫Null nodes (all black are not shown)

Page 52: Balanced Trees     (AVL and RedBlack)

Insertion Example•Consider the following actions;

▫ Insert 50, 30, 70, 65

▫Red property violation re-color (Case 3: Parent & Uncle are red) Re-color parent and uncle black while re-coloring grandparent red

Page 53: Balanced Trees     (AVL and RedBlack)

Insertion Example▫Continue with grandparent as X

▫ Root must be black! (Rule 1) color it black

Page 54: Balanced Trees     (AVL and RedBlack)

Insertion Example•Consider the following actions;

▫ Insert 50, 30, 70, 65, 80

▫No violations (rule 2)

Page 55: Balanced Trees     (AVL and RedBlack)

Insertion Example•Consider the following actions;

▫ Insert 50, 30, 70, 65, 80, 75

▫Case 3: Uncle & parent are both red re-color them black and the grandparent red

Page 56: Balanced Trees     (AVL and RedBlack)

Insertion Example•Consider the following actions;

▫ Insert 50, 30, 70, 65, 80, 75

▫Continue on recursively with grandparent being x▫Case 2: no violations

Page 57: Balanced Trees     (AVL and RedBlack)

Insertion Example•Consider the following actions;

▫ Insert 50, 30, 70, 65, 80, 75, 68, 67

▫Case 4: Parent & uncle: diff. color: right-left▫Balance: first rotation 67 right, then left

Page 58: Balanced Trees     (AVL and RedBlack)

Insertion Example•Consider the following actions;

▫ Insert 50, 30, 70, 65, 80, 75, 68, 67

▫Flip colors of G (65) and X (67)

Page 59: Balanced Trees     (AVL and RedBlack)

Insertion Example•Consider the following actions;

▫ Insert 50, 30, 70, 65, 80, 75, 68, 67

Page 60: Balanced Trees     (AVL and RedBlack)

Insertion Example•Consider the following actions;

▫ Insert 50, 30, 70, 65, 80, 75, 68, 67, 55

▫Case 3: parent & uncle same color re-color them and grandparent

Page 61: Balanced Trees     (AVL and RedBlack)

Insertion Example•Consider the following actions;

▫ Insert 50, 30, 70, 65, 80, 75, 68, 67, 55

▫Recursively continue with 67 being the new x▫Case 4: 70 and 30 are diff colors: right-left

First rotate 67 right

Page 62: Balanced Trees     (AVL and RedBlack)

Insertion Example•Consider the following actions;

▫ Insert 50, 30, 70, 65, 80, 75, 68, 67, 55

▫Then rotate 67 left

Page 63: Balanced Trees     (AVL and RedBlack)

Insertion Example•Consider the following actions;

▫ Insert 50, 30, 70, 65, 80, 75, 68, 67, 55

▫Then flip colors of 67 and 50

Page 64: Balanced Trees     (AVL and RedBlack)

Insertion Example•Consider the following actions;

▫ Insert 50, 30, 70, 65, 80, 75, 68, 67, 55

Done!

Page 65: Balanced Trees     (AVL and RedBlack)

Benefits/Usage of Red-Black Trees•Properties

▫O(log2N) and Θ(log2N) for search, insert, remove operations•Usage

▫Underlying implementation structure for hash maps, hash sets, multimaps, multisets in Standard Template Library

▫New scheduler for Linux

Page 66: Balanced Trees     (AVL and RedBlack)

Comparison of Red-Black and AVL•Both are Binary Search Trees•Both have the same worst case big-O time complexity for:

▫Search▫ Insert▫Delete

•AVL trees are more rigidly balanced▫AVL trees are slightly faster for lookup▫AVL trees are more costly for insert and delete

•Most people are using Red-Black trees in their implementations