14
1 Balanced Trees There are several ways to define balance Examples: – Force the subtrees of each node to have almost equal heights – Place upper and lower bounds on the heights of the subtrees of each node. – Force the subtrees of each node to have similar sizes (=number of nodes)

1 Balanced Trees There are several ways to define balance Examples: –Force the subtrees of each node to have almost equal heights –Place upper and lower

Embed Size (px)

Citation preview

Page 1: 1 Balanced Trees There are several ways to define balance Examples: –Force the subtrees of each node to have almost equal heights –Place upper and lower

1

Balanced Trees

There are several ways to define balance Examples:

– Force the subtrees of each node to have almost equal heights

– Place upper and lower bounds on the heights of the subtrees of each node.

– Force the subtrees of each node to have similar sizes (=number of nodes)

Page 2: 1 Balanced Trees There are several ways to define balance Examples: –Force the subtrees of each node to have almost equal heights –Place upper and lower

2

AVL Trees

AVL tree = – A binary search tree– with the property:

• for every node, the heights of the left and right subtrees differ at most by one.

Implementation issues:– Each node contains a value (-1, 1, 0) indicating which

subtree is "heavier"– Insert and Delete are modified. They restructure the

tree to make it balanced (if necessary).

Page 3: 1 Balanced Trees There are several ways to define balance Examples: –Force the subtrees of each node to have almost equal heights –Place upper and lower

3

AVL trees1

1-1

0 0 1

0

nodes are marked with balance value

Page 4: 1 Balanced Trees There are several ways to define balance Examples: –Force the subtrees of each node to have almost equal heights –Place upper and lower

4

AVL trees: Fixing imbalances

An imbalance is detected when the height difference between two subtrees of a node becomes greater than 1 or smaller than -1.

There are two types of imbalances:

2

-1

0

-2

1

0

2

1

0

-2

-1

0

oror

TYPE 1 TYPE 2

Page 5: 1 Balanced Trees There are several ways to define balance Examples: –Force the subtrees of each node to have almost equal heights –Place upper and lower

5

AVL trees: Fixing imbalances

Fixing an imbalance is done by rotating the tree.

There are two types of rotation:– single rotation

• for TYPE 1 imbalances

– double rotation• for TYPE 2 imbalances• consists of two single rotations.

The rotations must always preserve the BST property.

Page 6: 1 Balanced Trees There are several ways to define balance Examples: –Force the subtrees of each node to have almost equal heights –Place upper and lower

6

AVL Trees: single rotation

6

4

2 6

4

2

node with imbalance

A B

C

D

A B C D

This is a single right rotation. A single left rotation is symmetric.

right rotateat node 6

Page 7: 1 Balanced Trees There are several ways to define balance Examples: –Force the subtrees of each node to have almost equal heights –Place upper and lower

7

AVL Trees: Double rotation

6

2

4

6

4

2

node with imbalance

A

B C

D

A B C D

STEP 1: left rotate at node 2

6

4

2

A B

C

D

STEP2: right rotate at node 6

node with imbalance

Page 8: 1 Balanced Trees There are several ways to define balance Examples: –Force the subtrees of each node to have almost equal heights –Place upper and lower

8

AVL Trees: Double rotation

6

2

4

6

4

2

node with imbalance

A

B C

D

A B C D

If you want to do it in one step, imagine taking 4 and moving it up in between 2 and 6, so that 2 and 6 become its new children:

B and C will then be adopted by 2 and 6 respectively, in order to maintain the BST property.

Page 9: 1 Balanced Trees There are several ways to define balance Examples: –Force the subtrees of each node to have almost equal heights –Place upper and lower

9

AVL Trees: Insert

1. Insert the node as in a BST

2. Starting at the newly inserted node, travel up the tree (towards the root), updating the balances along the way.

a. If the tree becomes unbalanced, decide which rotation needs to be performed, rotate the tree and update the balances.

Page 10: 1 Balanced Trees There are several ways to define balance Examples: –Force the subtrees of each node to have almost equal heights –Place upper and lower

10

AVL Trees: Delete

1. Delete the node as in a BST

2. Starting at the newly inserted node, travel up the tree (towards the root), updating the balances along the way.

a. If the tree becomes unbalanced, decide which rotation needs to be performed, rotate the tree and update the balances.

b. Keep traveling towards the root, checking the balances. You may need to rotate again.

Page 11: 1 Balanced Trees There are several ways to define balance Examples: –Force the subtrees of each node to have almost equal heights –Place upper and lower

11

AVL Trees: Insert/Delete

Insert– maximum possible number of rotations = 1

Delete– maximum possible number of rotations = lgn

Worst case times– search: O(lgn)– insert: O(lgn)– delete: O(lgn)– one rotation: O(1)

Page 12: 1 Balanced Trees There are several ways to define balance Examples: –Force the subtrees of each node to have almost equal heights –Place upper and lower

12

AVL Trees: Efficiency

It can be shown that the worst case height of an AVL tree is at most 44% larger than the minimum possible for a BST (i.e. approximately 1.44lgn)

Page 13: 1 Balanced Trees There are several ways to define balance Examples: –Force the subtrees of each node to have almost equal heights –Place upper and lower

13

Other balanced trees

Red-black trees– Each node has an extra bit to hold a color – The tree stays balanced by placing restrictions on

the way the nodes are colored• Every path from the root to a leaf has the same number of

black nodes and there cannot be consecutive red nodes.

– A red-black tree is balanced when the longest path from the root to a leaf is at most twice the length of the shortest path from the root to a leaf.

– A red-black tree with n nodes has height at most 2lg(n+1)

Page 14: 1 Balanced Trees There are several ways to define balance Examples: –Force the subtrees of each node to have almost equal heights –Place upper and lower

14

Other balanced trees

α-balanced trees– α is a constant between 1/2 and 1– A tree is α-balanced if, for every node x:

size[left(x)] α·size[x]size[right(x)] α·size[x]