Unit 2
Unit 2
Lecture # 7&8
Brute Force
Brute-force algorithm
Step 1 Align pattern at beginning of text
Step 2 Moving from left to right, compare each character of
pattern to the corresponding character in text until
• all characters are found to match (successful search); or
• a mismatch is detected
Step 3 While pattern is not found and the text is not yet
exhausted, realign pattern one position to the right and
repeat Step 2
Examples of Brute-Force String Matching
1. Pattern: 001011
Text: 10010101101001100101111010
2. Pattern: happy
Text: It is never too late to have a
happy childhood.
Closest-Pair Problem
Brute-force algorithm
Compute the distance between every pair of
distinct points
and return the indexes of the points for which
the distance is the smallest.
Closest-Pair Brute-Force Algorithm (cont.)
• Weaknesses
– rarely yields efficient algorithms
– some brute-force algorithms are unacceptably slow
– not as constructive as some other design techniques
Exhaustive Search
A brute force solution to a problem involving
search for an element with a special property,
usually among combinatorial objects such as
permutations, combinations, or subsets of a set.
Method:
– generate a list of all potential solutions to the problem in a
systematic manner (see algorithms in Sec. 5.4)
Lecture #9&10
Divide-and-Conquer
a problem of size n
(instance)
subproblem 1 subproblem 2
of size n/2 of size n/2
a solution to a solution to
subproblem 1 subproblem 2
• Split array A[0..n-1] into about equal halves and make copies
of each half in arrays B and C
• Sort arrays B and C recursively
• Merge sorted arrays B and C into array A as follows:
• Repeat the following until no elements remain in one of the
arrays:
• compare the first elements in the remaining unprocessed
portions of the arrays
• copy the smaller of the two into A, while incrementing
the index indicating the unprocessed portion of that array
• Once all elements in one of the arrays are processed, copy
the remaining unprocessed elements from the other array
into A.
Pseudocode of Mergesort
Pseudocode of Merge
8 3 2 9 7 1 5 4
8 3 2 9 71 5 4
8 3 2 9 7 1 5 4
The non-recursive
version of Mergesort
3 8 2 9 1 7 4 5 starts from merging
single elements into
2 3 8 9 1 4 5 7 sorted pairs.
1 2 3 4 5 7 8 9
Analysis of Mergesort
A[i]p A[i]p
• Exchange the pivot with the last element in the first (i.e., )
subarray — the pivot is now in its final position
• Sort the two subarrays recursively
Partitioning Algorithm
or i > r
< or j = l
5 3 1 9 8 2 4 7
2 3 1 4 5 8 9 7
1 2 3 4 5 7 8 9
1 2 3 4 5 7 8 9
1 2 3 4 5 7 8 9
1 2 3 4 5 7 8 9
Analysis of Quicksort
• Best case: split in the middle — Θ(n log n)
• Worst case: sorted array! — Θ(n2) T(n) = T(n-1) + Θ(n)
• Average case: random arrays — Θ(n log n)
• Improvements:
• better pivot selection: median of three partitioning
• switch to insertion sort on small subfiles
• elimination of recursion
These combine to 20-25% improvement
l 0; r n-1
while l r do
m (l+r)/2
if K = A[m] return m
else if K < A[m] r m-1
else l m+1
return -1
Analysis of Binary Search
• Time efficiency
• worst-case recurrence: Cw (n) = 1 + Cw( n/2 ), Cw (1) = 1
solution: Cw(n) = log2(n+1)
TL TR
A = 12345678901357986429 B = 87654321284820912836
2135 4014
• Brute-force algorithm
c00 c01 a00 a01 b00 b01
= *
c10 c11 a10 a11 b10 b11
m1 + m4 - m5 + m7 m3 + m5
=
m2 + m4 m1 + m3 - m2 + m6
• m1 = (a00 + a11) * (b00 + b11)
• m2 = (a10 + a11) * b00
• m3 = a00 * (b01 - b11)
• m4 = a11 * (b10 - b00) 7 multiplications
• m5 = (a00 + a01) * b11
18 additions
• m6 = (a10 - a00) * (b00 + b01)
• m7 = (a01 - a11) * (b10 + b11)
Strassen’s Matrix Multiplication
M1 + M4 - M5 + M7 M3 + M5
=
M2 + M4 M1 + M3 - M2 + M6
Formulas for Strassen’s Algorithm
M1 = (A00 + A11) (B00 + B11)
Lecture #12
Substitution method
T (n) = 4T (n / 2) + n
4c ( n / 2) 3 + n
= ( c / 2) n 3 + n
= cn3 − ((c / 2)n3 − n) desired – residual
cn3 desired
whenever (c/2)n3 – n 0, for
example, if c 2 and n 1.
residual
4
Example (continued)
5
Example (continued)
8
Example of recursion tree
9
Example of recursion tree
10
Example of recursion tree
11
Example of recursion tree
Q(1)
12
Example of recursion tree
Q(1)
13
Example of recursion tree
Q(1)
14
Example of recursion tree
…
Q(1)
15
Example of recursion tree
…
Q(1) Total = n 2
( ( ) +( )
1 + 16 + 16
5 5 2 5 3
16
+ )
= Q(n2) geometric series
16
Recursion Tree
17
Recursion Tree
18
Recursion Tree
19
Recursion Tree
20
Recursion Tree
21
Recursion Tree
22
Recursion Tree
23
Recursion Tree
24
Recursion Tree
25
The master method
26
Three common cases
27
Three common cases
29
Examples
30
Examples
32
Examples
Lecture #13
Divide-and-Conquer
a problem of size n
(instance)
subproblem 1 subproblem 2
of size n/2 of size n/2
a solution to a solution to
subproblem 1 subproblem 2
Step 0 Sort the points by x (list one) and then by y (list two).
Step 1 Divide the points given into two subsets S1 and S2 by a vertical
line x = c so that half the points lie to the left or on the line and half
the points lie to the right or on the line.
Closest Pair by Divide-and-Conquer (cont.)
Step 2 Find recursively the closest pairs for the left and right
subsets.
Convex hull: smallest convex set that includes given points. An O(n^3)
bruteforce time is given in Levitin, Ch 3.
Assume points are sorted by x-coordinate values
Identify extreme points P1 and P2 (leftmost and rightmost)
Compute upper hull recursively:
find point Pmax that is farthest away from line P1P2
compute the upper hull of the points to the left of line P1Pmax
compute the upper hull of the points to the left of line PmaxP2
Compute lower hull in a similar manner
Pmax
P2
P1
Efficiency of Convex hull Algorithm
Finding point farthest away from line P1P2 can be done in linear time
Time efficiency: T(n) = T(x) + T(y) + T(z) + T(v) + O(n),
where x + y + z +v <= n.
worst case: Θ(n2) T(n) = T(n-1) + O(n)
average case: Θ(n) (under reasonable assumptions about
distribution of points given)
Iteration i. Repeatedly swap element i with the one to its left if smaller.
Property. After ith iteration, a[0] through a[i] contain first i+1
elements in ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
Value 2.78 7.42 0.56 1.12 1.17 0.32 6.21 4.42 3.14 7.71
Iteration 0: step 0.
Insertion Sort
Iteration i. Repeatedly swap element i with the one to its left if smaller.
Property. After ith iteration, a[0] through a[i] contain first i+1
elements in ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
Value 2.78 7.42 0.56 1.12 1.17 0.32 6.21 4.42 3.14 7.71
Iteration 1: step 0.
Insertion Sort
Iteration i. Repeatedly swap element i with the one to its left if smaller.
Property. After ith iteration, a[0] through a[i] contain first i+1
elements in ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
Value 2.78 0.56
7.42 7.42
0.56 1.12 1.17 0.32 6.21 4.42 3.14 7.71
Iteration 2: step 0.
Insertion Sort
Iteration i. Repeatedly swap element i with the one to its left if smaller.
Property. After ith iteration, a[0] through a[i] contain first i+1
elements in ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
Value 2.78 2.78
0.56 0.56 7.42 1.12 1.17 0.32 6.21 4.42 3.14 7.71
Iteration 2: step 1.
Insertion Sort
Iteration i. Repeatedly swap element i with the one to its left if smaller.
Property. After ith iteration, a[0] through a[i] contain first i+1
elements in ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
Value 0.56 2.78 7.42 1.12 1.17 0.32 6.21 4.42 3.14 7.71
Iteration 2: step 2.
Insertion Sort
Iteration i. Repeatedly swap element i with the one to its left if smaller.
Property. After ith iteration, a[0] through a[i] contain first i+1
elements in ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
Value 0.56 2.78 1.12
7.42 7.42
1.12 1.17 0.32 6.21 4.42 3.14 7.71
Iteration 3: step 0.
Insertion Sort
Iteration i. Repeatedly swap element i with the one to its left if smaller.
Property. After ith iteration, a[0] through a[i] contain first i+1
elements in ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
Value 0.56 1.12
2.78 2.78
1.12 7.42 1.17 0.32 6.21 4.42 3.14 7.71
Iteration 3: step 1.
Insertion Sort
Iteration i. Repeatedly swap element i with the one to its left if smaller.
Property. After ith iteration, a[0] through a[i] contain first i+1
elements in ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
Value 0.56 1.12 2.78 7.42 1.17 0.32 6.21 4.42 3.14 7.71
Iteration 3: step 2.
Insertion Sort
Iteration i. Repeatedly swap element i with the one to its left if smaller.
Property. After ith iteration, a[0] through a[i] contain first i+1
elements in ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
Value 0.56 1.12 2.78 1.17
7.42 7.42
1.17 0.32 6.21 4.42 3.14 7.71
Iteration 4: step 0.
Insertion Sort
Iteration i. Repeatedly swap element i with the one to its left if smaller.
Property. After ith iteration, a[0] through a[i] contain first i+1
elements in ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
Value 0.56 1.12 1.17
2.78 2.78
1.17 7.42 0.32 6.21 4.42 3.14 7.71
Iteration 4: step 1.
Insertion Sort
Iteration i. Repeatedly swap element i with the one to its left if smaller.
Property. After ith iteration, a[0] through a[i] contain first i+1
elements in ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
Value 0.56 1.12 1.17 2.78 7.42 0.32 6.21 4.42 3.14 7.71
Iteration 4: step 2.
Insertion Sort
Iteration i. Repeatedly swap element i with the one to its left if smaller.
Property. After ith iteration, a[0] through a[i] contain first i+1
elements in ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
Value 0.56 1.12 1.17 2.78 0.32
7.42 7.42
0.32 6.21 4.42 3.14 7.71
Iteration 5: step 0.
Insertion Sort
Iteration i. Repeatedly swap element i with the one to its left if smaller.
Property. After ith iteration, a[0] through a[i] contain first i+1
elements in ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
Value 0.56 1.12 1.17 0.32
2.78 2.78
0.32 7.42 6.21 4.42 3.14 7.71
Iteration 5: step 1.
Insertion Sort
Iteration i. Repeatedly swap element i with the one to its left if smaller.
Property. After ith iteration, a[0] through a[i] contain first i+1
elements in ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
Value 0.56 1.12 0.32
1.17 1.17
0.32 2.78 7.42 6.21 4.42 3.14 7.71
Iteration 5: step 2.
Insertion Sort
Iteration i. Repeatedly swap element i with the one to its left if smaller.
Property. After ith iteration, a[0] through a[i] contain first i+1
elements in ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
Value 0.56 0.32
1.12 1.12
0.32 1.17 2.78 7.42 6.21 4.42 3.14 7.71
Iteration 5: step 3.
Insertion Sort
Iteration i. Repeatedly swap element i with the one to its left if smaller.
Property. After ith iteration, a[0] through a[i] contain first i+1
elements in ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
Value 0.56 0.56
0.32 0.32 1.12 1.17 2.78 7.42 6.21 4.42 3.14 7.71
Iteration 5: step 4.
Insertion Sort
Iteration i. Repeatedly swap element i with the one to its left if smaller.
Property. After ith iteration, a[0] through a[i] contain first i+1
elements in ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
Value 0.32 0.56 1.12 1.17 2.78 7.42 6.21 4.42 3.14 7.71
Iteration 5: step 5.
Insertion Sort
Iteration i. Repeatedly swap element i with the one to its left if smaller.
Property. After ith iteration, a[0] through a[i] contain first i+1
elements in ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
Value 0.32 0.56 1.12 1.17 2.78 6.21
7.42 7.42
6.21 4.42 3.14 7.71
Iteration 6: step 0.
Insertion Sort
Iteration i. Repeatedly swap element i with the one to its left if smaller.
Property. After ith iteration, a[0] through a[i] contain first i+1
elements in ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
Value 0.32 0.56 1.12 1.17 2.78 6.21 7.42 4.42 3.14 7.71
Iteration 6: step 1.
Insertion Sort
Iteration i. Repeatedly swap element i with the one to its left if smaller.
Property. After ith iteration, a[0] through a[i] contain first i+1
elements in ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
Value 0.32 0.56 1.12 1.17 2.78 6.21 4.42
7.42 7.42
4.42 3.14 7.71
Iteration 7: step 0.
Insertion Sort
Iteration i. Repeatedly swap element i with the one to its left if smaller.
Property. After ith iteration, a[0] through a[i] contain first i+1
elements in ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
Value 0.32 0.56 1.12 1.17 2.78 4.42
6.21 6.21
4.42 7.42 3.14 7.71
Iteration 7: step 1.
Insertion Sort
Iteration i. Repeatedly swap element i with the one to its left if smaller.
Property. After ith iteration, a[0] through a[i] contain first i+1
elements in ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
Value 0.32 0.56 1.12 1.17 2.78 4.42 6.21 7.42 3.14 7.71
Iteration 7: step 2.
Insertion Sort
Iteration i. Repeatedly swap element i with the one to its left if smaller.
Property. After ith iteration, a[0] through a[i] contain first i+1
elements in ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
Value 0.32 0.56 1.12 1.17 2.78 4.42 6.21 3.14
7.42 7.42
3.14 7.71
Iteration 8: step 0.
Insertion Sort
Iteration i. Repeatedly swap element i with the one to its left if smaller.
Property. After ith iteration, a[0] through a[i] contain first i+1
elements in ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
Value 0.32 0.56 1.12 1.17 2.78 4.42 3.14
6.21 6.21
3.14 7.42 7.71
Iteration 8: step 1.
Insertion Sort
Iteration i. Repeatedly swap element i with the one to its left if smaller.
Property. After ith iteration, a[0] through a[i] contain first i+1
elements in ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
Value 0.32 0.56 1.12 1.17 2.78 3.14
4.42 4.42
3.14 6.21 7.42 7.71
Iteration 8: step 2.
Insertion Sort
Iteration i. Repeatedly swap element i with the one to its left if smaller.
Property. After ith iteration, a[0] through a[i] contain first i+1
elements in ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
Value 0.32 0.56 1.12 1.17 2.78 3.14 4.42 6.21 7.42 7.71
Iteration 8: step 3.
Insertion Sort
Iteration i. Repeatedly swap element i with the one to its left if smaller.
Property. After ith iteration, a[0] through a[i] contain first i+1
elements in ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
Value 0.32 0.56 1.12 1.17 2.78 3.14 4.42 6.21 7.42 7.71
Iteration 9: step 0.
Insertion Sort
Iteration i. Repeatedly swap element i with the one to its left if smaller.
Property. After ith iteration, a[0] through a[i] contain first i+1
elements in ascending order.
Array index 0 1 2 3 4 5 6 7 8 9
Value 0.32 0.56 1.12 1.17 2.78 3.14 4.42 6.21 7.42 7.71
Lecture #14
Graph Traversal
Topics
• Depth First Search (DFS)
• Breadth First Search (BFS)
Graph Search (traversal)
• How do we search a graph?
– At a particular vertices, where shall we go next?
• Two common framework:
▪ the depth-first search (DFS)
▪ the breadth-first search (BFS) and
– In DFS, go as far as possible along a single path until
reach a dead end (a vertex with no edge out or no
neighbor unexplored) then backtrack
– In BFS, one explore a graph level by level away
(explore all neighbors first and then move on)
Depth-First Search (DFS)
• The basic idea behind this algorithm is that it traverses the
graph using recursion
– Go as far as possible until you reach a deadend
– Backtrack to the previous path and try the next branch
– The graph below, started at node a, would be visited in the
following order: a, b, c, g, h, i, e, d, f, j
a c d
b
e f
g h i j
DFS: Color Scheme
source
vertex
DFS Example
source
vertex
d f
1 | | |
| |
| | |
DFS Example
source
vertex
d f
1 | | |
2 | |
| | |
DFS Example
source
vertex
d f
1 | | |
2 | |
3 | | |
DFS Example
source
vertex
d f
1 | | |
2 | |
3 | 4 | |
DFS Example
source
vertex
d f
1 | | |
2 | |
3 | 4 5 | |
DFS Example
source
vertex
d f
1 | | |
2 | |
3 | 4 5 | 6 |
DFS Example
source
vertex
d f
1 | | |
2 | 7 |
3 | 4 5 | 6 |
DFS Example
source
vertex
d f
1 | 8 | |
2 | 7 |
3 | 4 5 | 6 |
DFS Example
source
vertex
d f
1 | 8 | |
2 | 7 9 |
3 | 4 5 | 6 |
DFS Example
source
vertex
d f
1 | 8 | |
2 | 7 9 |10
3 | 4 5 | 6 |
DFS Example
source
vertex
d f
1 | 8 |11 |
2 | 7 9 |10
3 | 4 5 | 6 |
DFS Example
source
vertex
d f
1 |12 8 |11 |
2 | 7 9 |10
3 | 4 5 | 6 |
DFS Example
source
vertex
d f
1 |12 8 |11 13|
2 | 7 9 |10
3 | 4 5 | 6 |
DFS Example
source
vertex
d f
1 |12 8 |11 13|
2 | 7 9 |10
3 | 4 5 | 6 14|
DFS Example
source
vertex
d f
1 |12 8 |11 13|
2 | 7 9 |10
3 | 4 5 | 6 14|15
DFS Example
source
vertex
d f
1 |12 8 |11 13|16
2 | 7 9 |10
3 | 4 5 | 6 14|15
DFS: Algorithm
DFS(G)
▪ for each vertex u in V,
▪ color[u]=white; p[u]=NIL
▪ time=0;
▪ for each vertex u in V
▪ if (color[u]=white)
▪ DFS-VISIT(u)
DFS: Algorithm (Cont.)
DFS-VISIT(u)
source
▪ color[u]=gray; vertex
▪ time = time + 1;
▪ d[u] = time;
▪ for each v in Adj(u) do
▪ if (color[v] = white)
▪ p[v] = u;
▪ DFS-VISIT(v);
▪ color[u] = black;
▪ time = time + 1; f[u]= time;
DFS: Complexity Analysis
And DFS_VISIT scans all the edges which causes cost of O(E)
• Topological Sort
• Strongly Connected Component
Breadth-first Search (BFS)
E F G H
I J K L
M N O P
0 A B C D
E F G H
I J K L
M N O P
0 A B C D
1
1 E F 1 G H
I J K L
M N O P
0 A B C D
1 2
1 E F 1 G H
2 I J K L
M N O P
0 A B C D 3
1 2
1 E F 1 G 3 H
3
2 I J K L
3 M N 3 O P
0 A B C D 3
1 2
1 E F 1 G 3 H 4
3
2 I J K 4 L 4
3 M N 3 O P
0 A B C D 3
1 2
1 E F 1 G 3 H 4
3
2 I J K 4 L 4
5
3 M N 3 O P 5
0 A B C D 3
1 2
1 E F 1 G 3 H 4
3
2 I J K 4 L 4
5
3 M N 3 O P 5
0 A B C D 3
1 2
1 E F 1 G 3 H 4
3
2 I J K 4 L 4
5
3 M N 3 O P 5
BFS: Algorithm
BFS(G, s)
▪ For each vertex u in V – {s},
▪ color[u] = white;
▪ d[u] = infinity;
▪ p[u] = NIL
▪ color[s] = GRAY; d[s] = 0; p[s] = NIL; Q = empty queue
▪ ENQUEUE(Q,s)
▪ while (Q not empty)
▪ u = DEQUEUE(Q)
▪ for each v Adj[u]
▪ if color[v] = WHITE
▪ then color[v] = GREY
▪ d[v] = d[u] + 1; p[v] = u
▪ ENQUEUE(Q, v);
▪ color[u] = BLACK;
▪
Example
r s t u
v w x y
Example
r s t u
0
v w x y
Q: s
Example
r s t u
1 0
1
v w x y
Q: w r
Example
r s t u
1 0 2
1 2
v w x y
Q: r t x
Example
r s t u
1 0 2
2 1 2
v w x y
Q: t x v
Example
r s t u
1 0 2 3
2 1 2
v w x y
Q: x v u
Example
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: v u y
Example
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: u y
Example
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: y
Example
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: Ø
BFS: Complexity Analysis
Runtime?
Computing a mode
• A mode is a value that occurs most often in a list of
numbers
– e.g. the mode of [5, 1, 5, 7, 6, 5, 7] is 5
– If several different values occur most often any of them can be
considered the mode
• “Count Sort” approach: (assumes all values > 0; what if
they aren’t?)
max max(A)
freq[1..max] 0
for each x A
freq[x] +=1 Runtime?
mode freq[1]
for i 2 to max
if freq[i] > freq[mode] mode i
return mode
Presort Computing Mode
Sort A
i0
modefrequency 0
while i ≤ n-1
runlength 1; runvalue A[i]
while i+runlength ≤ n-1 and A[i+runlength] = runvalue
runlength += 1
if runlength > modefrequency
modefrequency runlength
modevalue runvalue
i+= runlength
return modevalue
AVL Animation Link
https://www.cs.usfca.edu/~galles/visualization/AVLtree.
html
Binary Search Tree - Best Time
1 4
2
2 5
3
1 3
4
4 Is this “balanced”?
5
2 6 6
1 3 5 7 7
Approaches to balancing trees
• Don't balance
– May end up with some nodes very deep
• Strict balance
– The tree must always be balanced perfectly
• Pretty good balance
– Only allow a little out of balance
• Adjust on access
– Self-adjusting
Balancing Binary Search Trees
1 5 8 1 4 6 9
AVL - Good but not Perfect Balance
height of node = h
balance factor = hleft-hright
empty height = -1
Node Heights after Insert 7
2 2
6 6
1 2 1 1
4 9 4 8
0 0 1 0 0 0 0
1 5 8 1 5 7 9
0
7
Insertions in AVL Trees
Consider a valid
AVL subtree
j
k h
h
h
Z
X Y
AVL Insertion: Outside Case
j Inserting into X
destroys the AVL
property at node j
k h
h+1 h Z
Y
X
AVL Insertion: Outside Case
j Do a “right rotation”
k h
h+1 h Z
Y
X
Single right rotation
j Do a “right rotation”
k h
h+1 h Z
Y
X
Outside Case Completed
h+1
j
h h
X Y Z
AVL property has been restored!
AVL Insertion: Inside Case
Consider a valid
AVL subtree
j
k h
h h Z
X Y
AVL Insertion: Inside Case
Inserting into Y
destroys the j Does “right rotation”
restore balance?
AVL property
at node j
k h
h h+1 Z
X
Y
AVL Insertion: Inside Case
“Right rotation”
k does not restore
balance… now k is
h j out of balance
X h+1
h
Z
Y
AVL Insertion: Inside Case
h h+1 Z
X
Y
AVL Insertion: Inside Case
Y = node i and
subtrees V and W
j
k h
h
i h+1 Z
X h or h-1
V W
AVL Insertion: Inside Case
j We will do a left-right
“double rotation” . . .
k
i Z
X
V W
Double rotation : first rotation
i
k Z
W
X V
Double rotation : second
rotation
i
k Z
W
X V
Double rotation : second
rotation
k j
h h
h or h-1
X V W Z
Implementation
balance (1,0,-1)
key
left right
No need to keep the height; just the difference in height, i.e. the
balance factor; this has to be modified on the path of insertion even if
you don’t perform rotations
Once you have performed a rotation (single or double) you won’t need to
go back up the tree
Single Rotation
V W
Insertion in AVL Trees
2
20 Insert 5, 40
0 1
10 30
0 0
25 35
Example of Insertions in an AVL
Tree
2
3
20 20
1 1 1 2
10 30 10 30
0 0 0 1
0 0
5 25 35 5 25 35
0
40
Now Insert 45
Single rotation (outside case)
3
3
20 20
1 2 1 2
10 30 10 30
0 0 2
0 0 1
5 25 35 5 25 40
0 0
40 35 45
Imbalance 1
0
45 Now Insert 34
Double rotation (inside case)
3
3
20 20
1 3 1 2
10 30 10 35
0 0 2
0 1
Imbalance 1
5 25 40 5 30 40
0
1
35 45 0 0 25 34 45
Insertion of 34 0
34
AVL Tree Deletion