Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.

38
Balanced Binary Search Tree 황황황 Fall 2010 CSE, POSTECH

Transcript of Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.

Page 1: Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.

Balanced Binary Search Tree

황승원Fall 2010

CSE, POSTECH

Page 2: Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.

2

Balanced Binary Search Trees

height is O(log n), where n is the number of elements in the tree

AVL (Adelson-Velsky and Landis) trees red-black trees get, put, and remove take O(log n) time

Page 3: Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.

3

Balanced Binary Search Trees

Indexed AVL trees Indexed red-black trees Indexed operations also take

O(log n) time

Page 4: Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.

4

Balanced Search Trees

weight balanced binary search trees 2-3 & 2-3-4 trees AA trees B-trees BBST etc.

Page 5: Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.

55

AVL Tree

Definition Binary tree. If T is a nonempty binary tree with TL and TR as it

s left and right subtrees, then T is an AVL tree iff1. TL and TR are AVL trees, and

2. |hL – hR| 1 where hL and hR are the heights of TL and TR, respectively

Page 6: Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.

6

Balance Factors

0 0

0 0

1

0

-1 0

1

0

-1

1

-1

This is an AVL tree.

Page 7: Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.

7

Height

The height of an AVL tree that has n nodes is at most 1.44 log2 (n+2).

Nh=Nh-1 + Nh-2+1 – reminds you anything?Fn=Fn-1 + Fn-2

Nh=Fh+2-1 when ~ / 5

(1 5) / 2

log ( 5( 1)) 2

hhF

h n

Page 8: Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.

8

AVL Search Tree

0 0

0 0

1

0

-1 0

1

0

-1

1

-110

7

83

1 5

30

40

20

25

35

45

60

Page 9: Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.

9

put(9)

0 0

0 0

1

0

-1 0

1

0

-1

1

-1

90

-1

0

10

7

83

1 5

30

40

20

25

35

45

60

Page 10: Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.

10

put(29)

0 0

0 0

1

0

0

1

0

-1

1

-110

7

83

1 5

30

40

20

25

35

45

60

29

0

-1

-1-2

RR imbalance => new node is in right subtree of right subtree of blue node (node with bf = -2)

Page 11: Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.

11

put(29)

0 0

0 0

1

0

0

1

0

-1

1

-110

7

83

1 5

30

40

2535

45

600

RR rotation. 20

029

Page 12: Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.

12

AVL Rotations

RR LL RL LR

Page 13: Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.

1313

Imbalance Types

After an insertion, when the balance factor of node A is –2 or 2, the node A is one of the following four imbalance types

1. LL: new node is in the left subtree of the left subtree of A

2. LR: new node is in the right subtree of the left subtree of A

3. RR: new node is in the right subtree of the right subtree of A

4. RL: new node is in the left subtree of the right subtree of A

Page 14: Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.

1414

Rotation

Definition To switch children and parents among two or three

adjacent nodes to restore balance of a tree. A rotation may change the depth of some nodes,

but does not change their relative ordering.

Page 15: Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.

1515

AVL Rotations

To balance an unbalanced AVL tree (after an insertion), we may need to perform one of the following rotations: LL, RR, LR, RL

Page 16: Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.

1616

An LL Rotation

Page 17: Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.

1717

An LR Rotation

Page 18: Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.

1818

RR and RL?

Symmetric!

Page 19: Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.

1919

Inserting into an AVL Search Tree

29

Insert(29)-1

1

0

0 0

0

1

1

-1 0

-1

0

0

10

40

30 45

20 35

25

60

7

3 8

1 5

• Where is 29 going to be inserted into?• After the insertion, is the tree still an AVL search tree? (i.e., still balanced?)

Page 20: Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.

2020

Inserting into an AVL Search Tree

• What are the balance factors for 20, 25, 29?

-2

-1

0

Insert(29)

29

-1

1

0

0 0

0

1

1

0

-1

0

10

40

30 45

20 35

25

60

7

3 8

1 5

• RR imbalance new node is in the right subtree of right subtree of node 20 (node with bf = -2)

Page 21: Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.

2121

After RR Rotation

-1

1

0

0 0

0

1

1

0

-1

0

10

40

30 45

35 60

7

3 8

1 5 0

00

25

20 29

• What would the left subtree of 30 look like after an RR rotation?• After the RR rotation, is the resulting tree an AVL search tree now?

Page 22: Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.

2222

Deletion from an AVL Search Tree

Deletion of a node may also produce an imbalance

Imbalance incurred by deletion is classified intothe types R0, R1, R-1, L0, L1, and L-1

Rotation is also needed for rebalancing Read the observations after deleting a node from

an AVL search tree Read Section 11.2.6 for Deletion from an AVL

search tree

Page 23: Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.

2323

An R0 Rotation

Page 24: Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.

2424

An R1 Rotation

Page 25: Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.

2525

An R-1 Rotation

Page 26: Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.

2626

Complexity of Dictionary Operations:Search, Insert, and Delete

Data Structure Worst Case Expected

Hash Table

Binary SearchTree

BalancedBinary SearchTree

O(n)

O(n)

O(log n)

O(1)

O(log n)

O(log n)

• n is the number of elements in dictionary.

Page 27: Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.

2727

Complexity of Other OperationsAscend, Search(rank), and Delete(rank)

Data Structure ascend get and remove

Hash Table

Indexed BST

IndexedBalanced BST

O(D + n log n)

O(n)

O(n)

O(D + n log n)

O(n)

O(log n)

• D is the number of buckets.

Page 28: Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.

28

Red Black Trees

Colored Nodes Definition Binary search tree. Each node is colored red or black. Root and all external nodes are black. No root-to-external-node path has two consecutive

red nodes. All root-to-external-node paths have the same

number of black nodes

Page 29: Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.

29

Example Red Black Tree

10

7

8

1 5

30

40

20

25

35

45

60

3

Page 30: Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.

30

Properties

The height of a red black tree that has n (internal) nodes is between log2(n+1) and 2log2(n+1).

Page 31: Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.

31

Lemma 16.2

r : rank of root

2

2

2 1

2log ( 1)

r

h r

n

h n

Page 32: Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.

3232

Insert

New pair is placed in a new node, which is inserted into the red-black tree.

New node color options.– Black node => one root-to-external-node path has an

extra black node (black pointer). Hard to remedy.

– Red node => one root-to-external-node path may have two consecutive red nodes (pointers).

May be remedied by color flips and/or a rotation.

Page 33: Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.

3333

Classification Of 2 Red Nodes/Pointers

XYz– X => relationship between gp and pp.

pp left child of gp => X = L.– Y => relationship between pp and p.

p right child of pp => Y = R.– z = b (black) if d = null or a black node.– z = r (red) if d is a red node.

a b

cd

gppp

p

Page 34: Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.

3434

XYr Color flip.

a b

cd

gppp

p

a b

cd

gppp

p

• Continue rebalancing if changing gp to red causes problems

Page 35: Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.

3535

LLb Rotate.

• Done!

• Same as LL rotation of AVL tree.

y

x

a b

z

c d

a b

cd

gppp

p x

y

z

Page 36: Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.

3636

LRb Rotate.

• Done!

• Same as LR rotation of AVL tree.

• RRb and RLb are symmetric.

y

x

a b

z

c d

b c

ad

gppp

py

x

z

Page 37: Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.

3737

Red-black Tree Animation

http://www.ece.uc.edu/~franco/C321/html/RedBlack/redblack.html

java.util.TreeMap => red black tree

Page 38: Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.

3838

Coming Up Next

READING: Ch 16.1~2 NEXT: Graphs (Ch 17.1~7)