0 views

Uploaded by vvsprasad

b

- Algorithm.docx
- c Programing
- Tan Van - Binomial Heap Presentation
- Heuristics for minimum length geometric embedding of free trees onto a point set
- Data Structures
- Perceptual Guidelines for Creating Rectangular Treemaps
- avl_tree
- Boris Ederov.bachelor.merkle Tree Traversal Techniques
- 10.1.1.148.8841
- CS9215-SET I.pdf
- 74 Algorithms
- Problem Page 199 New
- lecture08.ppt
- c%26ds
- Data Structure Lec24 Handout
- Graph based Approach and Clustering of Patterns (GACP) for Sequential Pattern Mining
- Data Structure 2
- CSE-II-I-DS-UNIT-7.pdf
- proiect otm
- Daa Assignment

You are on page 1of 9

CSE 326: Data Structures

Left and right subtrees of every node

have heights differing by at most 1

AVL Trees

• Ensures small depth

› Will prove this by showing that an AVL tree of height

Neva Cherniavsky h must have a lot of (i.e. O(2h)) nodes

Summer 2006 • Easy to maintain

› Using single and double rotations

Structural properties

1. Binary tree property 10

(0,1, or 2 children) 8 •How do we track the

2. Heights of left and right balance?

5 15

subtrees of every node 5 11 •How do we detect

differ by at most 1 imbalance?

Result: 2 9 20

2 6 10 12

•How do we restore

Worst case depth of any

node is: O(log n) 7 17 30 balance?

4 7 9 13 14

Ordering property

15

› Same as for BST

Circle One:

6

1 7 11

Not AVL • M(h) = minimum number of nodes in an

10 12 AVL tree of height h.

6 • Basis h

4 8 AVL

› M(0) = 1, M(1) = 2

• Induction

1 5 7 11

› M(h) = M(h-1) + M(h-2) + 1 h-2

Not AVL h-1

3 • Solution

2 › M(h) > φh - 1 (φ = (1+√5)/2 ≈ 1.62)

Student Activity If not AVL, put a box around nodes where AVL property is violated.

1

Proof that M(h) > φh Height of an AVL Tree

• Basis: M(0) = 1 > φ0 -1, M(1) = 2 > φ1-1 • M(h) > φh (φ ≈ 1.62)

• Induction step. • Suppose we have N nodes in an AVL

M(h) = M(h-1) + M(h-2) + 1 tree of height h.

> (φh-1 - 1) + (φh-2 - 1) + 1 › N > M(h)

= φh-2 (φ +1) - 1 › N > φh - 1

= φh - 1 (φ2 = φ +1) › logφ(N+1) > h (relatively well balanced

tree!!)

2 2 balance factor

2 3 1-(-1) = 2

6 6 6 6

1 0 1 1 1 1 1 2

4 9 4 9 4 9 4 9

0 0 0 0 0 0 0 0 0 0 1 -1

1 5 1 5 8 1 5 7 1 5 8

0

7

height of node = h height of node = h

balance factor = hleft-hright balance factor = hleft-hright

empty height = -1 empty height = -1

Trees Tree

• Insert operation may cause balance factor 2 2

to become 2 or –2 for some node 6 6

1 2 1 1

› only nodes on the path from insertion point to 4 9 4 8

root node have possibly changed in height 0 0 1 0 0 0 0

node by node, updating heights 0

› If a new balance factor (the difference hleft- 7

hright) is 2 or –2, adjust tree by rotation around

the node

2

Insertions in AVL Trees Bad Case #1

Let the node that needs rebalancing be α.

Insert(6)

There are 4 cases:

Outside Cases (require single rotation) : Insert(3)

1. Insertion into left subtree of left child of α. Insert(1)

2. Insertion into right subtree of right child of α.

Inside Cases (require double rotation) :

3. Insertion into right subtree of left child of α.

4. Insertion into left subtree of right child of α.

The rebalancing is performed through four

separate rotation algorithms.

AVL Property violated at this node (x)

2 1

Consider a valid

AVL subtree

j

6 3

3

1

0 0 k h

1 6

1

0 h

h

Z

X Y

Single Rotation:

1. Rotate between x and child

j Inserting into X j Do a “rotation from left”

destroys the AVL

property at node j

k h k h

h+1 h Z h+1 h Z

Y Y

X X

3

Single rotation from left Outside Case Completed

j k

“rotation from left” done!

(“rotation from right” is

mirror symmetric)

k h

h+1

j

h+1 h Z h h

Y X Y Z

X AVL property has been restored!

15

Bad Case #2

5 20

3 10 17 21 Insert(1)

2 4 Insert(6)

1 15 Insert(3)

3 20

2 5 17 21

1 4 10

AVL Property violated at this node (x) AVL Insertion: Inside Case

2 2

1 1 1 Consider a valid

AVL subtree

j

3

1 1

6 3

0

0 0 k h

0 1 6

3 6 h h Z

Double Rotation X Y

1. Rotate between x’s child and grandchild

2. Rotate between x and x’s new child

4

AVL Insertion: Inside Case AVL Insertion: Inside Case

Inserting into Y

destroys the j Does “rotation from left”

restore balance? k “Rotation from left”

does not restore

AVL property balance… now k is

at node j

k h

h j out of balance

h h+1 Z X h+1

h

X Z

Y Y

Consider the structure

of subtree Y… j Y = node i and

subtrees V and W

j

k h

k h

h h+1 Z h

i h+1 Z

X X h or h-1

Y

V W

2

j We will do a

“double rotation” . . .

j

1

k i

i Z k Z

X W

V W X V

5

Double rotation : second Double rotation : second

rotation rotation

j right rotation complete

i i restored to the universe

k Z k j

W h h or h-1

h

X V X V W Z

15 15

8 17 8 17

4 10 16 6 10 16

4

3 6

3 5

5

15 15

8 17 6 17

6 10 16 4 8 16

4 3 5 10

3 5

Single Rotation

1. Rotate between x and child

Double Rotation

1. Rotate between x’s child and grandchild

2. Rotate between x and x’s new child

Student Activity

6

Single and Double Rotations:

Inserting what integer values Insertion into AVL tree

would cause the tree to need a:

5 11

2. Hang new node there with this key

2 7 13

3. Search back up the path for imbalance

2. double rotation? 0 3

4. If there is an imbalance:

case #1: Perform single rotation and exit

Both rotations keep the subtree height unchanged.

Hence only one rotation is sufficient!

Student Activity

3

3 Insert(33) 10

Insert(3) 10

2 2

1 2 5 15

5 15

1 0 0 1

0 0 0 1 2 9 12 20

2 9 12 20

0 0 0

0 0 3 17 30

17 30 Unbalanced?

3

Single Rotation3 Hard Insert (Bad Case #2)

10 10 3

Insert(18) 10

2 3 2 2

5 15 5 20 2 2

5 15

1 0 0 2 1 0 1 1

2 9 12 20 2 9 15 30 1 0 0 1

2 9 12 20

0 0 1 0 0 0 0

3 17 30 3 12 17 33 0 0 0

Unbalanced? 3 17 30

0

33 How to fix?

7

Double Rotation (Step #1)

Single Rotation (oops!) 3 3

3 3 10 10

10 10

2 3 2 3

2 3 2 3 5 15 5 15

5 15 5 20

1 0 0 2 1 0 0 2

1 0 0 2 1 0 2 0 2 9 12 20 2 9 12 17

2 9 12 20 2 9 15 30

0 1 0 0 1

0 1 0 0 0 1 3 17 30 3 20

3 17 30 3 12 17

0 0 0

0 0 18 18 30

18 18

3 3

10 10

• Balance condition:

For every node x, -1 ≤ balance(x) ≤ 1

2 3 2 2 › Strong enough : Worst case depth is O(log n)

5 15 5 17 › Easy to maintain : one single or double rotation

1 0 0 2 1 0 1 1

2 9 12 17 2 9 15 20

• Guaranteed O(log n) running time for

0 1 0 0 0 0 › Find ?

3 20 3 12 18 30 › Insert ?

0 0 › Delete ?

18 30 › buildTree ?

• Could use different balance conditions,

• What extra info did we maintain in each different ways to maintain balance, different

node? guarantees on running time, …

• Why aren’t AVL trees perfect?

• Many other balanced BST data structures

• Where were rotations performed? › Red-Black trees

› AA trees

› Splay Trees

› 2-3 Trees

• How did we locate this node? › B-Trees

› …

8

Implementation Single Rotation

RotateFromRight(n : reference node pointer) {

balance (1,0,-1) p : node pointer;

key p := n.right; n

n.right := p.left;

left right p.left := n;

n := p

}

Y Z

• Class participation • Similar to insertion

• Implement Double Rotation in two lines.

› Rotations and double rotations needed to

DoubleRotateFromRight(n : reference node pointer) {

????

rebalance

n

} › Imbalance may propagate upward so that

many rotations may be needed.

V W

Arguments for AVL trees:

1. Search is O(log N) since AVL trees are always well balanced.

2. The height balancing adds no more than a constant factor to the

speed of insertion, deletion, and find.

1. Difficult to program & debug; more space for height info.

2. Asymptotically faster but rebalancing costs time.

3. Most large searches are done in database systems on disk and use

other structures (e.g. B-trees).

4. May be OK to have O(N) for a single operation if total run time for

many consecutive operations is fast (e.g. Splay trees).

- Algorithm.docxUploaded bysaurab_baraik
- c ProgramingUploaded byJulie Short
- Tan Van - Binomial Heap PresentationUploaded byRupesh Kashyap
- Heuristics for minimum length geometric embedding of free trees onto a point setUploaded byJournal of Computing
- Data StructuresUploaded byHenryHai Nguyen
- Perceptual Guidelines for Creating Rectangular TreemapsUploaded byaivuk
- avl_treeUploaded byPapori Borgohain
- Boris Ederov.bachelor.merkle Tree Traversal TechniquesUploaded byShiva prasad
- 10.1.1.148.8841Uploaded byAnonymous RrGVQj
- CS9215-SET I.pdfUploaded bysharmila
- 74 AlgorithmsUploaded byShivanshuSingh
- Problem Page 199 NewUploaded byWildatus Sholichah
- lecture08.pptUploaded byraysubha123
- c%26dsUploaded bytheresa.painter
- Data Structure Lec24 HandoutUploaded byJʋŋaiɗ Aĸɓar
- Graph based Approach and Clustering of Patterns (GACP) for Sequential Pattern MiningUploaded byAshish D Patel
- Data Structure 2Uploaded byWaqas Ahmed
- CSE-II-I-DS-UNIT-7.pdfUploaded bybskc_sunil
- proiect otmUploaded byCryss Bobo
- Daa AssignmentUploaded byAnonymous BOreSF
- Data Structure Lec27 HandoutUploaded byJʋŋaiɗ Aĸɓar
- Chapter 18Uploaded byKenDaniswara
- UntitledUploaded byapi-72498964
- Conference ProceedingUploaded byAnand Singh
- IEEEXplorer-1Uploaded bygaythree
- OurUploaded byjithinraju
- Fast Data Collection in Tree-Based Wireless Sensor NetworksUploaded byJAYAPRAKASH
- LabUploaded byHeenaAggarwal
- 4.3 Bounded algorithmUploaded byDuaa Raja
- Class DemonstrationUploaded byKushagra Manik

- SoftwareUploaded byvvsprasad
- 17-18_User Manual- AQIS Application - MODROBS_Version 2.0.CompressedUploaded byM. Sadiq. A. Pachapuri
- data science links.docxUploaded byvvsprasad
- Baby's BreathUploaded byvvsprasad
- 2015-425Uploaded byvvsprasad
- Chapter 1Uploaded byvvsprasad
- Arithmetic OperatorsUploaded byvvsprasad
- dbms-interview.pdfUploaded byvvsprasad
- Collision Resolution TechniquesUploaded byvvsprasad
- Bedroom Bill AugustUploaded byvvsprasad
- bfsdfsUploaded byvvsprasad
- Track ConsignmentUploaded byvvsprasad
- III Cse SullabusUploaded byBala Bhargav
- jnnnnUploaded byvvsprasad
- CreatiofasdfanUploaded byvvsprasad
- networking wirelessUploaded byvvsprasad
- emergency exit in networkingUploaded byvvsprasad
- JNTU Kakinada Cse i &II SyllabusUploaded bykatha_karma
- NewUploaded byPothurajuBorra
- EnglishUploaded byvvsprasad
- WEF IT Advanced Cloud Computing Report 2011Uploaded byNguyễn Tuấn Anh
- GrandUploaded byvvsprasad

- Health Education 6 8Uploaded byIsabel Behrmann
- How to Enable EPEL Repository for RHEL_CentOS 7.X_6.X_5.xUploaded bydanxl007
- Commision on Macroeconomic and Health Bg P2 Oral and Dental DiseasesUploaded byAnonymous 9aIw2a
- AndrewJacksonSlidesOFW7Uploaded byvenkat_09
- Affidavit of Loss(Pasport)Uploaded byAl John
- daily story simile stella snow queenUploaded byapi-300281604
- brochure-kone-e-link_tcm59-18662.pdfUploaded byFelipe Raul Chumpitaz Guevara
- China High Speed Rail Sector and Investment Opportunity AnalysisUploaded byNeeraj Chawla
- KRaHBXAeQzk10v8SYQHbUploaded byStefan Ličanin
- IFE Matrix.docxUploaded byShu Xin Chua
- Sustainability Reporting - The New GRI Standards. | Albert Vilariño Alonso | MediumUploaded byAlbert Vilariño Alonso
- 13-1662 Acadian Contractors - PV-14923A - 09-17-2013Uploaded byMichael Haise
- MGT 401.docUploaded bytanmoy2000
- Urban SlumsUploaded bySiddharth Nath
- Aakash Aiats Aieee 2012 Test-4 SolutionUploaded byblue_l1
- Moore-James-Wanda-1990-Kenya.pdfUploaded bythe missions network
- books_3017_0.pdfUploaded bymanojkhadka23
- IPCAM User Guide 4.1.2Uploaded byjabijabijabi
- Partner DeterminationUploaded bymshabnam
- Shail Shah ResumeUploaded byShailR.Shah
- R12-ITURKA.BAND-C-0001!!MSW-E (1).docUploaded byhoma
- international businessUploaded byKona Ahmed
- FB 09.20.2013Uploaded byMark Reinhardt
- required_info_map.txtUploaded byjohn
- Risk Assessment Plan templateUploaded bysanthurkc
- Thirteen Reason's Why-PlotUploaded byShiela Belle Poppy
- Acute and Early HIV Infection_ Treatment - UpToDateUploaded byNicole Sierra
- EDI StandardsUploaded byGaurav Kumar
- In Re Crystallex - Shareholder Motion to Permit Oppression Claim - 22 January 2018Uploaded byLatin American Herald Tribune
- Large Scale Urban DesignUploaded bybingley