Download presentation

Presentation is loading. Please wait.

Published byFernando Crowell Modified over 3 years ago

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

2
AVL Tree properties Height-Balance Property AVL Tree AVL Interface AVL Height 2

3
AVL Tree example 3 14 17 7 4 53 11 12 813

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

5
right A single right rotation of ’11’ is executed to rebalance the tree: Insert 13 5 14 177 45311 13

6
Now insert 12 6 14 177 45311 13 12 The sub-tree of 11 is unbalanced. Double rotation: right and then left.

7
After right rotation of ’13’ Now left rotate ’11’ 7 14 177 45311 12 13

8
After left rotation of ’11’ Now balanced! 8 14 177 45312 1311

9
Now insert 8 9 14 177 45312 1311 8 The sub-tree of 7 is unbalanced. Required double rotation: right and then left.

10
After right rotation of ’12’ Now left rotate ‘7’ 10 14 177 45311 128 13

11
Now balanced! 11 14 17 7 4 53 11 12 813

12
Remove 53 12 14 17 7 4 53 11 12 813

13
Unbalanced! Right rotate ’14’ 13 14 17 7 4 11 12 813

14
Balanced! Remove 11 14 17 7 4 11 128 13

15
Remove 11 Replace it with the maximum in its left branch 15 14 17 7 4 11 128 13

16
Remove 8 16 14 17 7 4 8 12 13

17
Unbalanced! Required double rotatation 17 14 17 4 7 12 13

18
After right rotation of ‘14’ 18 14 17 4 7 12 13

19
After left rotation of ‘7’ 19 14 17 4 7 12 13

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. 20

21
Question 2 solution balance 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. 21

22
Question 2 solution balance 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. 22 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 )

23
Question 3 23

24
Question 3 solution 24

25
Question 3 solution 25

26
Question 3 solution Reminder: 26 TREE-SUCCESSOR(x) If x.right != NULL then return TREE-MINIMUM(x.right) y ← x.parent while y != NULL and x == y.right do x ← y y ← y.parent return y

27
Question 3 solution 27

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

29
Question 4 solution 29

30
Question 5 Suggest a data structure for storing integers that supports the following operations. 30 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 i th 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)

31
Question 5 solution For 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 solution We 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. 31

32
Question 5 solution 32

Similar presentations

OK

Januar MDMDFSSMDMDFSSS 3 10 17 24 31 4 11 18 25 5 12 19 26 6 13 20 27 7 14 21 28 1 8 15 22 29 2 9 16 23 30.

Januar MDMDFSSMDMDFSSS 3 10 17 24 31 4 11 18 25 5 12 19 26 6 13 20 27 7 14 21 28 1 8 15 22 29 2 9 16 23 30.

© 2018 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on game theory five nights Ppt on nuclear family and joint family definition Download ppt on oxidation and reduction reaction Ppt on forward contract examples Ppt on acid base indicators Ppt on conservation of forest and wildlife Ppt on life of amelia earhart Ppt on conservation of natural resources for class 10 Ppt on ip address classes history Ppt on spice mobile