SlideShare a Scribd company logo
AVL Trees
Recall
Motivation: Linked lists and queues work well enough
for most applications, but provide slow service for large
data sets.

3
3

4
4

835
835

837
837

836
836
Ordered insertion takes too long for large sets.
Solution: Trees
4
4
1
1

Binary Trees
provide quick
access to data

7
7
6
6

1
1

9
9

4
4

6
6

O (logN)
O (logN)
vs
vs
O (N)
O (N)

7
7

9
9
15

O(N2)

Why it
Why it
matters
matters

O(N)

# Steps

10

O(log N)

5

0

5

10

# Items

15

20
4
4
1
1

Trees: Balance
Consider an
imbalanced
Binary Search
Tree. Search
performance
degrades
back into a
linked list.

7
7
6
6

The promised
O(log N) will
approach O(N).

9
9
12
12
17
17
Balance
If we had some
way of keeping the
tree “in balance”, our
searches would be
closer to O(log N)

9
9
4
4
1
1

12
12
7
7

17
17

6
6
But how does one
measure balance?
Measuring “Balance”
4
4

1
2
3
4
5

1
1

Perhaps we
measure
the height...

7
7
6
6

9
9
4
4

1
1

9
9
12
12

12
12
7
7

6
6
17
17

…but the height alone does not
give us a indication of imbalance

17
17
Balance: The Key
4
4
1
1

Subtree’s right
child too tall for
its sibling

7
7
6
6

Balance is a
relative
property, not
an absolute
measure...

9
9
4
4
1
1

9
9
12
12

12
12
7
7

6
6
17
17
…as with
all things.

17
17
Measuring “Balance”
Adelson-Velskii and Landis suggested that,
since trees may be defined recursively, balance
may also be determined recursively.

9
9

Internal nodes are
also roots of
subtrees.
If each node’s
subtree has a similar
height, the overall
tree is balanced

4
4
1
1

12
12
7
7

6
6

17
17
Measuring Balance
Thus, the actual numerical height does not determine
balance; rather, it’s the relative height of both
subtrees.

9
9
4
4
H

1 7
1 7

9
9

12
12
17
17

H-2
H-1

6
6
Balanced Tree Test: subtrees differ in height by no more
than one. This recursively holds true for all nodes!
Balance Factor
To measure the balance of a node, there are
two steps:
Record the node height (NH)
--an absolute numeric measure
Record the node’s balance factor (BF)
--a relative measure
The node height alone does not indicate
balance: we have to compare it to the height
of other nodes.
EP
EP
ST
ST

Node / Tree Height
Tree height is easy, Here are three trees.

1
1

H=3

H=1

4
4

9
9
Height?
H=2

12
12
17
17

1
1

7
7
6
6
Node / Tree Heights for AVLs
By convention, a null
reference will return a
height of zero (0).
Therefore:
The Balance Factor of

is Ht of

17
17

= 1 – (0)
= +1

12
12
0
12
12

minus (0)

17
17
0

0
EP
EP
ST
ST

Balance Factor

2
2

Remember that balance is relative.
relative
So we define the Balance Factor (BF) at:
BF = (right subtree height - left subtree height)

9
9

0

+1

12
12

4
4
0

17
17

1
1

+1

0

7
7
6
6

0

-1
Balance Factor
Each node has a balance factor. Remember,
it’s based on (right ht - left ht).
-1

9
9
4
4
1 7
1 7
6
6

+1

12
12
17
17

9
9
4
4

0

12
12

-1

1 7
1 7
6
6

+1
0

17
17

0

(We will note BF outside the node, to avoid confusion with the key
values. Some texts don’t even give the keys, because values are
irrelevant once you have a proper BST.)
Quiz Yourself
Calculate the balance factor for each node in
each tree:

9
9

9
9

4
4

4
4
1 7
1 7

9
9
4
4

12
12

6
6

9
9
4
4
1 7
1 7

12
12
17
17
16
16
Quiz Yourself- Ans
Calculate the balance factor for each node in each tree:
Here, the height is written inside the
node, covering the key value it holds.
Hopefully, this graphical clue helps.

2
2

-1

1
1

1

3
3
2
2

1
1

4
4

0

1
1

1 2
1 2
1
1

4
4

-3

-1

2
2

1

3
3

2

1 1
1 1

2
2
1
1

-1
Using Balance Factors
How do the Balance Factors help?
Recall: Adelson-Velskii and Landis
used a recursive definition
of trees. Our BF measure
+1
is likewise
4
4
recursive.
-2

0

Not
Balanced

0

9
9
4
4

0

1 7
1 7

0

-1

9
9

+1

12
12

-1

0

1 7
1 7
6
6

17
17

0
Balanced

If any BF is > +1 or < -1, there’s imbalance
AVL’s “Weak” Balancing
•

A fully balanced tree guarantees that, for nodes on each level, all
left and right subtrees have the same height, and imposes this
recursively.

•

AVL trees use a weaker balance definition which still maintains the
logarithmic depth requirement:

12
12

– for any node in an AVL tree, the
height of the left and right nodes
differs by at most 1.
•

For example, this is a valid AVL
tree, but not strictly balanced:

8
8
4
4
6
6

10
10

16
16
2

17
17
Addressing Imbalance
If we build a tree from scratch, and check nodes
for imbalance after a new insertion, we can guard
against imbalance.

Q:
So what do we do when the tree is unbalanced?

A:
Rotate the tree.
Identifying Offending Scenarios
The key to fixing an unbalanced tree is knowing what
caused it to break. Let’s inductively look at what can
cause a good tree to go bad.

Given an AVL balanced tree:
Two possible left insertions could
break balance:
#1
Insertion to
left subtree
of left child

-2
-1

1
1

9
9
4
4
0

#2
+1

9
9
4
4

-1

0

-2

9
9
4
4
7
7

0

Insertion to
right subtree
of left child
#1

Patterns
of Problems

-2

Insertion to
99
left subtree -1
44
of left child
11

#2

-2
99

+1

44
0

0

77

Insertion to
right subtree
of left child

… and the mirror of these insertions

#3
Insertion to
left subtree
of right child

+2

9
9 -1
11
11
0
10
10

#4

+2

Insertion to
right subtree
of right child

9
9 +1
11
11
0
13
13
#1

-2

Insertion to
99
left subtree -1
44
of left child
11

#2

Imbalance found
regardless of height

0

9
9

matches

-2

-2

+1

99

44
0

77

Insertion to
right subtree
of left child

H-1

H
H+1

#3
+2
99
Insertion to
left subtree
of right child

11 -1
11
10
10 0

4
4
(H-1) - (H+1) = -2

#4
+2
99
11
Insertion to 11 +1
right subtree
of right child
13 0
13

Given our recursive definition of trees, we know these four scenarios
are complete. The same insertion into a larger AVL balanced tree
does not create a new category.
#1

#2

-2

Insertion to
99
left subtree -1
44
of left child
11

0

#3
+2
99
Insertion to
left subtree
of right child

11 -1
11
10
10 0

Let’s look at two
insertions that
cause imbalance.
Recall they are
mirrors.

They both deal with
“outside” insertions
on the tree’s exterior
face: the left-left and
right-right insertions.

-2

+1

99

44
0

77

Insertion to
right subtree
of left child

#4
+2
99
11
Insertion to 11 +1
right subtree
of right child
13 0
13
Single Rotation
#1
Insertion to
left subtree
of left child

-2
-1

1
1

9
9
4
4

These insertions can
be cured by swapping
parent and child . . .

0
#4

. . . provided you
maintain search
order. (An AVL tree is
a BST)

+2

Insertion to
right subtree
of right child

9
9 +1
11
11
0
13
13
Single Rotation: Right

#1

0

-2
-1

Insertion to
left subtree
of left child

9
9
4
4

1
1

0

X
X

1
1

9
9

0

A single rotation round the
unbalanced node restores AVL
balance in all cases

W
W

W
W
A

0

4
4

X
X
B

C

A B

C
9
9

9
9

4
4
1
1

9
9

4
4
1
1

4
4
9
9

1
1

9
9

Think of rotations - Imagine the joints at the
‘4’ node starting to flex and articulate.
#4

Single Rotation: Left
+2

Insertion to
right subtree
of right child

0

9
9 +1
11
11
0
13
13

11
11
0
0
9 13
9 13

A similar rotation is used for right-right
insertions that cause an imbalance

X
X

W
W
W
W

X
X

A
B

C

A

B

C
Word of caution
9
9

X
W
A

3
3

6
6

4
4

7
7
5
5

C

11
11
14
14

The single rotations
are simple, but you
have to keep track
of child references.

B

1
1

9
9

W
A

1
1

4
4
3
3

B

5
5

6
6

X

7
7

C

11
11
14
14
#1

#2

-2

Insertion to
99
left subtree -1
44
of left child
11

0

#3
+2
99
Insertion to
left subtree
of right child

11 -1
11
10
10 0

Let’s look at the
other two insertions
causing problems.
Recall they are
mirrors.

They both deal with
“inside” insertions on
the tree’s interior:
right-left and left-right

-2

+1

99

44
0

77

Insertion to
right subtree
of left child

#4
+2
99
11
Insertion to 11 +1
right subtree
of right child
13 0
13
Double Rotation
#2
These insertions are
not solved with a
single rotation.

+1

-2

9
9
4
4
7
7

#3
Insertion to
left subtree
of right child

+2

9
9 -1
11
11
0
10
10

0

Insertion to
right subtree
of left child

Instead, two single
rotations, left-right
and right-left are
performed.
Why Doesn’t Single Rotation Work?

X
X

Single
Right

W
W
A

C
B

W
W

Rotation
Attempted

X
X
A
B

C

Unbalanced insertions into the “interior” of
the tree remain unbalanced with only a
single rotation
Double Rotation

#3

+2

0

9
9 -1
Insertion to
left subtree
11
11
of right child
0
10
10

10
10
0
0
9 11
9 11
(trivial case)

Y
Y
X
X

X
X
W
W

A

Y
Y
D

B

C

A

W
W
B

C

D
Double Rotation

#4

-2

Insertion to
right subtree
of left child

+1

0

9
9
4
4
7
7

7
7

0

4
4

0

Y
Y
W
W
A

X
X
W
W

X
X
D

B

9
9

C

0

A

Y
Y
B

C

D
Double Rotation
W
A

99

Y
77
44

33
B 55

88
66

Magic

11
11

X

D

X
W

14
14

Step?

A

33

99
66

44

77
55

B

11
11

Y
88

14
14
D

Solved:
Move up X!
W becomes X’s left child
Y becomes X’s right child
W’s right child becomes X’s left child
X’s right child (if any) becomes Y’s left child
Double Rotation Strategy
On a left-right insert, we first rotate left

9
9
4
4

D

7
7

A

7
7

at 4

D

4
4
C

B

C

A

SRR

9
9

SLR

B

7
7
4
4

at 9
A

9
9

B C

D

Double left rotate =
single left rot + single right rot

This places the tree in a position for which we already
have a working solution. (Code reuse!)
Double Rotation Strategy
9
9
11
11
A
10
10

D

B

C

SRR
at 11
A

SLR

9
9
10
10
11
11

10
10
9
9

at 9

B

A
C

11
11

B C

D

Similarly, we perform a right and left rotation on
insertions that went left-right. The first rotation reforms
the tree into an exterior imbalance. We already have a
way of fixing this: single left rotations

D

More Related Content

What's hot

AVL tree ( Balanced Binary Search Tree)-Data Structure
AVL tree ( Balanced Binary Search Tree)-Data StructureAVL tree ( Balanced Binary Search Tree)-Data Structure
AVL tree ( Balanced Binary Search Tree)-Data Structure
Yaksh Jethva
 
Avl trees
Avl treesAvl trees
Avl trees
ppreeta
 
Avl trees
Avl treesAvl trees
Avl trees
amna izzat
 
Data Structure and Algorithms AVL Trees
Data Structure and Algorithms AVL TreesData Structure and Algorithms AVL Trees
Data Structure and Algorithms AVL Trees
ManishPrajapati78
 
AVL Tree
AVL TreeAVL Tree
Computer notes - AVL Tree
Computer notes - AVL TreeComputer notes - AVL Tree
Computer notes - AVL Tree
ecomputernotes
 
AVL Tree
AVL TreeAVL Tree
AVL Tree
Chhatra Thapa
 
Lecture3
Lecture3Lecture3
Lecture3
Ali Baba
 
4. avl
4. avl4. avl
Splay Tree
Splay TreeSplay Tree
Splay tree
Splay treeSplay tree
Splay tree
hina firdaus
 
Lecture 10 data structures and algorithms
Lecture 10 data structures and algorithmsLecture 10 data structures and algorithms
Lecture 10 data structures and algorithms
Aakash deep Singhal
 
Binary tree
Binary treeBinary tree
Binary tree
Afaq Mansoor Khan
 
Binary Trees
Binary TreesBinary Trees
Binary Trees
Sadaf Ismail
 
Tree
TreeTree
Trees
TreesTrees
Trees
9590133127
 
Octal to Hexadecimal and Hexadecimal to Octal
Octal to Hexadecimal  and Hexadecimal to OctalOctal to Hexadecimal  and Hexadecimal to Octal
Octal to Hexadecimal and Hexadecimal to Octal
SRM Institute of Science & Technology, Tiruchirappalli
 
Lecture 11 data structures and algorithms
Lecture 11 data structures and algorithmsLecture 11 data structures and algorithms
Lecture 11 data structures and algorithms
Aakash deep Singhal
 
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
sumitbardhan
 
Tree traversal
Tree traversalTree traversal
Tree traversal
shamim alamgir
 

What's hot (20)

AVL tree ( Balanced Binary Search Tree)-Data Structure
AVL tree ( Balanced Binary Search Tree)-Data StructureAVL tree ( Balanced Binary Search Tree)-Data Structure
AVL tree ( Balanced Binary Search Tree)-Data Structure
 
Avl trees
Avl treesAvl trees
Avl trees
 
Avl trees
Avl treesAvl trees
Avl trees
 
Data Structure and Algorithms AVL Trees
Data Structure and Algorithms AVL TreesData Structure and Algorithms AVL Trees
Data Structure and Algorithms AVL Trees
 
AVL Tree
AVL TreeAVL Tree
AVL Tree
 
Computer notes - AVL Tree
Computer notes - AVL TreeComputer notes - AVL Tree
Computer notes - AVL Tree
 
AVL Tree
AVL TreeAVL Tree
AVL Tree
 
Lecture3
Lecture3Lecture3
Lecture3
 
4. avl
4. avl4. avl
4. avl
 
Splay Tree
Splay TreeSplay Tree
Splay Tree
 
Splay tree
Splay treeSplay tree
Splay tree
 
Lecture 10 data structures and algorithms
Lecture 10 data structures and algorithmsLecture 10 data structures and algorithms
Lecture 10 data structures and algorithms
 
Binary tree
Binary treeBinary tree
Binary tree
 
Binary Trees
Binary TreesBinary Trees
Binary Trees
 
Tree
TreeTree
Tree
 
Trees
TreesTrees
Trees
 
Octal to Hexadecimal and Hexadecimal to Octal
Octal to Hexadecimal  and Hexadecimal to OctalOctal to Hexadecimal  and Hexadecimal to Octal
Octal to Hexadecimal and Hexadecimal to Octal
 
Lecture 11 data structures and algorithms
Lecture 11 data structures and algorithmsLecture 11 data structures and algorithms
Lecture 11 data structures and algorithms
 
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
 
Tree traversal
Tree traversalTree traversal
Tree traversal
 

Similar to AVL Trees

Adelson velskii Landis rotations based on
Adelson velskii Landis rotations based onAdelson velskii Landis rotations based on
Adelson velskii Landis rotations based on
banupriyar5
 
Sienna6bst 120411102353-phpapp02
Sienna6bst 120411102353-phpapp02Sienna6bst 120411102353-phpapp02
Sienna6bst 120411102353-phpapp02
Getachew Ganfur
 
Complete binary tree and heap
Complete binary tree and heapComplete binary tree and heap
Complete binary tree and heap
Nazir Ahmed
 
Binary tree data structure
Binary tree data structureBinary tree data structure
Binary tree data structure
Learning Courses Online
 
irrational number.pdf
irrational number.pdfirrational number.pdf
irrational number.pdf
analeelumaday2
 
AVL Tree.pptx
AVL Tree.pptxAVL Tree.pptx
AVL Tree.pptx
Trad5
 
1.2 Irrational Numbers ppt
1.2 Irrational Numbers ppt1.2 Irrational Numbers ppt
1.2 Irrational Numbers ppt
Sandra Johnson
 
AVL Tree.ppt
AVL Tree.pptAVL Tree.ppt
AVL Tree.ppt
ChiragJoshi59934
 
5220191CS146 Data Structures and AlgorithmsC.docx
5220191CS146 Data Structures and AlgorithmsC.docx5220191CS146 Data Structures and AlgorithmsC.docx
5220191CS146 Data Structures and AlgorithmsC.docx
fredharris32
 
Humming code presentation by Hridoy bepari
Humming code presentation by Hridoy bepariHumming code presentation by Hridoy bepari
Humming code presentation by Hridoy bepari
Hridoy Bepari
 
Sorting in data structures and algorithms , it has all the necessary points t...
Sorting in data structures and algorithms , it has all the necessary points t...Sorting in data structures and algorithms , it has all the necessary points t...
Sorting in data structures and algorithms , it has all the necessary points t...
BhumikaBiyani1
 
Sorting algorithms v01
Sorting algorithms v01Sorting algorithms v01
Sorting algorithms v01
Dusan Vuckovic
 
Leftist heap
Leftist heapLeftist heap
Leftist heap
Shuvro Roy
 
12_prime.pdf
12_prime.pdf12_prime.pdf
Lecture 10 - AVL Trees.pdf
Lecture 10 - AVL Trees.pdfLecture 10 - AVL Trees.pdf
Lecture 10 - AVL Trees.pdf
SanghdipUdrake
 
2 4 Tree
2 4 Tree2 4 Tree
2 4 Tree
vikram singh
 

Similar to AVL Trees (16)

Adelson velskii Landis rotations based on
Adelson velskii Landis rotations based onAdelson velskii Landis rotations based on
Adelson velskii Landis rotations based on
 
Sienna6bst 120411102353-phpapp02
Sienna6bst 120411102353-phpapp02Sienna6bst 120411102353-phpapp02
Sienna6bst 120411102353-phpapp02
 
Complete binary tree and heap
Complete binary tree and heapComplete binary tree and heap
Complete binary tree and heap
 
Binary tree data structure
Binary tree data structureBinary tree data structure
Binary tree data structure
 
irrational number.pdf
irrational number.pdfirrational number.pdf
irrational number.pdf
 
AVL Tree.pptx
AVL Tree.pptxAVL Tree.pptx
AVL Tree.pptx
 
1.2 Irrational Numbers ppt
1.2 Irrational Numbers ppt1.2 Irrational Numbers ppt
1.2 Irrational Numbers ppt
 
AVL Tree.ppt
AVL Tree.pptAVL Tree.ppt
AVL Tree.ppt
 
5220191CS146 Data Structures and AlgorithmsC.docx
5220191CS146 Data Structures and AlgorithmsC.docx5220191CS146 Data Structures and AlgorithmsC.docx
5220191CS146 Data Structures and AlgorithmsC.docx
 
Humming code presentation by Hridoy bepari
Humming code presentation by Hridoy bepariHumming code presentation by Hridoy bepari
Humming code presentation by Hridoy bepari
 
Sorting in data structures and algorithms , it has all the necessary points t...
Sorting in data structures and algorithms , it has all the necessary points t...Sorting in data structures and algorithms , it has all the necessary points t...
Sorting in data structures and algorithms , it has all the necessary points t...
 
Sorting algorithms v01
Sorting algorithms v01Sorting algorithms v01
Sorting algorithms v01
 
Leftist heap
Leftist heapLeftist heap
Leftist heap
 
12_prime.pdf
12_prime.pdf12_prime.pdf
12_prime.pdf
 
Lecture 10 - AVL Trees.pdf
Lecture 10 - AVL Trees.pdfLecture 10 - AVL Trees.pdf
Lecture 10 - AVL Trees.pdf
 
2 4 Tree
2 4 Tree2 4 Tree
2 4 Tree
 

More from Shreyans Pathak

Introduction to 8086 microprocessor
Introduction to 8086 microprocessorIntroduction to 8086 microprocessor
Introduction to 8086 microprocessor
Shreyans Pathak
 
8051 interrupts
8051 interrupts8051 interrupts
8051 interrupts
Shreyans Pathak
 
8051 Timers and Counters
8051 Timers and Counters8051 Timers and Counters
8051 Timers and Counters
Shreyans Pathak
 
8051 Programming Instruction Set
 8051 Programming Instruction Set 8051 Programming Instruction Set
8051 Programming Instruction Set
Shreyans Pathak
 
Introduction state machine
Introduction state machineIntroduction state machine
Introduction state machine
Shreyans Pathak
 
Java packages
Java packagesJava packages
Java packages
Shreyans Pathak
 
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classes
Shreyans Pathak
 

More from Shreyans Pathak (7)

Introduction to 8086 microprocessor
Introduction to 8086 microprocessorIntroduction to 8086 microprocessor
Introduction to 8086 microprocessor
 
8051 interrupts
8051 interrupts8051 interrupts
8051 interrupts
 
8051 Timers and Counters
8051 Timers and Counters8051 Timers and Counters
8051 Timers and Counters
 
8051 Programming Instruction Set
 8051 Programming Instruction Set 8051 Programming Instruction Set
8051 Programming Instruction Set
 
Introduction state machine
Introduction state machineIntroduction state machine
Introduction state machine
 
Java packages
Java packagesJava packages
Java packages
 
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classes
 

Recently uploaded

RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
IreneSebastianRueco1
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
Celine George
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
Dr. Mulla Adam Ali
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
simonomuemu
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Assessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptxAssessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptx
Kavitha Krishnan
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
Bisnar Chase Personal Injury Attorneys
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
Celine George
 

Recently uploaded (20)

RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
Assessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptxAssessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptx
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
 

AVL Trees

  • 2. Recall Motivation: Linked lists and queues work well enough for most applications, but provide slow service for large data sets. 3 3 4 4 835 835 837 837 836 836 Ordered insertion takes too long for large sets.
  • 3. Solution: Trees 4 4 1 1 Binary Trees provide quick access to data 7 7 6 6 1 1 9 9 4 4 6 6 O (logN) O (logN) vs vs O (N) O (N) 7 7 9 9
  • 4. 15 O(N2) Why it Why it matters matters O(N) # Steps 10 O(log N) 5 0 5 10 # Items 15 20
  • 5. 4 4 1 1 Trees: Balance Consider an imbalanced Binary Search Tree. Search performance degrades back into a linked list. 7 7 6 6 The promised O(log N) will approach O(N). 9 9 12 12 17 17
  • 6. Balance If we had some way of keeping the tree “in balance”, our searches would be closer to O(log N) 9 9 4 4 1 1 12 12 7 7 17 17 6 6 But how does one measure balance?
  • 7. Measuring “Balance” 4 4 1 2 3 4 5 1 1 Perhaps we measure the height... 7 7 6 6 9 9 4 4 1 1 9 9 12 12 12 12 7 7 6 6 17 17 …but the height alone does not give us a indication of imbalance 17 17
  • 8. Balance: The Key 4 4 1 1 Subtree’s right child too tall for its sibling 7 7 6 6 Balance is a relative property, not an absolute measure... 9 9 4 4 1 1 9 9 12 12 12 12 7 7 6 6 17 17 …as with all things. 17 17
  • 9. Measuring “Balance” Adelson-Velskii and Landis suggested that, since trees may be defined recursively, balance may also be determined recursively. 9 9 Internal nodes are also roots of subtrees. If each node’s subtree has a similar height, the overall tree is balanced 4 4 1 1 12 12 7 7 6 6 17 17
  • 10. Measuring Balance Thus, the actual numerical height does not determine balance; rather, it’s the relative height of both subtrees. 9 9 4 4 H 1 7 1 7 9 9 12 12 17 17 H-2 H-1 6 6 Balanced Tree Test: subtrees differ in height by no more than one. This recursively holds true for all nodes!
  • 11. Balance Factor To measure the balance of a node, there are two steps: Record the node height (NH) --an absolute numeric measure Record the node’s balance factor (BF) --a relative measure The node height alone does not indicate balance: we have to compare it to the height of other nodes.
  • 12. EP EP ST ST Node / Tree Height Tree height is easy, Here are three trees. 1 1 H=3 H=1 4 4 9 9 Height? H=2 12 12 17 17 1 1 7 7 6 6
  • 13. Node / Tree Heights for AVLs By convention, a null reference will return a height of zero (0). Therefore: The Balance Factor of is Ht of 17 17 = 1 – (0) = +1 12 12 0 12 12 minus (0) 17 17 0 0
  • 14. EP EP ST ST Balance Factor 2 2 Remember that balance is relative. relative So we define the Balance Factor (BF) at: BF = (right subtree height - left subtree height) 9 9 0 +1 12 12 4 4 0 17 17 1 1 +1 0 7 7 6 6 0 -1
  • 15. Balance Factor Each node has a balance factor. Remember, it’s based on (right ht - left ht). -1 9 9 4 4 1 7 1 7 6 6 +1 12 12 17 17 9 9 4 4 0 12 12 -1 1 7 1 7 6 6 +1 0 17 17 0 (We will note BF outside the node, to avoid confusion with the key values. Some texts don’t even give the keys, because values are irrelevant once you have a proper BST.)
  • 16. Quiz Yourself Calculate the balance factor for each node in each tree: 9 9 9 9 4 4 4 4 1 7 1 7 9 9 4 4 12 12 6 6 9 9 4 4 1 7 1 7 12 12 17 17 16 16
  • 17. Quiz Yourself- Ans Calculate the balance factor for each node in each tree: Here, the height is written inside the node, covering the key value it holds. Hopefully, this graphical clue helps. 2 2 -1 1 1 1 3 3 2 2 1 1 4 4 0 1 1 1 2 1 2 1 1 4 4 -3 -1 2 2 1 3 3 2 1 1 1 1 2 2 1 1 -1
  • 18. Using Balance Factors How do the Balance Factors help? Recall: Adelson-Velskii and Landis used a recursive definition of trees. Our BF measure +1 is likewise 4 4 recursive. -2 0 Not Balanced 0 9 9 4 4 0 1 7 1 7 0 -1 9 9 +1 12 12 -1 0 1 7 1 7 6 6 17 17 0 Balanced If any BF is > +1 or < -1, there’s imbalance
  • 19. AVL’s “Weak” Balancing • A fully balanced tree guarantees that, for nodes on each level, all left and right subtrees have the same height, and imposes this recursively. • AVL trees use a weaker balance definition which still maintains the logarithmic depth requirement: 12 12 – for any node in an AVL tree, the height of the left and right nodes differs by at most 1. • For example, this is a valid AVL tree, but not strictly balanced: 8 8 4 4 6 6 10 10 16 16 2 17 17
  • 20. Addressing Imbalance If we build a tree from scratch, and check nodes for imbalance after a new insertion, we can guard against imbalance. Q: So what do we do when the tree is unbalanced? A: Rotate the tree.
  • 21. Identifying Offending Scenarios The key to fixing an unbalanced tree is knowing what caused it to break. Let’s inductively look at what can cause a good tree to go bad. Given an AVL balanced tree: Two possible left insertions could break balance: #1 Insertion to left subtree of left child -2 -1 1 1 9 9 4 4 0 #2 +1 9 9 4 4 -1 0 -2 9 9 4 4 7 7 0 Insertion to right subtree of left child
  • 22. #1 Patterns of Problems -2 Insertion to 99 left subtree -1 44 of left child 11 #2 -2 99 +1 44 0 0 77 Insertion to right subtree of left child … and the mirror of these insertions #3 Insertion to left subtree of right child +2 9 9 -1 11 11 0 10 10 #4 +2 Insertion to right subtree of right child 9 9 +1 11 11 0 13 13
  • 23. #1 -2 Insertion to 99 left subtree -1 44 of left child 11 #2 Imbalance found regardless of height 0 9 9 matches -2 -2 +1 99 44 0 77 Insertion to right subtree of left child H-1 H H+1 #3 +2 99 Insertion to left subtree of right child 11 -1 11 10 10 0 4 4 (H-1) - (H+1) = -2 #4 +2 99 11 Insertion to 11 +1 right subtree of right child 13 0 13 Given our recursive definition of trees, we know these four scenarios are complete. The same insertion into a larger AVL balanced tree does not create a new category.
  • 24. #1 #2 -2 Insertion to 99 left subtree -1 44 of left child 11 0 #3 +2 99 Insertion to left subtree of right child 11 -1 11 10 10 0 Let’s look at two insertions that cause imbalance. Recall they are mirrors. They both deal with “outside” insertions on the tree’s exterior face: the left-left and right-right insertions. -2 +1 99 44 0 77 Insertion to right subtree of left child #4 +2 99 11 Insertion to 11 +1 right subtree of right child 13 0 13
  • 25. Single Rotation #1 Insertion to left subtree of left child -2 -1 1 1 9 9 4 4 These insertions can be cured by swapping parent and child . . . 0 #4 . . . provided you maintain search order. (An AVL tree is a BST) +2 Insertion to right subtree of right child 9 9 +1 11 11 0 13 13
  • 26. Single Rotation: Right #1 0 -2 -1 Insertion to left subtree of left child 9 9 4 4 1 1 0 X X 1 1 9 9 0 A single rotation round the unbalanced node restores AVL balance in all cases W W W W A 0 4 4 X X B C A B C
  • 27. 9 9 9 9 4 4 1 1 9 9 4 4 1 1 4 4 9 9 1 1 9 9 Think of rotations - Imagine the joints at the ‘4’ node starting to flex and articulate.
  • 28. #4 Single Rotation: Left +2 Insertion to right subtree of right child 0 9 9 +1 11 11 0 13 13 11 11 0 0 9 13 9 13 A similar rotation is used for right-right insertions that cause an imbalance X X W W W W X X A B C A B C
  • 29. Word of caution 9 9 X W A 3 3 6 6 4 4 7 7 5 5 C 11 11 14 14 The single rotations are simple, but you have to keep track of child references. B 1 1 9 9 W A 1 1 4 4 3 3 B 5 5 6 6 X 7 7 C 11 11 14 14
  • 30. #1 #2 -2 Insertion to 99 left subtree -1 44 of left child 11 0 #3 +2 99 Insertion to left subtree of right child 11 -1 11 10 10 0 Let’s look at the other two insertions causing problems. Recall they are mirrors. They both deal with “inside” insertions on the tree’s interior: right-left and left-right -2 +1 99 44 0 77 Insertion to right subtree of left child #4 +2 99 11 Insertion to 11 +1 right subtree of right child 13 0 13
  • 31. Double Rotation #2 These insertions are not solved with a single rotation. +1 -2 9 9 4 4 7 7 #3 Insertion to left subtree of right child +2 9 9 -1 11 11 0 10 10 0 Insertion to right subtree of left child Instead, two single rotations, left-right and right-left are performed.
  • 32. Why Doesn’t Single Rotation Work? X X Single Right W W A C B W W Rotation Attempted X X A B C Unbalanced insertions into the “interior” of the tree remain unbalanced with only a single rotation
  • 33. Double Rotation #3 +2 0 9 9 -1 Insertion to left subtree 11 11 of right child 0 10 10 10 10 0 0 9 11 9 11 (trivial case) Y Y X X X X W W A Y Y D B C A W W B C D
  • 34. Double Rotation #4 -2 Insertion to right subtree of left child +1 0 9 9 4 4 7 7 7 7 0 4 4 0 Y Y W W A X X W W X X D B 9 9 C 0 A Y Y B C D
  • 35. Double Rotation W A 99 Y 77 44 33 B 55 88 66 Magic 11 11 X D X W 14 14 Step? A 33 99 66 44 77 55 B 11 11 Y 88 14 14 D Solved: Move up X! W becomes X’s left child Y becomes X’s right child W’s right child becomes X’s left child X’s right child (if any) becomes Y’s left child
  • 36. Double Rotation Strategy On a left-right insert, we first rotate left 9 9 4 4 D 7 7 A 7 7 at 4 D 4 4 C B C A SRR 9 9 SLR B 7 7 4 4 at 9 A 9 9 B C D Double left rotate = single left rot + single right rot This places the tree in a position for which we already have a working solution. (Code reuse!)
  • 37. Double Rotation Strategy 9 9 11 11 A 10 10 D B C SRR at 11 A SLR 9 9 10 10 11 11 10 10 9 9 at 9 B A C 11 11 B C D Similarly, we perform a right and left rotation on insertions that went left-right. The first rotation reforms the tree into an exterior imbalance. We already have a way of fixing this: single left rotations D