0% found this document useful (0 votes)
64 views30 pages

CS214 DS2022 Lec 11 Trees Part4 Balancing AVL Delete

Here are the steps to remove 24 and 20 from the AVL tree: 1. Remove 24 by replacing it with its right child 25. This causes the tree to become unbalanced. 2. Perform a right rotation at node 13 to balance the tree. 3. Remove 20 by replacing it with its left child 15. This also causes the tree to become unbalanced. 4. Perform a left rotation at node 10 to balance the tree again. The final balanced AVL tree after removing 24 and 20 is: 13 10 15 7 30 25 36

Uploaded by

azer elsaied
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views30 pages

CS214 DS2022 Lec 11 Trees Part4 Balancing AVL Delete

Here are the steps to remove 24 and 20 from the AVL tree: 1. Remove 24 by replacing it with its right child 25. This causes the tree to become unbalanced. 2. Perform a right rotation at node 13 to balance the tree. 3. Remove 20 by replacing it with its left child 15. This also causes the tree to become unbalanced. 4. Perform a left rotation at node 10 to balance the tree again. The final balanced AVL tree after removing 24 and 20 is: 13 10 15 7 30 25 36

Uploaded by

azer elsaied
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

CS214 – Data Structures

Lecture 11: Trees IV

Slides by
Mohamed El-Ramly, PhD
Basheer Youssef, PhD
Instructor
Cherry Ahmed
Agenda
1. Introduction to Binary Trees
2. Implementing Binary Trees
3. Searching Binary Search Trees
4. Tree Traversal …1. Breadth-First ….2. Depth-First
5. Insertion
6. Deletion by Copying
7. Balanced Trees
8. Heaps / Heap Sort

2
Exercises
• What is the minimum number of nodes in an AVL tree of
height 15?

Minimum number of nodes in AVL Tree of height H


is given by a recursive relation:
N(H) = N(H-1) + N(H-2) + 1
where N(0) = 1 and N(1) = 2

• Show the result of inserting 2, 1, 4, 5, 9, 3, 6, 7 into an


initially empty AVL tree.

3
Deletion

• Delete a node x as in ordinary binary search tree. Note


that the last node deleted is a leaf or a node with 1 child.
• Then trace the path from the new leaf towards the root.
• For each node x encountered, check if heights of left(x)
and right(x) differ by at most 1. If yes, proceed to
parent(x). If not, perform an appropriate rotation at x.
There are 4 cases as in the case of insertion.
• For deletion, after we perform a rotation at x, we may
have to perform a rotation at some ancestor of x. Thus,
we must continue to trace the path until we reach the
root.

4
Deletion

• On closer examination: the single rotations for deletion


can be divided into 4 cases (instead of 2 cases)
• Two cases for rotate with left child
• Two cases for rotate with right child

5
Single rotations in deletion
In both figures, a node is deleted in subtree C, causing the height to drop
to h. The height of y is h+2. When the height of subtree A is h+1, the
height of B can be h or h+1. Fortunately, the same single rotation can
correct both cases.

6
rotate with left child
Single rotations in deletion
In both figures, a node is deleted in subtree A, causing the height to drop
to h. The height of y is h+2. When the height of subtree C is h+1, the
height of B can be h or h+1. A single rotation can correct both cases.

7
rotate with right child
Rotations in deletion

• There are 4 cases for single rotations, but we do not


need to distinguish among them.
• There are exactly two cases for double rotations (as in
the case of insertion)
• Therefore, we can reuse exactly the same procedure for
insertion to determine which rotation to perform

8
9
AVL Examples
Tree Tool – You must try it
https://www.cs.usfca.edu/~galles/visualizatio
n/AVLtree.html

10
AVL Tree Example:
• Insert 14, 17, 11, 7, 53, 4, 13 into an empty AVL tree

14

11 17

7 53

11
AVL Tree Example:
• Insert 14, 17, 11, 7, 53, 4, 13 into an empty AVL tree

14

7 17

4 11 53

13

12
AVL Tree Example:
• Now insert 12

14

7 17

4 11 53

13

12

13
AVL Tree Example:
• Now insert 12

14

7 17

4 11 53

12

13

14
AVL Tree Example:
• Now the AVL tree is balanced.

14

7 17

4 12 53

11 13

15
AVL Tree Example:
• Now insert 8

14

7 17

4 12 53

11 13

16
AVL Tree Example:
• Now insert 8

14

7 17

4 11 53

8 12

13

17
AVL Tree Example:
• Now the AVL tree is balanced.

14

11 17

7 12 53

4 8 13

18
AVL Tree Example:
• Now remove 53

14

11 17

7 12 53

4 8 13

19
AVL Tree Example:
• Now remove 53, unbalanced

14

11 17

7 12

4 8 13

20
AVL Tree Example:
• Balanced! Remove 11

11

7 14

4 8 12 17

13

21
AVL Tree Example:
• Remove 11, replace it with the largest in its left branch

7 14

4 12 17

13

22
AVL Tree Example:
• Remove 8, unbalanced

4 14

12 17

13

23
AVL Tree Example:
• Remove 8, unbalanced

4 12

14

13 17

24
AVL Tree Example:
• Balanced!!

12

7 14

4 13 17

25
In Class Exercises

• Build an AVL tree with the following values:


15, 20, 24, 10, 13, 7, 30, 36, 25

26
15, 20, 24, 10, 13, 7, 30, 36, 25
20

15
15 24
20
10
24

13

20 20

13 24 15 24

10 15 13

10
27
15, 20, 24, 10, 13, 7, 30, 36, 25

20
13

13 24 10 20

10 15 7 15 24

7 30

13 36

10 20

7 15 30

24 36

28
15, 20, 24, 10, 13, 7, 30, 36, 25

13 13

10 20 10 20

7 15 30 7 15 24

24 36 30

25 13 25 36

10 24

7 20 30

15 25 36

29
Remove 24 and 20 from the AVL tree.

13 13

10 24 10 20

7 20 30 7 15 30

15 25 36 25 36

13 13

10 30 10 15

7 15 36 7 30

25 25 36 30

You might also like