avl trees avl trees avl trees avl trees avl trees avl trees avl trees

yatakonakiran2 52 views 25 slides Aug 31, 2024
Slide 1
Slide 1 of 25
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25

About This Presentation

avl trees


Slide Content

Data Structures: A Pseudocode Approach with C, Second Edition 1
Objectives
Upon completion you will be able to:
• Explain the differences between a BST and an AVL tree
• Insert, delete, and process nodes in an AVL tree
• Understand the operation of the AVL tree ADT
AVL Search TreesAVL Search Trees

Data Structures: A Pseudocode Approach with C, Second Edition 2
AVL Tree Basic Concepts
• While BST is simple and easy to understand, it has one major
problem which is not balanced.
• In 1968, two Russian mathematicians, G.M. Adelson-Velskii and
E.M. Landis, created the balanced binary tree structure that is
AVL tree.
• An AVL tree is a BST that is guaranteed to always be balanced.
• An AVL tree is a binary tree that either is
1. empty or
2. consists of two AVL subtrees, T
L and T
R , whose heights
differ by no more than 1
| H
L – H
R | ≤ 1

Data Structures: A Pseudocode Approach with C, Second Edition 3

Data Structures: A Pseudocode Approach with C, Second Edition 4
8-1 AVL Tree Basic Concepts
• AVL Tree Balance Factor
• Balancing Trees

Data Structures: A Pseudocode Approach with C, Second Edition 5
AVL Tree Balance Factor
The balance factor = H
L – H
R

The balance factor for any node in AVL tree must be +1, 0, -1

The descriptive identifiers are as follows:

LH for left high (+1) indicates the left subtree is higher than the right
subtree.

EH for even high (0) indicates the subtrees are the same height.

RH for right high (-1) indicates the left subtree is shorter than the right
subtree.

Data Structures: A Pseudocode Approach with C, Second Edition 6

Data Structures: A Pseudocode Approach with C, Second Edition 7
Balancing Trees
AVL trees are balanced by rotating nodes either to the left or
to the right
Four cases that require rebalancing. All unbalanced trees fall
into one of these four cases:
1. Left of left: A subtree of a tree that is left high has also
become left high.
2. Right of right: A subtree of a tree that is right high has also
become right high.
3. Right of left: A subtree of a tree that is left high become
right high.
4. Left of right: A subtree of a tree that is right high become
left high.

Data Structures: A Pseudocode Approach with C, Second Edition 8

Data Structures: A Pseudocode Approach with C, Second Edition 9
(continued)

Data Structures: A Pseudocode Approach with C, Second Edition 10
Case 1:
Left of Left

Data Structures: A Pseudocode Approach with C, Second Edition 11
Case 2:
Right of Right

Data Structures: A Pseudocode Approach with C, Second Edition 12
Case 3:
Right of Left

Data Structures: A Pseudocode Approach with C, Second Edition 13
Case 4:
Left of Right

Data Structures: A Pseudocode Approach with C, Second Edition 14
AVL Tree Implementations
Insertion and deletion in an AVL tree follow the same basic rules Insertion and deletion in an AVL tree follow the same basic rules
for insertion and deletion in a BST. The difference lies in the for insertion and deletion in a BST. The difference lies in the
tree balancing, which occurs as we back out of the tree. In this tree balancing, which occurs as we back out of the tree. In this
section we develop the insertion and deletion algorithms for a section we develop the insertion and deletion algorithms for a
AVL tree, including algorithms to balance the tree.AVL tree, including algorithms to balance the tree.
•Insert into AVL Tree
•AVL Tree Delete Algorithm

Data Structures: A Pseudocode Approach with C, Second Edition 15

Data Structures: A Pseudocode Approach with C, Second Edition 16

Data Structures: A Pseudocode Approach with C, Second Edition 17

Data Structures: A Pseudocode Approach with C, Second Edition 18

Data Structures: A Pseudocode Approach with C, Second Edition 19

Data Structures: A Pseudocode Approach with C, Second Edition 20

Data Structures: A Pseudocode Approach with C, Second Edition 21

Data Structures: A Pseudocode Approach with C, Second Edition 22

Data Structures: A Pseudocode Approach with C, Second Edition 23

Data Structures: A Pseudocode Approach with C, Second Edition 24

Data Structures: A Pseudocode Approach with C, Second Edition 25
Tags