Presentation is loading. Please wait.

Presentation is loading. Please wait.

AVL Trees "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust.

Similar presentations


Presentation on theme: "AVL Trees "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust."β€” Presentation transcript:

1 AVL Trees "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust

2 Binary Search Trees All the operations on a binary search tree cost 𝑂(β„Ž), where β„Ž is the height of the tree. These operations are efficient when β„Ž= O(log 𝑛) . However, it may happen that β„Ž=𝑂(𝑛). When the tree is entirely unbalanced. The cost of any operation is linear in the number of nodes and not logarithmic. Solution: Keep the tree balance to ensure operations always cost O(log n)! CS Data Structures

3 Measuring Tree Balance
In a tree, the balance factor of a node x is defined as the difference in the heights of its two sub-trees: π΅π‘Žπ‘™π‘Žπ‘›π‘π‘’πΉπ‘Žπ‘π‘‘π‘œπ‘Ÿ π‘₯ =π»π‘’π‘–π‘”β„Žπ‘‘ π‘…π‘–π‘”β„Žπ‘‘ π‘₯ βˆ’π»π‘’π‘–π‘”β„Žπ‘‘(𝐿𝑒𝑓𝑑 π‘₯ ) Node height is the number of edges between a node and the furthest leaf in its sub-trees. Nodes with balance factors <0 are called "left-heavy." Nodes with balance factors >0 are called "right-heavy." Nodes with balance factors =0 are called "balanced." A balanced tree is a tree in which all of its nodes are balanced. CS Data Structures

4 Calculating Balance Factor
For instance, the balance factor of J is: π΅π‘Žπ‘™π‘Žπ‘›π‘π‘’πΉπ‘Žπ‘π‘‘π‘œπ‘Ÿ 𝐽 =π»π‘’π‘–π‘”β„Žπ‘‘ π‘…π‘–π‘”β„Žπ‘‘ 𝐽 βˆ’π»π‘’π‘–π‘”β„Žπ‘‘ 𝐿𝑒𝑓𝑑 𝐽 π΅π‘Žπ‘™π‘Žπ‘›π‘π‘’πΉπ‘Žπ‘π‘‘π‘œπ‘Ÿ 𝐽 =π»π‘’π‘–π‘”β„Žπ‘‘ 𝑃 βˆ’π»π‘’π‘–π‘”β„Žπ‘‘ 𝐹 π΅π‘Žπ‘™π‘Žπ‘›π‘π‘’πΉπ‘Žπ‘π‘‘π‘œπ‘Ÿ 𝐽 =3βˆ’2 π΅π‘Žπ‘™π‘Žπ‘›π‘π‘’πΉπ‘Žπ‘π‘‘π‘œπ‘Ÿ 𝐽 =+1 CS Data Structures

5 Balancing Trees Don’t balance Strict balance Good balance
May end up with some nodes very deep Strict balance The tree must always be balanced perfectly Expensive must ensure tree complete after every modification Good balance Allow some imbalance Adjust on access Self-adjusting CS Data Structures

6 Balancing Binary Search Trees
Ignoring balance leads to poor performance Maintaining perfect balance is too costly Many algorithms exist for keeping good balance Height-balanced trees AVL trees (Adelson-Velskii and Landis) Self-adjusting trees Splay trees Multiway search trees B-Trees CS Data Structures

7 AVL-Trees An AVL tree is a self-balancing, binary search tree.
named after its inventors, Adelson-Velskii and Landis. first proposed dynamically balancing trees. not perfectly balanced, but each node has a balance factor of -1, 0, or 1. maintains 𝑂 log 𝑛 search, addition and deletion time. CS Data Structures

8 Unbalanced AVL-Trees Insertions and deletions may change the balance factor of nodes in a binary search tree For instance, given this deletion: 6 -1 6 -2 3 8 3 +1 Delete 8 2 5 2 5 +1 CS Data Structures

9 Unbalanced AVL-Trees or this insertion: 6 6 3 8 3 8 Insert 4 2 5 2 5 4
-1 6 -2 3 8 3 +1 8 Insert 4 2 5 2 5 +1 4 CS Data Structures

10 AVL-Tree Imbalance After an insertion or a deletion, one or more nodes can become too far out of balance (i.e. have balance factors of βˆ’2 π‘œπ‘Ÿ 2). These imbalances can occur in four places: outside in the left sub-tree in the right sub-tree inside in the right sub-tree of the left sub-tree in the left sub-tree of the right sub-tree CS Data Structures

11 AVL-Tree Imbalances Outside: -Left sub-tree -Right sub-tree
Note: T1, T2, and T3 represent arbitrary subtrees, which may be empty or may contain any number of nodes. CS Data Structures

12 AVL-Tree Imbalances Inside: - Right sub-tree of left sub-tree
- Left sub-tree of right sub-tree CS Data Structures

13 AVL-Tree Rotation To rebalance an AVL-Tree, perform one or more tree rotations. There are 4 types of tree rotations, depending on where the imbalance occurs in the tree: Outside: LL-Rotation – right sub-tree’s, right sub-tree imbalance RR-Rotation – left sub-tree’s, left sub-tree imbalance Inside: LR-Rotation – left sub-tree’s, right sub-tree imbalance a LL-Rotation followed by a RR-Rotation RL-Rotation – right sub-tree’s, left-sub-tree imbalance a RR-Rotation followed by a LL-Rotation CS Data Structures

14 Key Idea If a node is out of balance in a given direction, rotate it in the opposite direction. rotation: A swap between a parent and its left or right child, maintaining proper BST ordering. 25 8 rotate right 8 3 25 3 11 11 CS Data Structures

15 RR-Rotations When insert a node into the left sub-tree of a left sub-tree, it may cause a sub-tree imbalance. To rebalance, perform a RR-Rotation in the clockwise direction. 𝑅𝑅 CS Data Structures

16 Example: RR-Rotation Below, when A is inserted into the left sub-tree of C’s left sub-tree, C has a -2 imbalance. We perform a RR-Rotation by making C the right-subtree of its left sub-tree, B. Note: If B had a right sub-tree, that sub-tree would become C’s left sub-tree (i.e. a second right rotation). -2 -1 CS Data Structures

17 LL-Rotations When insert a node into the right sub-tree of a right sub-tree, it may cause a sub-tree imbalance. To rebalance the tree, perform a LL-Rotation, in the counter-clockwise direction. 𝐿𝐿 CS Data Structures

18 Example: LL-Rotation Below, when C is inserted into the right sub-tree of A’s right sub-tree, A has a +2 imbalance. Perform a left rotation by making A the left-subtree of its right sub-tree, B. Note: If B had a left sub-tree, that sub-tree would become A’s right sub-tree (i.e a second left rotation). +2 +1 CS Data Structures

19 LR-Rotations When insert a node into the right sub-tree of a left sub-tree, it may cause a sub-tree imbalance. To rebalance, perform a LR-Rotation. Inside rotations are combinations of the outside rotations. A LR-Rotation is the combination of a LL-Rotation and a RR-Rotation. 𝐿𝑅 CS Data Structures

20 Example: LR-Rotation -2 For example, when B is inserted into the right sub-tree of C’s left sub-tree, C has a -2 imbalance. +1 Perform a LL-Rotation on the left sub-tree of C. This makes A the left sub-tree of B. Note: If B had a left sub-tree, it would become A’s right sub-tree. CS Data Structures

21 Example: LR-Rotation -2 The sub-tree is still unbalanced. However, it’s now because of the left sub-tree of the left sub-tree. -1 Perform a RR-Rotation, making B the new root node of this sub-tree. C becomes the right sub-tree of its left sub-tree, B. Note: If B had a right sub-tree, it would become C’s left sub-tree. The sub-tree is now balanced. CS Data Structures

22 RL-Rotations When insert a node into the left sub-tree of a right sub-tree, it may cause a sub-tree imbalance. To rebalance, perform a RL-Rotation. A RL-Rotation is the combination of a RR-Rotation a LL-Rotation. 𝑅𝐿 CS Data Structures

23 Example: RL-Rotation +2 For example, when B is inserted into the left sub-tree of A’s right sub-tree, B has a +2 imbalance. -1 Perform a RR-Rotation on the right sub-tree of A. This makes C the right sub-tree of B. Note: If B had a right sub-tree, it would become C’s left sub-tree. CS Data Structures

24 Example: RL-Rotation +2 The sub-tree is still unbalanced. However, it’s now because of the right sub-tree of the right sub-tree. -1 Perform LL-Rotation, making B the new root of the sub-tree. A becomes the left sub-tree of its right sub-tree B. Note: If B had a left sub-tree, it would become A’s right sub-tree. The sub-tree is now balanced. CS Data Structures

25 AVL-Tree Insertion Insert the new node into the tree, like insertion into a binary search tree Fix tree balance via rotations, if necessary A rotation is performed in sub-trees with a root that has a balance factor equal to +2 or -2. If there is more than one imbalance, first rotate at the node closest to the newly inserted node. CS Data Structures

26 Insert 15 in the following AVL-Tree:
Example: Insertion I Insert 15 in the following AVL-Tree: 10 20 5 12 4 7 8 2 -1 +1 CS Data Structures

27 Insertion induces left, right imbalance
Example: Insertion I Insertion induces left, right imbalance 10 20 5 12 4 7 8 2 -1 -2 +1 15 CS Data Structures

28 Example: Insertion I Which rotation use? CS Data Structures

29 After apply LR-Rotation at node 20
Example: Insertion I After apply LR-Rotation at node 20 10 15 5 12 4 7 8 2 -1 +1 20 CS Data Structures

30 Insert 9 in the following AVL-Tree:
Example: Insertion II Insert 9 in the following AVL-Tree: 10 20 5 12 4 7 8 2 -1 +1 CS Data Structures

31 Example: Insertion II Insertion induces left, right imbalance 10 20 5 12 4 7 8 2 -2 -1 +1 +2 We rotate at 7, closest node to 9 9 and a right, right imbalance CS Data Structures

32 Example: Insertion II Which rotation use? CS Data Structures

33 After apply LL-Rotation at node 7
Example: Insertion II 10 20 5 12 4 8 9 2 -1 7 After apply LL-Rotation at node 7 CS Data Structures

34 AVL-Trees Deletion Delete node as in other binary search trees
Fix tree balance via rotations, if necessary A rotation is performed in sub-trees having a root that has a balance factor equal to +2 or -2. If there is more than one imbalance, first rotate at the node closest to the deleted node. CS Data Structures

35 Delete 7 from the following AVL-Tree:
Example: Deletion I Delete 7 from the following AVL-Tree: 10 20 5 12 4 7 8 2 -1 +1 CS Data Structures

36 Example: Deletion I 10 20 5 12 4 8 2 No need to rotate! -1
No need to rotate! After deleting 7 CS Data Structures

37 Delete 10 from the following AVL-Tree:
Example: Deletion II Delete 10 from the following AVL-Tree: 10 20 5 12 4 7 8 2 -1 +1 CS Data Structures

38 Example: Deletion II After deleting 10, promote its successor, 12 Could also promote its predecessor, 5 12 20 5 4 7 8 2 -2 -1 +1 CS Data Structures

39 Example: Deletion II Deletion induces left, left imbalance Choose to use RR-Rotation but could use a LR-Rotation too. 12 20 5 4 7 8 2 -2 -1 +1 and a left, right imbalance! CS Data Structures

40 Example: Deletion II Which rotation use? CS Data Structures

41 After apply RR-Rotation at node 12
Example: Deletion II After apply RR-Rotation at node 12 5 12 4 2 20 8 +1 -1 +1 7 CS Data Structures

42 Delete 4 from the following AVL-Tree:
Example: Deletion III Delete 4 from the following AVL-Tree: 10 20 5 12 4 7 6 -1 +1 CS Data Structures

43 Deletion induces right, left imbalance
Example: Deletion III Deletion induces right, left imbalance 10 20 5 12 7 6 -1 +2 CS Data Structures

44 Example: Deletion II Which rotation use? CS Data Structures

45 After apply RL-Rotation at node 5
Example: Deletion III 10 20 6 12 5 7 -1 After apply RL-Rotation at node 5 CS Data Structures

46 Height of AVL-Trees Maximum number of nodes in a full AVL-tree:
𝑛 ≀ 2 β„Ž+1 βˆ’1 Then, β„Ž β‰₯ log 2 𝑛+1 βˆ’1 = Ξ©( log 𝑛 ) CS Data Structures

47 Minimum Number of Nodes
Minimum number of nodes in the AVL-tree: nh: minimum number of nodes when the height is h CS Data Structures

48 Minimum Number of Nodes
Minimum number of nodes in the AVL-tree: Recall the Fibonacci numbers nh = Fh + 1 CS Data Structures

49 Height of AVL-Trees Minimum number of nodes in the AVL-tree:
𝑛 β„Ž = 𝐹 β„Ž +1⟹ 𝑛 β„Ž > 𝐹 β„Ž 𝐹 β„Ž is the closest integer to πœ™ β„Ž , where πœ™=1.618 Then, 𝐹 β„Ž β‰ˆ πœ™ β„Ž 5 and 𝑛 β„Ž > πœ™ β„Ž , then β„Ž< log πœ™ log πœ™ 𝑛 =𝑂( log 𝑛 ) Since h is Ξ©( log 𝑛 )and 𝑂 log 𝑛 , h is Θ( log 𝑛 ). CS Data Structures

50 Disadvantages of AVL-Trees
Difficult to program and debug Asymptotically faster than other self-adjusting trees but rebalancing takes time. Most large searches are done in database systems on disk and use other structures (e.g. B-trees). A 𝑂(𝑛) runtime for a single operation may be justified, if total run time for many consecutive operations is fast (e.g. Splay trees). CS Data Structures

51 Advantages of AVL-Trees
Since AVL-Trees are always close to balanced, search is 𝑂( log 𝑛 ). Insertion and deletion are also 𝑂( log 𝑛 ). For both insertion and deletion, height re-balancing adds no more than a constant factor to the runtime. CS Data Structures

52 CS Data Structures

53 AVL Insertion: Outside Case
j Consider a valid AVL subtree k h Z h h X Y CS Data Structures

54 AVL Insertion: Outside Case
j Inserting into X destroys the AVL property at node j k h Z h+1 h Y X CS Data Structures

55 AVL Insertion: Outside Case
j Do a β€œright rotation” k h Z h+1 h Y X CS Data Structures

56 j k Z Y X Single right rotation Do a β€œright rotation”
CS Data Structures

57 Outside Case Completed
k β€œRight rotation” done! (β€œLeft rotation” is mirror symmetric) j h+1 h h X Z Y AVL property has been restored! CS Data Structures

58 AVL Insertion: Inside Case
j Consider a valid AVL subtree k h Z h h X Y CS Data Structures

59 AVL Insertion: Inside Case
j Inserting into Y destroys the AVL property at node j Does β€œright rotation” restore balance? k h Z h h+1 X Y CS Data Structures

60 AVL Insertion: Inside Case
k β€œRight rotation” does not restore balance… now k is out of balance j h X h h+1 Z Y CS Data Structures

61 AVL Insertion: Inside Case
j Consider the structure of subtree Y… k h Z h h+1 X Y CS Data Structures

62 AVL Insertion: Inside Case
j Y = node i and subtrees V and W k h Z i h h+1 X h or h-1 V W CS Data Structures

63 AVL Insertion: Inside Case
j We will do a left-right β€œdouble rotation” . . . k Z i X V W CS Data Structures

64 Double rotation : first rotation
j left rotation complete i Z k W V X CS Data Structures

65 Double rotation : second rotation
j Now do a right rotation i Z k W V X CS Data Structures

66 Double rotation : second rotation
right rotation complete Balance has been restored i k j h h h or h-1 V X W Z CS Data Structures

67 Implementation CS 321 - Data Structures balance (1,0,-1) key left
right No need to keep the height; just the difference in height, i.e. the balance factor; this has to be modified on the path of insertion even if you don’t perform rotations Once you have performed a rotation (single or double) you won’t need to go back up the tree CS Data Structures


Download ppt "AVL Trees "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust."

Similar presentations


Ads by Google