s12 AVL trees - York University · AVL Trees (11.2, 11.3) EECS 2011 25 February 2020 2 AVL Trees...

Preview:

Citation preview

1

1

AVL Trees (11.2, 11.3)

EECS 2011

25 February 2020

2

AVL Trees •  AVL trees are

balanced.

•  An AVL Tree is a binary search tree such that for every internal node v of T, the heights of the children of v can differ by at most 1.

88

44

17 78

32 50

48 62

2

4

1

1

2

3

1

1

An example of an AVL tree where the heights are shown next to the nodes

2

3

Height of an AVL Tree •  Proposition: The height of an AVL tree T storing n

keys is O(log n).

Proof: •  Find n(h): the minimum number of internal nodes of

an AVL tree of height h •  We see that n(1) = 1 and n(2) = 2 •  For h ≥ 3, an AVL tree of height h contains the root

node, one AVL subtree of height h-1 and the other AVL subtree of height h-2.

•  i.e. n(h) = 1 + n(h-1) + n(h-2)

4

Height of an AVL Tree (2)

•  Knowing n(h-1) > n(h-2), we get n(h) > 2n(h-2) n(h) > 2n(h-2) n(h) > 4n(h-4) … n(h) > 2in(h-2i)

•  Solving the base case we get: n(h) ≥ 2 h/2-1

•  Taking logarithms: h < 2log n(h) +2

•  Thus the height of an AVL tree is O(log n)

3

5

Insertion in an AVL Tree •  Insertion is as in a binary search tree. •  Always done by expanding an external node.

44

17 78

32 50 88

48 62

54 w

b=x

a=y

c=z

44

17 78

32 50 88

48 62

before insertion after insertion

6

Rebalancing •  A binary search tree T is called balanced if for every node

v, the height of v’s children differ by at most 1.

•  Inserting a node into an AVL tree involves performing expandExternal(w, (k, e)) on T, which changes the heights of some of the nodes in T.

•  The insertion may cause the tree to become unbalanced,

•  We start from the newly created node w and travel up the tree until we find the first node z that is unbalanced.

4

Rebalancing (2) •  y = child of z with higher

height Note: y = ancestor of w •  x = child of y with higher

height Note: x = ancestor of w or x = w

•  Since z became unbalanced by an insertion in the subtree rooted at its child y, height(y) = height(sibling(y)) + 2

7

Restructuring •  Now to rebalance... •  To rebalance the subtree rooted at z, we must

perform a restructuring.

•  Two methods: 1. cut/link restructuring (not in textbook)

Given 7 integer keys 1, 2, 3, 4, 5, 6, and 7, how do we build a balanced BST?

2. tri-node restructuring (textbook)

8

5

9

Cut/Link Restructure Algorithm •  Any tree that needs to be balanced can be grouped into 7

parts: x, y, z, and the 4 subtrees anchored at the children of those nodes (T0, T1, T2, T3).

•  Any of the 4 subtrees can be empty (one dummy leaf).

88

44

17

7850

48

62

54T0

T1

T2

T3

y

x

z

10

Cut/Link Restructure Algorithm (2) •  Number the 7 parts by doing an inorder traversal. •  x,y, and z are now renamed based upon their order within

the inorder traversal. •  x, y, z: even numbers •  T0, T1, T2, T3: odd numbers

88

44

17

7850

48

62

54T0

T1

T2

T3

z (a)

y (b)

x (c)

1 2

34

56

7

6

11

Cut/Link Restructure Algorithm (3)

62

b4

•  Now we can re-link these subtrees to the main tree. •  Link in node 4 (b) where the subtree’s root was.

12

Cut/Link Restructure Algorithm (4)

•  Link in nodes 2 (a) and 6 (c) as children of node 4.

62

b4

44 78

a c2 6

7

13

Cut/Link Restructure Algorithm (5) •  Finally, link in subtrees 1 and 3 as the children of node 2,

and subtrees 5 and 7 as the children of 6.

62

y4

44 78

z x

17

T0

2 6

50

48 54

T1

3 588

T3

7T2

1

Possible Configurations

14

8

15

Tri-node Restructuring •  To rebalance the subtree rooted at z, we must perform

a restructuring. •  Let (a, b, c) be an inorder listing of x, y, z. •  Perform the rotations needed to make b the topmost

node of the three (to become parent of a and c). •  4 subtrees (formerly children of x, y, z) become children

of a, b, c so as to maintain BST properties. •  Connect b to the rest of the tree (to z’s former parent). •  There are 4 cases:

–  2 cases of single rotations –  2 cases of double rotations.

Single Rotations

16

9

Double Rotations

17

18

Restructuring Example

88

44

17 78

32 50

48 62

2

5

1

1

3

4

2

1

54

1

T0 T2

T3

x

y

z

2

3

4

5

67

1

88

44

17

7832 50

48

622

4

1

1

2 2

3

1541

T0 T1

T2

T3

x

y z

unbalanced...

...balanced

12

3

4

5

6

7

10

19

Restructure Algorithm After an insertion, we start from the newly created node w and travel up

the tree until we find the first node z that is unbalanced. Algorithm restructure( x ):

Input: A node x of a binary search tree T that has both a parent y and a grandparent z Output: Tree T restructured by a rotation involving nodes x, y, and z.

1. Let (a, b, c) be an inorder listing of the nodes x, y, and z, and let (T1,

T2, T3, T4) be an inorder listing of the the four subtrees of x, y, and z, not rooted at x, y, or z.

2. Replace the subtree rooted at z with a new subtree rooted at b 3. Let a be the left child of b and let T1 and T2 be the left and right

subtrees of a, respectively. 4. Let c be the right child of b and let T3 and T4 be the left and right

subtrees of c, respectively.

20

Cut/Link vs. Tri-node Restructuring

•  Both methods give the same results. •  Both methods have the same time complexity. •  Time complexity = ? •  Cut/link method:

–  Advantage: no case analysis; more elegant. –  Disadvantage: can be more code to write.

11

AVL Trees 21

Removal •  Removal begins as in a binary search tree (3 cases). •  The tree may become unbalanced.

44

17

78 32 50

88 48

62

54

44

17

78 50

88 48

62

54

before deletion of 32 after deletion

Removal: Rebalancing •  Let z be the first

unbalanced node encountered while travelling up the tree from node w.

•  y = child of z with higher height (y cannot be ancestor of w)

•  x = child of y with higher height, or either child if two children of y have the same height.

22

12

Removal: Restructuring •  Perform operation

restructure( x ) to restore balance at the subtree rooted at z.

•  As this restructuring may upset the balance of another node higher in the tree, we must continue checking for balance until the root of T is reached.

23

24

Removal Example

88

44

17

78

32

50

48

621

4

1

2 2

3

154

1T0

T1

T2

T3

y

x

0

Oh no, unbalanced!

8817

78

50

48

62

1

1

2

23

1

541

T0

T2

T3

y

x44

4

z

0 Whew, balanced!

Choose either 78 or 50 as node x.

13

25

Removal Example (2)

Whew, balanced!

88

17 78

50

48

621 1

4

2

3

154

1

T0 T1 T2

y

x

0

442z

88

44

17

78

32

50

48

621

4

1

2 2

3

1541

T0

T1 T2 T3

z

y

x

0

Oh no, unbalanced!

Choose 50 as x.

AVL Trees 26

AVL Tree Performance •  AVL tree storing n items

–  The data structure uses O(n) space

–  A single restructuring takes O(1) time •  using a linked-structure binary tree

–  Searching takes O(log n) time •  height of tree is O(log n), no restructures needed

–  Insertion takes O(log n) time •  initial find is O(log n) •  restructuring up the tree, maintaining heights is O(log n)

–  Removal takes O(log n) time •  initial find is O(log n) •  restructuring up the tree, maintaining heights is O(log n)

14

Next lecture …

•  Heaps (9.3)

27

Recommended