foundations of data structures practical session #7 avl trees 2

32
Foundations of Data Structures Practical Session #7 AVL Trees 2

Upload: fernando-crowell

Post on 01-Apr-2015

221 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Foundations of Data Structures Practical Session #7 AVL Trees 2

Foundations of Data Structures

Practical Session #7AVL Trees 2

Page 2: Foundations of Data Structures Practical Session #7 AVL Trees 2

2

AVL Tree properties

Height-Balance Property

For every internal node of a tree , the height of the children nodes of differ by at most 1.

AVL TreeAny binary search tree that satisfies the Height-Balance property. Thus, it has a height of , which implies an worst case search and insertion times.

AVL InterfaceSupports the following operations in time:insert, search, delete, maximum, minimum, predecessor and successor.

AVL Height Lemma: The height of an AVL tree storing keys is ).

Page 3: Foundations of Data Structures Practical Session #7 AVL Trees 2

3

AVL Tree example

14

17

7

4

53

11

12

8 13

Page 4: Foundations of Data Structures Practical Session #7 AVL Trees 2

4

Question 1

Insert the following sequence of integers into an empty AVL tree: 14, 17, 11, 7, 53, 4, 13

14

1711

7 53

4

Page 5: Foundations of Data Structures Practical Session #7 AVL Trees 2

5

A single right rotation of ’11’ is executed to rebalance the tree:

Insert 13

14

177

4 5311

13

Page 6: Foundations of Data Structures Practical Session #7 AVL Trees 2

6

Now insert 1214

177

4 5311

13

12

The sub-tree of 11 is unbalanced. Double rotation: right and then left.

Page 7: Foundations of Data Structures Practical Session #7 AVL Trees 2

7

After right rotation of ’13’Now left rotate ’11’ 14

177

4 5311

12

13

Page 8: Foundations of Data Structures Practical Session #7 AVL Trees 2

8

After left rotation of ’11’Now balanced! 14

177

4 5312

1311

Page 9: Foundations of Data Structures Practical Session #7 AVL Trees 2

9

Now insert 8

14

177

4 5312

1311

8

The sub-tree of 7 is unbalanced. Required double rotation: right and then left.

Page 10: Foundations of Data Structures Practical Session #7 AVL Trees 2

10

After right rotation of ’12’Now left rotate ‘7’ 14

177

4 5311

128

13

Page 11: Foundations of Data Structures Practical Session #7 AVL Trees 2

11

Now balanced!

14

17

7

4

53

11

12

8 13

Page 12: Foundations of Data Structures Practical Session #7 AVL Trees 2

12

Remove 53

14

17

7

4

53

11

12

8 13

Page 13: Foundations of Data Structures Practical Session #7 AVL Trees 2

13

Unbalanced!Right rotate ’14’ 14

17

7

4

11

12

8 13

Page 14: Foundations of Data Structures Practical Session #7 AVL Trees 2

14

Balanced!Remove 11

14

17

7

4

11

128

13

Page 15: Foundations of Data Structures Practical Session #7 AVL Trees 2

15

Remove 11

Replace it with the maximum in its left branch

14

17

7

4

11

128

13

Page 16: Foundations of Data Structures Practical Session #7 AVL Trees 2

16

Remove 8

14

17

7

4

8

12

13

Page 17: Foundations of Data Structures Practical Session #7 AVL Trees 2

17

Unbalanced!Required double rotatation

14

17

4

7

12

13

Page 18: Foundations of Data Structures Practical Session #7 AVL Trees 2

18

After right rotation of ‘14’

14

17

4

7

12

13

Page 19: Foundations of Data Structures Practical Session #7 AVL Trees 2

19

After left rotation of ‘7’

14

174

7

12

13

Page 20: Foundations of Data Structures Practical Session #7 AVL Trees 2

20

Question 2

In class we’ve seen an implementation of AVL tree where each node v has an extra field h, the height of the sub-tree rooted at v. The height can be used in order to balance the tree.

- How many bits are required to store the height in a node?- Answer: For an AVL tree with n nodes, h=O(logn) thus requires

O(loglogn) extra bits. 1. How can we reduce the number of the extra bits necessary for

balancing the AVL tree? 2. Suggest an algorithm for computing the height of a given AVL

tree given in the representation you suggested in 1.

Page 21: Foundations of Data Structures Practical Session #7 AVL Trees 2

21

Question 2 solution

1. Instead of a height field, which is redundant, each node will store 2 balance bits, calculated as the difference of heights between its right and left sub-trees.• Two bits suffice because the difference can be one

of the three: -1, 0, 1. (The leftmost bit represents the sign)

• The balance field should be updated on insert and delete operations, along the path to the root.

Page 22: Foundations of Data Structures Practical Session #7 AVL Trees 2

22

Question 2 solution

2. To compute the height of a tree, follow the path from the root to the deepest leaf by reading the balance field. If a sub tree is balanced to one side, the deepest leaf resides on that side.

CalcHeight(T) if T == null return -1 if T.balance == -1 or T.balance == 0 return 1 + CalcHeight( T.left ) else return 1 + CalcHeight( T.right )

Page 23: Foundations of Data Structures Practical Session #7 AVL Trees 2

23

Question 3

Suggest two ways for an AVL tree to support a query for retrieving all the keys in range in time, where is the number of keys in the range.

Page 24: Foundations of Data Structures Practical Session #7 AVL Trees 2

24

Question 3 solution

1. Store in each node pointers to its predecessor and successor.• Requires updating on insert and delete

operations.• Finding the successor/predecessor requires an

time, equivalent to in an AVL tree, thus the time of the insert and delete operations is unchanged.

Page 25: Foundations of Data Structures Practical Session #7 AVL Trees 2

25

Question 3 solution

2. Use the following claim: “Starting at any node in a height BST, successive calls to TREE-SUCCESSOR take time.”• Doesn’t require extra pointers.• Doesn’t require modifications to the insert and

delete operations.

Page 26: Foundations of Data Structures Practical Session #7 AVL Trees 2

26

Question 3 solution

Reminder:

TREE-SUCCESSOR(x)If x.right != NULL then

return TREE-MINIMUM(x.right)y ← x.parentwhile y != NULL and x == y.right do

x ← yy ← y.parent

return y

Page 27: Foundations of Data Structures Practical Session #7 AVL Trees 2

27

Question 3 solutionClaim: “Starting at any node in a height BST, successive calls to TREE-SUCCESSOR take time.”

Proof outline• Let be the starting node and be the ending node after successive calls to TREE-

SUCCESSOR.• Let be the simple path between and inclusive.• Let be the common ancestor of and that visits.• The length of is at most , which is .• Let output be the elements with keys between and inclusive.• The size of the output is .• In the execution of successive calls to TREE-SUCCESSOR, each node in is visited at

most 3 times (on the way to its left, right and up). • Besides the nodes and , if a sub tree of a node in is visited then all its elements are

in output.• Hence, the total running time is .

Page 28: Foundations of Data Structures Practical Session #7 AVL Trees 2

28

Question 4

Suggest an efficient algorithm for sorting an array of numbers. Analyze its running time and required space.

Page 29: Foundations of Data Structures Practical Session #7 AVL Trees 2

29

Question 4 solution

1. Define a new empty AVL tree, T.2. Traverse the input array and insert each item to T.3. Traverse the tree T in a In-order manner, copying

the items back to the array.

Time: step 2 requires , step 3 requires . In total .

Extra space: an AVL tree of size requires extra space.

Page 30: Foundations of Data Structures Practical Session #7 AVL Trees 2

30

Question 5

Suggest a data structure for storing integers that supports the following operations.

Init() Initialize the data structure. O(1)

Insert(x) Insert x, if it is not present yet. O(log n)

Delete(x) Delete x if it exists. O(log n)

DeletePlace(i) Delete the element in the ith place (as determined by the order of insertion). O(log n)

GetPlace(x) Return the place (which is determined by the order of insertion) of x. If x does not exist, return -1. O(log n)

Page 31: Foundations of Data Structures Practical Session #7 AVL Trees 2

31

Question 5 solutionFor example, for the following sequence of actions:Insert(3), Insert(5), Insert(11), Insert(4), Insert(7), Delete(5) GetPlace(7) returns 4, and DeletePlace(2) will delete 11.

The solutionWe will use two AVL trees:

• T1 stores the elements by their key. • T2 stores the elements by the order of insertion (using a running

counter).• There are pointers between the two trees connecting the nodes with

the same key.

Page 32: Foundations of Data Structures Practical Session #7 AVL Trees 2

32

Question 5 solutionInit() – initialize two empty trees Insert(x) – insert the element by its key into T1, insert it by its order of insertion into T2 and update the pointers. Delete(x) – find the element in T1 and delete it from both the trees, following the pointer.

DeletePlace(i) – find the node with key in T2, delete it from both the trees, following the pointer.

GetPlace(x) – find the node with key in T1, follow the pointer to its copy in T2 and return its key (in T2).