SlideShare uma empresa Scribd logo
1 de 37
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

Mais conteúdo relacionado

Mais procurados (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
 

Semelhante a AVL Trees

Data structures trees and graphs - AVL tree.pptx
Data structures trees and graphs - AVL  tree.pptxData structures trees and graphs - AVL  tree.pptx
Data structures trees and graphs - AVL tree.pptxMalligaarjunanN
 
Adelson velskii Landis rotations based on
Adelson velskii Landis rotations based onAdelson velskii Landis rotations based on
Adelson velskii Landis rotations based onbanupriyar5
 
Sienna6bst 120411102353-phpapp02
Sienna6bst 120411102353-phpapp02Sienna6bst 120411102353-phpapp02
Sienna6bst 120411102353-phpapp02Getachew Ganfur
 
Complete binary tree and heap
Complete binary tree and heapComplete binary tree and heap
Complete binary tree and heapNazir Ahmed
 
1.2 Irrational Numbers ppt
1.2 Irrational Numbers ppt1.2 Irrational Numbers ppt
1.2 Irrational Numbers pptSandra Johnson
 
5220191CS146 Data Structures and AlgorithmsC.docx
5220191CS146 Data Structures and AlgorithmsC.docx5220191CS146 Data Structures and AlgorithmsC.docx
5220191CS146 Data Structures and AlgorithmsC.docxfredharris32
 
Humming code presentation by Hridoy bepari
Humming code presentation by Hridoy bepariHumming code presentation by Hridoy bepari
Humming code presentation by Hridoy bepariHridoy 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 v01Dusan Vuckovic
 
Lecture 10 - AVL Trees.pdf
Lecture 10 - AVL Trees.pdfLecture 10 - AVL Trees.pdf
Lecture 10 - AVL Trees.pdfSanghdipUdrake
 

Semelhante a AVL Trees (16)

Data structures trees and graphs - AVL tree.pptx
Data structures trees and graphs - AVL  tree.pptxData structures trees and graphs - AVL  tree.pptx
Data structures trees and graphs - AVL tree.pptx
 
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
 
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
 

Mais de Shreyans Pathak

Introduction to 8086 microprocessor
Introduction to 8086 microprocessorIntroduction to 8086 microprocessor
Introduction to 8086 microprocessorShreyans Pathak
 
8051 Timers and Counters
8051 Timers and Counters8051 Timers and Counters
8051 Timers and CountersShreyans Pathak
 
8051 Programming Instruction Set
 8051 Programming Instruction Set 8051 Programming Instruction Set
8051 Programming Instruction SetShreyans Pathak
 
Introduction state machine
Introduction state machineIntroduction state machine
Introduction state machineShreyans Pathak
 
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classesShreyans Pathak
 

Mais de 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
 

Último

Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfChris Hunter
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfSanaAli374401
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 

Último (20)

Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 

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