Download presentation
Presentation is loading. Please wait.
Published byJared Owens Modified over 9 years ago
1
1 CSC 2053 -TREES AVL & BALANCED TREES
2
2 Balanced Trees The advantage of balanced trees is that we can perform most operation in time proportional to O(log n) But what is a balanced tree? – A balanced binary tree is a binary tree where all leaves are within a specified distance from the root.
3
3 Balanced Trees Keeping a tree balanced guarantees an efficient search Why don't we do this then? Why don't we rebalance a tree every time a new node is inserted? – Because rebalancing can take up to O(n) operations.
4
4 Rebalancing a Binary Tree (example from Standish book) 5 73 246
5
5 5 73 246 1 Tree is now unbalanced
6
6 Rebalancing a Binary Tree (example from Standish book) 5 73 246 1 4 62 13 5 7 Compare the two trees. Every single node changes its position Rebalancing can indeed be O(n)
7
7 The Cost of Rebalancing To include complete rebalancing as part of the insert function, insertion may take O(n) instead of O(log n) What we need is the guarantee that search will take O(log n) in the worst case Can we do better? Can we find a way to get O(log n) for searches and O(log n) for insertion?
8
8 AVL Trees In 1960, two Russian mathematicians Georgii Maksimovich Adelson-Velskii and Evgenii Mikhailovich Landis developed a technique for keeping a binary search tree balanced as items are inserted into it. A binary search tree that incorporate their techniques into the operations is called an AVL tree You will be asked to spell their names in the final exam.
9
9 AVL TREES An AVL tree is a balanced binary search tree where the height of the two subtrees (children) of a node differs by at most one. A binary search tree has the AVL property if the heights of the left and right subtrees are either equal or differ by 1, and both subtrees hold the AVL property
10
10 Examples of AVL Trees
11
11 Examples of AVL Trees – Unbalanced Page 11
12
12 Balance factors We judge a tree to be balanced using the balance factor of nodes. – The balance factor of node A is the difference between the height of the left and the height of the right subtrees – – the left minus the right A tree is said to be an AVL tree if all of its nodes have balance factors 1, 0 or -1 0 1 00
13
13 Example of Trees and their balance factors subtract # of levels in the Left sub tree from the # of levels in the Right subtree
14
14 Example of Trees and their balance factors 1 11 1 0 0 00 0
15
15 Example of Trees and their balance factors
16
16 Example of Trees and their balance factors 0 2 0 0 0 0 -2 1 0
17
17 Example of Trees and their balance factors
18
18 Example of Trees and their balance factors 1 -23 0 0
19
19 Balance Factors and insertion There are only two ways that a tree can become unbalanced and that is through insertion or deletion. Thus each time one of these operations is performed, the balance factors must be checked starting at the point of insertion or removal of node and working back up the tree. Because of the need to go back up the tree, AVL trees are usually implemented with a reference to the parent node kept inside each node.
20
20 Rotations The most important characteristic of AVL trees is that: which are called rotations. rebalancing can be achieved using one of four possible shape transformations which are called rotations. new node is inserted These rotations are used when a new node is inserted
21
21 Rotations – Single-right rotation: – Single-right rotation: This rotation is used when the new item is in the left subtree of the left child of a node that has a balance factor of 2 – the node’s left sub tree is too long – Single-left rotation – Single-left rotation: This rotation is used when the new item is in the right subtree of the right child of an ancestor with balance factor -2 the nodes right sub tree is too long.
22
22 ROTATIONS – Double-right rotation(Left Right): – Double-right rotation(Left Right): This rotation is used when the new item is in the right subtree of the left child of the nearest ancestor with balance factor -2 – Double-left rotation(Right Left:) – Double-left rotation(Right Left:) This rotation is used when the new item is in the left subtree of the right child of the nearest ancestor with balance factor 2
23
23 AVL Trees balance factor of a node is 2left sub-tree has a path that is too long If the balance factor of a node is 2, this means the left sub-tree has a path that is too long balance factor of the left child is 1 long path is the left sub-tree of the left child If the balance factor of the left child is 1, this means that the long path is the left sub-tree of the left child simple right rotation of the left child In this case, a simple right rotation of the left child around the original node will solve the imbalance
24
24 balance factor of node is 2 Single-Right Rotation- balance factor of node is 2 C2C2 B1B1 A C B A Initial Configuration Final Configuration Parent
25
25 Single-Right Rotation C2C2 B A link from the parent of C to B 1. Reset the link from the parent of C to B left link of C equal to the right link of B. ( currently null) 2. Set the left link of C equal to the right link of B. ( currently null) right link of B to point to C. 3. Set the right link of B to point to C. link from the parent of C to B 1. Reset the link from the parent of C to B left link of C equal to the right link of B. ( currently null) 2. Set the left link of C equal to the right link of B. ( currently null) right link of B to point to C. 3. Set the right link of B to point to C. Parent
26
26 Single-Right Rotation C B A null 1. Reset the link from the parent of C to B 2. Set the left link of C equal to the right link of B. 3. Set the right link of B to point to C. 1. Reset the link from the parent of C to B 2. Set the left link of C equal to the right link of B. 3. Set the right link of B to point to C. P Represents the right link of B
27
27 Single-Right Rotation C B A X 1. Reset the link from the parent of C to B. Set the left link of C equal to the right link of B. ( which is null) 2. Set the left link of C equal to the right link of B. ( which is null) 3. Set the right link of B to point to C. 1. Reset the link from the parent of C to B. Set the left link of C equal to the right link of B. ( which is null) 2. Set the left link of C equal to the right link of B. ( which is null) 3. Set the right link of B to point to C. P
28
28 Single-Right Rotation C B A 1. Reset the link from the parent of C to B 2. Set the left link of C equal to the right link of B. Set the right link of B to point to C. 3. Set the right link of B to point to C. 1. Reset the link from the parent of C to B 2. Set the left link of C equal to the right link of B. Set the right link of B to point to C. 3. Set the right link of B to point to C. X P
29
29 Single-Right Rotation - Result C2 C2 B A C B A Initial Configuration Final Configuration parent
30
30 AVL Trees balance factor of a node is -2 If the balance factor of a node is -2, right sub-tree has a path that is too long this means the right sub-tree has a path that is too long balance factor of the right child is -1, long path is the right sub-tree of the right child Then if the balance factor of the right child is -1, this means that the long path is the right sub-tree of the right child simple left rotation of the right child In this case, a simple left rotation of the right child around the original node will solve the imbalance
31
31 Single-Left Rotation - Right subtree too long C F N Initial Configuration Final Configuration P C (-2) F N P
32
32 Single-Left Rotation C F N 1. Reset the link from the parent of C to F 2. Set the right link of C equal to the left link of F. 3. Set the left link of F to point to C. 1. Reset the link from the parent of C to F 2. Set the right link of C equal to the left link of F. 3. Set the left link of F to point to C. P
33
33 Single-Left Rotation C F N 1. Reset the link from the parent of C to F 2. Set the right link of C equal to the left link of F. (which is null, for this example) 3. Set the left link of F to point to C. 1. Reset the link from the parent of C to F 2. Set the right link of C equal to the left link of F. (which is null, for this example) 3. Set the left link of F to point to C. P
34
34 Single-Left Rotation C F N X 1. Reset the link from the parent of C to F Set the right link of C equal to the left link of F. 2. Set the right link of C equal to the left link of F. 3. Set the left link of F to point to C. 1. Reset the link from the parent of C to F Set the right link of C equal to the left link of F. 2. Set the right link of C equal to the left link of F. 3. Set the left link of F to point to C. P Represents the left link of F (null) Could have a value
35
35 Single-Left Rotation C F N 1. Reset the link from the parent of C to F 2. Set the right link of C equal to the left link of F. Set the left link of F to point to C. 3. Set the left link of F to point to C. 1. Reset the link from the parent of C to F 2. Set the right link of C equal to the left link of F. Set the left link of F to point to C. 3. Set the left link of F to point to C. P
36
36 Single-Left Rotation C F N Initial Configuration Final Configuration C (-2) F N parent
37
37 AVL Trees If the balance factor of a node is 2, this means the left sub-tree has a path that is too long Then if the balance factor of the left child is -1, this means that the long path is the right sub-tree of the left child In this case, a leftright rotation will solve the imbalance
38
38 Double-Right Rotation(left-right rotation) M (2) F H M F H Initial Configuration Final Configuration P Parent
39
39 Double-Right Rotation(left-right rotation) M F H Reset the link from the parent of F(M) to H 1. Reset the link from the parent of F(M) to H 2. Set the left link of H to point to F. 3. Reset the link from the parent of M to H 4. Set the right link of H to point to M. Reset the link from the parent of F(M) to H 1. Reset the link from the parent of F(M) to H 2. Set the left link of H to point to F. 3. Reset the link from the parent of M to H 4. Set the right link of H to point to M. P M points to H We need to rotate left around F so M points to H
40
40 Double-Right Rotation (left-right rotation) M F H 1. Reset the link from the parent of F (M)to H 2. Set the left link of H to point to F. 3. Reset the link from the parent of M to H 4. Set the right link of H to point to M. 1. Reset the link from the parent of F (M)to H 2. Set the left link of H to point to F. 3. Reset the link from the parent of M to H 4. Set the right link of H to point to M. P M points to H
41
41 Double-Right Rotation (left-right rotation) M F H 1. Reset the link from the parent of F to H Set the left link of H to point to F. 2. Set the left link of H to point to F. 3. Reset the link from the parent of M to H 4. Set the right link of H to point to M. 1. Reset the link from the parent of F to H Set the left link of H to point to F. 2. Set the left link of H to point to F. 3. Reset the link from the parent of M to H 4. Set the right link of H to point to M. P We now need a right rotation around H
42
42 Double-Right Rotation(left-right rotation) B 1. Reset the link from the parent of F to H 2. Set the left link of H to point to F. Reset the link from the parent of M (P) to H 3. Reset the link from the parent of M (P) to H 4. Set the right link of H to point to M. 1. Reset the link from the parent of F to H 2. Set the left link of H to point to F. Reset the link from the parent of M (P) to H 3. Reset the link from the parent of M (P) to H 4. Set the right link of H to point to M. M F H P Make P reference H Make P reference H
43
43 Double-Right Rotation (left-right rotation) M F H 1. Reset the link from the parent of F to H 2. Set the left link of H to point to F. 3. Reset the link from the parent of M to H 4. Set the right link of H to point to M. 1. Reset the link from the parent of F to H 2. Set the left link of H to point to F. 3. Reset the link from the parent of M to H 4. Set the right link of H to point to M. P
44
44 Final Configuation Tree rebalanced F H P M
45
45 L (-2) P1P1 S O M F 1.RIGHT- LEFT ROTATION Another implementation point P. left to right child of M point P. left to right child of M L(-2) P1 S M F 2. Then POINT PARENT OF P (L) TO M p.left
46
46 L(-2) M-2 P O S-0 F POINT PARENT OF P TO M Still unbalanced, need to rotate left around the M. RIGHT- LEFT ROTATION point P. left to right child of M point P. left to right child of M
47
47 L1 M0 P O S0 F Rotate Left
48
48 AVL Trees If the balance factor of a node is -2, this means the right sub- tree has a path that is too long Then if the balance factor of the right child is 1, this means that the long path is the left sub-tree of the left child In this case, a rightleft (Double Left) rotation will solve the imbalance Same as leftright in reverse.
49
49 Building an AVL tree P0 P0 Insert Elements: P,J,B,D,Z,M,O,N,A,G,E
50
50 Building an AVL tree P1 P1 J0 J0 Insert Elements: P,J,B,D,Z,M,O,N,A,G,E
51
51 Building an AVL tree P2 P2 J1 J1 B0 B0 Insert Elements: P,J,B,D,Z,M,O,N,A,G,E
52
52 Building an AVL tree P2 P2 J1 J1 B0 B0 Insert Elements: P,J,B,D,Z,M,O,N,A,G,E The new item (B) is in the left subtree of a left child(J) of a node that has balance factor 2 (P) We need to apply a single-right rotation The new item (B) is in the left subtree of a left child(J) of a node that has balance factor 2 (P) We need to apply a single-right rotation
53
53 Building an AVL tree J0 J0 P0 P0 B0 B0 Insert Elements: P,J,B,D,Z,M,O,N,A,G,E
54
54 Building an AVL tree J1 J1 P0 P0 B -1 D0 D0 Insert Elements: P,J,B,D,Z,M,O,N,A,G,E
55
55 Building an AVL tree J0 J0 P -1 B -1 D0 D0 Insert Elements: P,J,B,D,Z,M,O,N,A,G,E Z0 Z0
56
56 Building an AVL tree J0 J0 P0 P0 B -1 D0 D0 Insert Elements: P,J,B,D,Z,M,O,N,A,G,E Z0 Z0 M0 M0
57
57 Building an AVL tree J1 J1 P1 P1 B -1 D0 D0 Insert Elements: P,J,B,D,Z,M,O,N,A,G,E Z0 Z0 M -1 O0 O0
58
58 Building an AVL tree J -2 P2 P2 B -1 D0 D0 I nsert Elements : P,J,B,D,Z,M,O,N,A,G,E Z0 Z0 M -2 O1 O1 N0 N0
59
59 Building an AVL tree J -2 P2 P2 B -1 D0 D0 Insert Elements: P,J,B,D,Z,M,O,N,A,G,E Z0 Z0 M -2 O1 O1 N0 N0 The new item (N) is in the left subtree of a right child(O) of a node that has balance factor -2 (M) We need to apply a rightleft rotation The new item (N) is in the left subtree of a right child(O) of a node that has balance factor -2 (M) We need to apply a rightleft rotation
60
60 Building an AVL tree J -2 P2 P2 B -1 D0 D0 Insert Elements: P,J,B,D,Z,M,O,N,A,G,E Z0 Z0 M -2 O0 O0 N First we need a right rotation around the O and then a left rotation around the M
61
61 Building an AVL tree J -1 P1 P1 B -1 D0 D0 Insert Elements : P,J,B,D,Z,M,O,N,A,G,E Z0 Z0 N0 N0 O0 O0 M0 M0 Then a left rotation around the M
62
62 Building an AVL tree J -1 P1 P1 B0 B0 D0 D0 Insert Elements: P,J,B,D,Z,M,O,N,A,G,E Z0 Z0 N0 N0 O0 O0 M0 M0 A0 A0
63
63 Building an AVL tree J0 J0 P -1 B -1 D -1 Insert Elements: P,J,B,D,Z,M,O,N,A,G,E Z0 Z0 N0 N0 O0 O0 M0 M0 A0 A0 G0 G0
64
64 Building an AVL tree J1 J1 P1 P1 B -2 D -2 Insert Elements: P,J,B,D,Z,M,O,N,A,G,E Z0 Z0 N0 N0 O0 O0 M0 M0 A0 A0 G1 G1 E0 E0 What rotation is necessary?
65
65 Building an AVL tree J1 J1 P1 P1 B -2 D -2 Insert Elements: P,J,B,D,Z,M,O,N,A,G,E Z0 Z0 N0 N0 O0 O0 M0 M0 A0 A0 G1 G1 E0 E0 The new item (E) is in the left subtree of a right child(G) of a node that has balance factor -2 (D) We need to apply a rightleft rotation The new item (E) is in the left subtree of a right child(G) of a node that has balance factor -2 (D) We need to apply a rightleft rotation First a right rotation around the G
66
66 Building an AVL tree J1 J1 P1 P1 B -2 D -2 Insert Elements: P,J,B,D,Z,M,O,N,A,G,E Z0 Z0 N0 N0 O0 O0 M0 M0 A0 A0 G1 G1 E0 E0 We execute a right rotation around the G and then a left around the D
67
67 Building an AVL tree J0 J0 P1 P1 B -1 E0 E0 Insert Elements: P,J,B,D,Z,M,O,N,A,G,E Z0 Z0 N0 N0 O0 O0 M0 M0 A0 A0 G0 G0 D0 D0 We execute a right rotation around the G and then a left around the D
68
68 Rotations These were simple examples and were done when building a tree. In the homework, we will look at another way to rotate in the manner of the next slide when the tree already exists and needs to be fixed.
69
69 A leftright rotation - Easy way Rotate left around the 5Rotate right around the 7
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.