Inf 2B: AVL Trees Lecture 5 of ADS thread Kyriakos Kalorkoti - - PowerPoint PPT Presentation

inf 2b avl trees
SMART_READER_LITE
LIVE PREVIEW

Inf 2B: AVL Trees Lecture 5 of ADS thread Kyriakos Kalorkoti - - PowerPoint PPT Presentation

Inf 2B: AVL Trees Lecture 5 of ADS thread Kyriakos Kalorkoti School of Informatics University of Edinburgh Dictionaries A Dictionary stores keyelement pairs, called items . Several elements might have the same key. Provides three methods:


slide-1
SLIDE 1

Inf 2B: AVL Trees

Lecture 5 of ADS thread Kyriakos Kalorkoti

School of Informatics University of Edinburgh

slide-2
SLIDE 2

Dictionaries

A Dictionary stores key–element pairs, called items. Several elements might have the same key. Provides three methods:

◮ findElement(k): If the dictionary contains an item with

key k, then return its element; otherwise return the special element NO_SUCH_KEY.

◮ insertItem(k, e): Insert an item with key k and element e. ◮ removeItem(k): If the dictionary contains an item with key

k, then delete it and return its element; otherwise return NO_SUCH_KEY. Assumption: we have a total order on keys (always the case in applications). Note: We are concerned entirely with fast access and storage so focus on keys.

slide-3
SLIDE 3

ADT Dictionary & its implementations

List implementation: Θ(1) time for InsertItem(k, e) but Θ(n) for findElement(k) and removeItem(k). HashTable implementation (with Bucket Arrays): Good average-case performance for n = Ω(N). Worst-case running time: is InsertItem(k, e) Θ(1), findElement(k) and removeItem(k) are both Θ(n). Binary Search Tree implem. (without Balancing): Good in the average-case—about Θ(lg n) for all operations. Worst-case running time: Θ(n) for all operations. Balanced Binary search trees: Worst-case is Θ(lg n) for all operations.

slide-4
SLIDE 4

Binary Search Trees

Abstract definition: A binary tree is either empty or has a root vertex with a left and a right child each of which is a tree.

◮ Recursive datatype definition.

So every vertex v, either: (i) has two children (v is an internal vertex), or (ii) has no children (v is a leaf). An internal vertex v has a left child and a right child which might be another internal vertex or a leaf. A near leaf is an internal vertex with one or both children being leaves.

Definition

A tree storing (key, element) pairs is a Binary Search Tree if for every internal vertex v, the key k of v is:

◮ greater than or equal to every key in v’s left subtree, and ◮ less than or equal to every key in v’s right subtree.

slide-5
SLIDE 5

Key parameter for runtimes: height

◮ Given any vertex v of a tree T and a leaf there is a unique

path form the vertex to the leaf:

◮ length of path defined as number of internal vertices.

◮ The height of a vertex is the maximum length over all

paths from it to leaves.

◮ The height of a tree is the height of the root. ◮ Note that if v has left child l and right child r then

height(v) = 1 + max{height(l), height(r)}.

◮ If we insert vr along the path v1, v2, . . . , vr then only the

heights of v1, v2, . . . , vr might be affected, all other vertices keep their previous height.

slide-6
SLIDE 6

Binary Search Trees for Dictionary

Leaves are kept empty. Algorithm findElement(k)

  • 1. if isEmpty(T) then return NO_SUCH_KEY
  • 2. else

3. u ← root 4. while ((u is not null) and u.key = k) do 5. if (k < u.key) then u ← u.left 6. else u ← u.right 7.

  • d

8. if (u is not null) and u.key = k then return u.elt 9. else return NO_SUCH_KEY findElement runs in O(h) time, where h is height.

slide-7
SLIDE 7

Binary Search Trees

4 1 1 1 1 2 3 3 2

67 45 18 21 16 24 43 24 55

slide-8
SLIDE 8

Binary Search Trees for Dictionary

Algorithm insertItemBST(k, e)

  • 1. Perform findElement(k) to find the “right" place for an item

with key k (if it finds k high in the tree, walk down to the “near-leaf” with largest key no greater than k).

  • 2. Neighbouring leaf vertex u becomes internal vertex,

u.key ← k, u.elt ← e.

4 1 1 1 1 2 3 3 2

67 45 18 21 16 24 43 24 55

slide-9
SLIDE 9

Binary Search Trees for Dictionary

Algorithm removeItemBST(k)

  • 1. Perform findElement(k) on the tree to get to vertex t.
  • 2. if we find t with t.key = k,

3. then remove the item at t, set e = t.elt. 4. Let u be “near-leaf" closest to k. Move u’s item up to t.

  • 5. else return NO_SUCH_KEY

4 1 1 1 1 2 3 3 2

67 45 18 21 16 24 43 24 55

slide-10
SLIDE 10

Worst-case running time

Theorem: For the binary search tree implementation of Dictionary, all methods (findElement, insertItemBST, removeItemBST) have asymptotic worst-case running time Θ(h), where h is the height of the tree. (can be Θ(n)).

slide-11
SLIDE 11

AVL Trees (G.M. Adelson-Velsky & E.M. Landis, 1962)

  • 1. A vertex of a tree is balanced if the heights of its children

differ by at most 1.

  • 2. An AVL tree is a binary search tree in which all vertices are

balanced.

slide-12
SLIDE 12

Not an AVL tree:

4 1 1 1 1 2 3 3 2

67 45 18 21 16 24 43 24 55

slide-13
SLIDE 13

An AVL tree

67 45 24 18 21 16 24 43 55 10

slide-14
SLIDE 14

The height of AVL trees

Theorem: The height of an AVL tree storing n items is O(lg(n)). Corollary: The running time of the binary search tree methods findElement, insertItem, removeItem is O(lg(n)) on an AVL tree. Let n(h) denote minimum number of items stored in an AVL tree of height h. So n(1) = 1, n(2) = 2, n(3) = 4. Claim: n(h) > 2h/2 − 1. n(h) ≥ 1 + n(h − 1) + n(h − 2) > 1 + 2

h−1 2 − 1 + 2 h−2 2 − 1

= (2− 1

2 + 2−1) 2 h 2 − 1

> 2

h 2 − 1.

Problem: After we apply insertItem or removeItem to an AVL tree, the resulting tree might no longer be an AVL tree.

slide-15
SLIDE 15

Example

67 45 24 18 21 16 24 43 55 10

AVL tree. INSERT 60

slide-16
SLIDE 16

Example (cont’d)

45 24 18 21 16 24 43 55 10 67 60

not AVL now . . .

slide-17
SLIDE 17

Example (cont’d)

x Y X V W z y 18 21 16 24 55 10 67 24 45 60 43

We can rotate . . .

slide-18
SLIDE 18

Example (cont’d)

z x y 24 45 60 18 21 16 24 10 43 67 55

Now is AVL tree. INSERT 44

slide-19
SLIDE 19

Example (cont’d)

18 21 16 24 10 60 43 45 24 44 55 67

AVL tree.

slide-20
SLIDE 20

Restructuring

◮ z unbalanced vertex of minimal height ◮ y child of z of larger height ◮ x child of y of larger height (exists because 1 ins/del

unbalanced the tree).

◮ V, W subtrees rooted at children of x ◮ X subtree rooted at sibling of x ◮ Y subtree rooted at sibling of y

Then height(V) − 1 ≤ height(W) ≤ height(V) + 1 max{height(V), height(W)} = height(X) max{height(V), height(W)} = height(Y).

slide-21
SLIDE 21

A clockwise single rotation

(b) x y z x y z (a) X Y V W W Y X V

slide-22
SLIDE 22

An anti-clockwise single rotation

(b) x y z y x (a) z W V X Y Y X W V

slide-23
SLIDE 23

An anti-clockwise clockwise double rotation

z y (a) z x y (b) x Y W Y V X W V X

rot 1 rot 2

slide-24
SLIDE 24

A clockwise anti-clockwise double rotation

y x z (b) z y (a) x X W V Y Y W V X

rot 1 rot 2

slide-25
SLIDE 25

Rotations

After an InsertItem(): We can always rebalance using just one single rotation or one double rotation (only 2x2 cases in total). single rotation: We make y the new root (of rebalancing subtree), z moves down, and the X subtree crosses to become 2nd child of z (with X as sibling). double rotation: We make x the new root, y and z become its children, and the two subtrees of x get split between each side. Θ(1) time for a single or double rotation.

slide-26
SLIDE 26

The insertion algorithm

Algorithm insertItem(k, e)

  • 1. Insert (k, e) into the tree with insertItemBST.

Let u be the newly inserted vertex.

  • 2. Find first unbalanced vertex z on the path from u to root.
  • 3. if there is no such vertex,

4. then return 5. else Let y and x be child, grandchild of z on z → u path. 6. Apply the appropriate rotation to x, y, z. return

slide-27
SLIDE 27

Example (cont’d)

18 21 16 24 10 60 43 45 24 44 55 67

AVL tree. REMOVE 10.

slide-28
SLIDE 28

Example (cont’d)

z x y 18 21 16 24 60 43 45 24 44 55 67

Not AVL tree . . . We rotate

slide-29
SLIDE 29

Example (cont’d)

z y x 21 24 16 60 43 45 24 44 55 67 18

Still not AVL . . . We rotate again.

slide-30
SLIDE 30

Example (cont’d)

y z x 21 18 16 45 44 24 60 67 55 24 43

AVL tree again.

slide-31
SLIDE 31

Rotations

After a removeItem(): We may need to re-balance “up the tree”. This requires O(lg n) rotations at most, each takes O(1) time.

slide-32
SLIDE 32

The removal algorithm

Algorithm removeItem(k)

  • 1. Remove item (k, e) with key k from tree using removeItemBST.

Let u be leaf replacing removed vertex.

  • 2. while u is not the root do

3. let z be the parent of u 4. if z is unbalanced then 5. do the appropriate rotation at z 6. let u be the parent of u

  • 7. return e
slide-33
SLIDE 33

Question on heights of AVL trees

◮ By definition of an AVL tree, for every internal vertex v, the

difference between the height of the left child of v and the right child of v is at most 1.

◮ How large a difference can there be in the heights of any

two vertices at the same “level” of an AVL tree?

◮ 1. ◮ 2. ◮ At most lg(n). ◮ Up to n.

Answer: At most lg(n).

slide-34
SLIDE 34

Example of “globally-less-balanced” AVL tree

3 5 7 6 4 5 4 4 3 2 3 3 3 2 2 1 3 2 1 1 1 2 1 1 2 1 2 2 1 1 1 1 1 1

For this example, n = 33, lg(n) > 5.

slide-35
SLIDE 35

Ordered Dictionaries

The OrderedDictionary ADT is an extension of the Dictionary ADT that supports the following additional methods:

◮ closestKeyBefore(k): Return the key of the item with the

largest key less than or equal to k.

◮ closestElemBefore(k): Return the element of the item with

the largest key less than or equal to k.

◮ closestKeyAfter(k): Return the key of the item with the

smallest key greater than or equal to k.

◮ closestElemAfter(k): Return the element of the item with

the smallest key greater than or equal to k.

slide-36
SLIDE 36

Range Queries

findAllItemsBetween(k1, k2): Return a list of all items whose key is between k1 and k2. Binary Search Trees support Ordered Dictionaries AND Range Queries well.

slide-37
SLIDE 37

Reading and Resources

◮ If you have [GT]:

The Chapter on “Binary Search Trees" has a nice treatment of AVL trees. The chapter on “Trees" has details

  • f tree traversal etc.

◮ If you have [CLRS]:

The balanced trees are Red-Black trees, a bit different from AVL trees.