![Page 1: DATA STRUCTURES USING ‘C’ · AVL Tree is… • named after Adelson-Velskii and Landis • the first dynamically balanced trees to be propose • Binary search tree with balance](https://reader035.vdocuments.site/reader035/viewer/2022081606/5e6e7cbec4db8038bc3330d6/html5/thumbnails/1.jpg)
DATA STRUCTURES USING ‘C’
![Page 2: DATA STRUCTURES USING ‘C’ · AVL Tree is… • named after Adelson-Velskii and Landis • the first dynamically balanced trees to be propose • Binary search tree with balance](https://reader035.vdocuments.site/reader035/viewer/2022081606/5e6e7cbec4db8038bc3330d6/html5/thumbnails/2.jpg)
AVL TREES
![Page 3: DATA STRUCTURES USING ‘C’ · AVL Tree is… • named after Adelson-Velskii and Landis • the first dynamically balanced trees to be propose • Binary search tree with balance](https://reader035.vdocuments.site/reader035/viewer/2022081606/5e6e7cbec4db8038bc3330d6/html5/thumbnails/3.jpg)
AVL Tree is…
• named after Adelson-Velskii and Landis• the first dynamically balanced trees to be
propose• Binary search tree with balance condition
in which the sub-trees of each node can differ by at most 1 in their height
![Page 4: DATA STRUCTURES USING ‘C’ · AVL Tree is… • named after Adelson-Velskii and Landis • the first dynamically balanced trees to be propose • Binary search tree with balance](https://reader035.vdocuments.site/reader035/viewer/2022081606/5e6e7cbec4db8038bc3330d6/html5/thumbnails/4.jpg)
Definition of a balanced tree
• Ensure the depth = O(log N)• Take O(log N) time for searching, insertion,
and deletion• Every node must have left & right sub-trees
of the same height
![Page 5: DATA STRUCTURES USING ‘C’ · AVL Tree is… • named after Adelson-Velskii and Landis • the first dynamically balanced trees to be propose • Binary search tree with balance](https://reader035.vdocuments.site/reader035/viewer/2022081606/5e6e7cbec4db8038bc3330d6/html5/thumbnails/5.jpg)
An AVL tree has the following properties:
1. Sub-trees of each node can differ by at most 1 in their height
2. Every sub-trees is an AVL tree
![Page 6: DATA STRUCTURES USING ‘C’ · AVL Tree is… • named after Adelson-Velskii and Landis • the first dynamically balanced trees to be propose • Binary search tree with balance](https://reader035.vdocuments.site/reader035/viewer/2022081606/5e6e7cbec4db8038bc3330d6/html5/thumbnails/6.jpg)
AVL tree?
YESEach left sub-tree has height 1 greater than each right sub-tree
NOLeft sub-tree has height 3, but right sub-tree has height 1
![Page 7: DATA STRUCTURES USING ‘C’ · AVL Tree is… • named after Adelson-Velskii and Landis • the first dynamically balanced trees to be propose • Binary search tree with balance](https://reader035.vdocuments.site/reader035/viewer/2022081606/5e6e7cbec4db8038bc3330d6/html5/thumbnails/7.jpg)
Insertion and Deletions
• It is performed as in binary search trees• If the balance is destroyed, rotation(s) is
performed to correct balance• For insertions, one rotation is sufficient• For deletions, O(log n) rotations at most are
needed
![Page 8: DATA STRUCTURES USING ‘C’ · AVL Tree is… • named after Adelson-Velskii and Landis • the first dynamically balanced trees to be propose • Binary search tree with balance](https://reader035.vdocuments.site/reader035/viewer/2022081606/5e6e7cbec4db8038bc3330d6/html5/thumbnails/8.jpg)
Single Rotation
left sub-tree is two leveldeeper than the right sub-tree
move ① up a level and② down a level
![Page 9: DATA STRUCTURES USING ‘C’ · AVL Tree is… • named after Adelson-Velskii and Landis • the first dynamically balanced trees to be propose • Binary search tree with balance](https://reader035.vdocuments.site/reader035/viewer/2022081606/5e6e7cbec4db8038bc3330d6/html5/thumbnails/9.jpg)
Double Rotation
Left sub-tree is two leveldeeper than the right sub-tree
Move ② up two levels and ③ down a level
![Page 10: DATA STRUCTURES USING ‘C’ · AVL Tree is… • named after Adelson-Velskii and Landis • the first dynamically balanced trees to be propose • Binary search tree with balance](https://reader035.vdocuments.site/reader035/viewer/2022081606/5e6e7cbec4db8038bc3330d6/html5/thumbnails/10.jpg)
Insertion
Insert 6
Imbalance at 8Perform rotation with 7
![Page 11: DATA STRUCTURES USING ‘C’ · AVL Tree is… • named after Adelson-Velskii and Landis • the first dynamically balanced trees to be propose • Binary search tree with balance](https://reader035.vdocuments.site/reader035/viewer/2022081606/5e6e7cbec4db8038bc3330d6/html5/thumbnails/11.jpg)
![Page 12: DATA STRUCTURES USING ‘C’ · AVL Tree is… • named after Adelson-Velskii and Landis • the first dynamically balanced trees to be propose • Binary search tree with balance](https://reader035.vdocuments.site/reader035/viewer/2022081606/5e6e7cbec4db8038bc3330d6/html5/thumbnails/12.jpg)
Deletion
Delete 4
Imbalance at 3Perform rotation with 2
Imbalance at 5Perform rotation with 8
![Page 13: DATA STRUCTURES USING ‘C’ · AVL Tree is… • named after Adelson-Velskii and Landis • the first dynamically balanced trees to be propose • Binary search tree with balance](https://reader035.vdocuments.site/reader035/viewer/2022081606/5e6e7cbec4db8038bc3330d6/html5/thumbnails/13.jpg)
![Page 14: DATA STRUCTURES USING ‘C’ · AVL Tree is… • named after Adelson-Velskii and Landis • the first dynamically balanced trees to be propose • Binary search tree with balance](https://reader035.vdocuments.site/reader035/viewer/2022081606/5e6e7cbec4db8038bc3330d6/html5/thumbnails/14.jpg)
Key Points
• AVL tree remain balanced by applying rotations, therefore it guarantees O(log N)search time in a dynamic environment
• Tree can be re-balanced in at most O(log N)time