Presentation is loading. Please wait.

Presentation is loading. Please wait.

Balancing Binary Search Trees, Rotations

Similar presentations


Presentation on theme: "Balancing Binary Search Trees, Rotations"— Presentation transcript:

1 Balancing Binary Search Trees, Rotations
AVL Trees, AA Trees Balancing Binary Search Trees, Rotations Tree-Like Structures SoftUni Team Technical Trainers Software University © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

2 Table of Contents AVL Trees AA Trees Properties of AVL
* Table of Contents AVL Trees Properties of AVL Rotations in AVL (Double Left, Double Right) AVL Insertion Algorithm AA Trees Properties of AA Rotations in AA Skew Split (c) 2007 National Academy for Software Development - All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

3 Have a Question? sli.do #DsAlgo

4 Properties and Rotations
AVL Trees Properties and Rotations

5 AVL Tree AVL tree is a self-balancing binary-search tree (visualization) Height of two subtrees can differ by at most 1 17 25 9 5 12 Average Worst case Space O(n) Search O(log n) Insert Delete

6 AVL Tree Rebalancing Height difference is measured by a balance factor (BF) BF(Tree) = Height(Left) – Height(Right) BF of any node is in the range [-1, 1] If BF becomes -2 or 2  rebalance 17 25 9 5 12 20 1

7 AVL Tree Rebalancing Rebalancing is done by retracing
Start from inserted node's parent and go up to root Perform rotations to restore balance -1 17 25 9 5 12 20 19 1 2 1

8 Right Rotation Set to be child of
x y Set to be child of Set Right Child of to be Left Child of y x In Order Preserved x y C A Right rotation (x) x y A B B C

9 Left Rotation Set to be child of
y x Set to be child of Set Left Child of to be Right Child of x y In Order Preserved y x A C Left rotation (y) x y B C A B

10 AVL Tree Insertion Algorithm
Insert like in ordinary BST Retrace up to root Modify balance / height If balance factor ∉ [-1,1]  rebalance 4 20 3 2 12 25 2 2 1 8 17 26 1 1 1 1 4 10 15 19

11 Insertion - #1 Insert 11 4 20 3 2 12 25 2 2 1 8 17 26 1 1 1 1 4 10 15 19

12 Insertion - #2 11 <  go left 4 2 3 2 1 2 1 1 1 1 20 20 12 25 8 17
26 1 1 1 1 4 10 15 19

13 Insertion - #3 11 <  go left 4 2 3 2 1 2 1 1 1 1 12 20 12 25 8 17
26 1 1 1 1 4 10 15 19

14 Insertion - #4 11 >  go right 4 2 3 2 1 2 1 1 1 1 8 20 12 25 8 17
26 1 1 1 1 4 10 15 19

15 Insertion - #5 11 >  go right 4 2 3 2 1 2 1 1 1 1 10 20 12 25 8 17
26 1 1 1 1 4 10 15 19

16 Insertion - #5 Right node is null  insert Update height balance is -1
4 20 10 3 2 10 12 25 2 2 1 8 17 26 1 2 1 1 4 10 15 19 1 11

17 Insertion - #5 Update height balance is -1 4 2 3 2 1 3 1 1 2 1 1 8 20
12 25 2 3 1 8 17 26 1 2 1 1 4 10 15 19 1 11

18 Insertion - #5 Update height balance is 1 4 2 4 2 1 3 1 1 2 1 1 12 20
25 2 3 1 8 17 26 1 2 1 1 4 10 15 19 1 11

19 Insertion - #5 Update height balance is 2 is left heavy rotate right 5
20 5 20 20 4 2 20 12 25 2 20 3 1 8 17 26 1 2 1 1 4 10 15 19 1 11

20 Insertion - #5 Switch parent 4 5 3 2 2 1 2 1 1 1 1 17 12 20 8 17 25 4
10 1 1 1 1 11 15 19 26

21 Insertion - #5 Update height 4 3 3 2 2 1 2 1 1 1 1 20 12 20 8 17 25 4
10 1 1 1 1 11 15 19 26

22 Insertion - #5 Update height Balance is 0 4 3 3 2 2 1 2 1 1 1 1 12 12
20 8 2 2 1 2 17 25 4 10 1 1 1 1 11 15 19 26

23 Insertion - #5 Over More: https://en.wikipedia.org/wiki/AVL_tree 4 3 3
12 3 3 20 8 2 2 1 2 17 25 4 10 1 1 1 1 11 15 19 26

24 AVL Tree - Quiz Delete 25. What will be the resulting tree? 17 25 9 5
TIME: TIME’S UP! Delete 25. What will be the resulting tree? 17 25 9 5 12

25 AVL Tree - Quiz   Delete 25. What will be the resulting tree? 17 25
TIME’S UP! Delete 25. What will be the resulting tree? 17 25 9 5 12 17 9 5 12 9 5 17 12

26 Double Left, Double Right Rotation
Double Rotations Double Left, Double Right Rotation

27 Single Rotation Problem
Insert 4 2 5 D -1 3 A 4 B C

28 Single Rotation Problem (2)
Rotate a node with opposite balanced child 2 -2 Rotate Right 5 doesn’t solve it 5 3 D A 1 -1 3 5 A D 4 4 B C B C

29 Double Right Rotation Right-Left

30 Double Right Rotation Rotate Right (node) with negatively balanced Left Child 2 5 D -1 3 A 4 B C

31 Double Right Rotation Left Rotate 3 2 5 D -1 3 A 4 B C

32 AVL Tree - Double Rotations
Update Balance 3 2 5 D 4 C -1 3 A B

33 AVL Tree - Double Rotations
Update Balance 3 2 5 D 4 C 3 A B

34 AVL Tree - Double Rotations
Right Rotate (5) 2 Reduced to Single Right (5) 5 D 1 4 C 3 A B

35 AVL Tree - Double Rotations
Update Balance 5 1 4 2 3 5 A B C D

36 AVL Tree - Double Rotations
Update Balance 4 1 4 3 5 A B C D

37 AVL Tree - Double Rotations
Balance Restored! 4 3 5 A B C D

38 Double Left Rotation Left-Right

39 AVL Tree - Double Rotations
Rotate Left (node) with positively balanced Right Child -2 5 D 1 3 A 4 C B

40 AVL Tree - Double Rotations
Rotate Right (3) -2 5 D 1 3 A 4 C B

41 AVL Tree - Double Rotations
Update Balance (3) -2 5 D 4 C 1 3 B A

42 AVL Tree - Double Rotations
Update Balance (3) -2 5 D 4 C 3 B A

43 AVL Tree - Double Rotations
Rotate Left (5) -2 Reduced to Single Left (5) 5 D 1 4 C 3 B A

44 AVL Tree - Double Rotations
Update Balance (5) 1 4 -2 5 3 D C C A

45 AVL Tree - Double Rotations
Update Balance (5) 1 4 5 3 D C C A

46 AVL Tree - Double Rotations
Update Balance (4) 4 5 3 D C C A

47 AVL Tree - Quiz Insert 22. What will be the resulting tree? 17 25 9 5
TIME: TIME’S UP! Insert 22. What will be the resulting tree? 17 25 9 5 20 12

48 AVL Tree - Quiz   Insert 22. What will be the resulting tree? 17 25
TIME’S UP! Insert 22. What will be the resulting tree? 17 25 9 5 20 12 22 17 25 9 5 12 20 17 25 9 5 12 22 20

49 Insert/Delete perform O(lgN) rotations
AVL Tree - Summary Structure Worst case Average case Search Insert Delete Search Hit BST N 1.39 lg N 2-3 Tree c lg N Red-Black 2 lg N lg N AVL Tree 1.44 lg N Insert/Delete perform O(lgN) rotations

50 Balancing Implementation
22 17 25 9 5 12 20 AVL Tree Balancing Implementation

51 Rotate Right public static Node<T> RotateRight(Node<T> node) { Node<T> left = node.Left; node.Left = node.Left.Right; left.Right = node; UpdateHeight(node); return left; }

52 Balance Node private static Node<T> Balance(Node<T> node)
{ int balance = Height(node.Left) - Height(node.Right); if (balance < -1) // Right child is heavy balance = Height(node.Right.Left) - Height(node.Right.Right); if (balance <= 0) { return RotateLeft(node); } else { node.Right = RotateRight(node.Right); return RotateLeft(node); } } else if (balance > 1) // Left child is heavy balance = Height(node.Left.Left) - Height(node.Left.Right); if (balance >= 0) { return RotateRight(node); } else { node.Left = RotateLeft(node.Left); return RotateRight(node); } return node;

53 Lab Exercise AVL Tree - Balancing

54 AA Tree Utilizes the concept of levels
Level - the number of left links on the path to a null node 17 Level 3 9 25 Level 2 5 12 22 32 Level 1

55 AA Tree AA tree invariants The level of every leaf node is one
Every left child has level one less than its parent Every right child has level equal to or one less than its parent Right grandchildren have levels less than their grandparents Every node of level greater than one has two children

56 AA Tree Right horizontal links are possible
Left horizontal links are not allowed 17 Level 3 9 25 Level 2 5 10 12 22 32 55 Level 1

57 Skew Skew operation is a single right rotation
Skew when an insertion or deletion creates a horizontal left link 17 Level 3 9 25 Level 2 5 10 12 22 32 55 Level 1

58 Skew (2) Skew operation is a single right rotation
Skew when an insertion or deletion creates a horizontal left link 17 Level 3 9 25 Level 2 5 10 12 22 32 55 Level 1

59 Split Split operation is a single left rotation
Split when an insertion or deletion two consecutive right horizontal links 17 Level 3 9 25 Level 2 5 10 12 22 32 55 71 Level 1

60 Split Split operation is a single left rotation
Split when an insertion or deletion two consecutive right horizontal links 17 Level 3 9 25 55 Level 2 5 10 12 22 32 71 Level 1

61 17 25 9 5 12 22 32 55 10 71 AA Tree Insertion Algorithm

62 AA Tree Insertion #1 Insert: 6
New nodes are always inserted at Level 1 Level 3 Level 2 6 Level 1

63 AA Tree Insertion #1 Insert: 2 Level 3 Level 2 6 Level 1

64 AA Tree Insertion #1 Left horizontal link is not allowed
Rotate right (skew) 6 Level 3 Level 2 2 6 Level 1

65 AA Tree Insertion #1 Insert: 8 Level 3 Level 2 2 6 Level 1

66 AA Tree Insertion #1 Insert: 8 Level 3 Level 2 2 6 Level 1

67 AA Tree Insertion #1 Insert: 8 Level 3 Level 2 2 6 Level 1

68 AA Tree Insertion #1 Two consecutive right horizontal links
Rotate left (split) 2 Level 3 Level 2 2 6 8 Level 1

69 AA Tree Insertion #1 Insert: 16 Level 3 6 Level 2 2 8 Level 1

70 AA Tree Insertion #1 Insert: 16 Level 3 6 Level 2 2 8 Level 1

71 AA Tree Insertion #1 Insert: 16 Level 3 6 Level 2 2 8 Level 1

72 AA Tree Insertion #1 Insert: 16 Level 3 6 Level 2 2 8 16 Level 1

73 AA Tree Insertion #1 Insert: 10 Level 3 6 Level 2 2 8 16 Level 1

74 AA Tree Insertion #1 Insert: 10 Level 3 6 Level 2 2 8 16 Level 1

75 AA Tree Insertion #1 Insert: 10 Level 3 6 Level 2 2 8 16 Level 1

76 AA Tree Insertion #1 Horizontal left link not allowed
Rotate right (skew) 16 Level 3 6 Level 2 2 8 16 Level 1 10

77 AA Tree Insertion #1 Two consecutive right horizontal links
Rotate left (split) 8 Level 3 6 Level 2 2 8 10 16 Level 1

78 AA Tree Insertion #1 Two consecutive right horizontal links
Rotate left (split) 8 Level 3 6 10 Level 2 2 8 16 Level 1

79 Trees - Quiz Consider web application in which
TIME’S UP! TIME: Consider web application in which searches are far more frequent than insertions/deletions Which of the following do you prefer: AVL Linked List Red-Black B+

80 AVL trees are more rigidly balanced, so they have faster search
Trees - Quiz TIME’S UP! Consider web application in which searches are far more frequent than insertions/deletions Which of the following do you prefer: AVL Linked List Red-Black B+ AVL trees are more rigidly balanced, so they have faster search

81 Summary AVL/AA trees tend to be flatter than Red-Black Trees
AVL/AA trees perform more rotations for insertion and deletion Red-Black trees have faster insertion/deletion AVL/AA trees have faster searching AA have simpler implementation

82 AVL Trees, AA Trees © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

83 License This course (slides, examples, labs, videos, homework, etc.) is licensed under the "Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International" license Attribution: this work may contain portions from "Fundamentals of Computer Programming with C#" book by Svetlin Nakov & Co. under CC-BY-SA license "Data Structures and Algorithms" course by Telerik Academy under CC-BY-NC-SA license © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

84 Free Trainings @ Software University
Software University Foundation – softuni.org Software University – High-Quality Education, Profession and Job for Software Developers softuni.bg Software Facebook facebook.com/SoftwareUniversity Software YouTube youtube.com/SoftwareUniversity Software University Forums – forum.softuni.bg © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.


Download ppt "Balancing Binary Search Trees, Rotations"

Similar presentations


Ads by Google