- InfoGlue - User Manual for Version 2.5.0
- FaThumb: A facet-based interface for mobile search
- Final Sol
- 10.1.1.30
- Cs 301 Final Term Solved Paper 2
- MELJUN CORTES ALGORITHM_Coping With the Limitations of Algorithm Power_II
- 29review(AVL)
- ho1_prefinals
- thispainedmetowrite
- DS1211
- Predicate Pushdown
- avl supp
- LPMoRS_1994
- New Microsoft Word Document
- Soloviev Phml
- Fast Data Collection in Tree-Based Wireless Sensor Networks
- best job for computer science major by salary potential
- anthony carmichael
- Technical Aptitude Questions.pdf
- Convolutional Codes
- Submitting Future Requests Via Package fnd_request.submit_request
- DAC e Brochure
- moerkerken program 2017
- Data Structures
- DME Process in AP
- Business Process Intelligence
- ploteo y graficacion en f90.pdf
- Technical Questions Placement
- huffman
- modifStoreIUTA
- sql-server-hardware-ebook.pdf
- REEP001
- Pump Parts Dimentional Tolerance
- Pump Parts Dimentional Tolerance
- CBT Nuggets 70 463.docx
- Pump Maintenance Guidelines
- inside-the-sql-server-query-optimizer.pdf
- Testing and Commisioning Procedures for HVAC
- Pump Grouting Article
- Nyaa
- Reep 001
- Bearing Failure on a Boiler Feed Water Pump
- Pump Grouting Article

Fall 2005

AVL-Trees

AVL Trees / Slide 2

**Balanced binary tree
**

**The disadvantage of a binary search tree is that its
**

height can be as large as N-1

This means that the time needed to perform insertion

and deletion and many other operations can be O(N)

in the worst case

We want a tree with small height

A binary tree with N node has height at least (log N)

Thus, our goal is to keep the height of a binary search

tree O(log N)

Such trees are called balanced binary search trees.

Examples are AVL tree, red-black tree.

AVL Trees / Slide 3

AVL tree

Height of a node

The height of a leaf is 1. The height of a null

pointer is zero.

The height of an internal node is the

maximum height of its children plus 1

Note that this definition of height is different from the

one we defined previously (we defined the height of a

leaf as zero previously).

AVL Trees / Slide 4

AVL tree

**An AVL tree is a binary search tree in which
**

**for every node in the tree, the height of the left and
**

right subtrees differ by at most 1.

AVL property

violated here

AVL Trees / Slide 5

AVL tree

**Let x be the root of an AVL tree of height h
**

Let Nh denote the minimum number of nodes in an

AVL tree of height h

Clearly, Ni ≥ Ni-1 by definition

N h N h 1 N h 2 1

We have

2 N h2 1

2 N h2

**By repeated substitution, we
**

obtain the general form

i

N h 2 N h2

**The boundary conditions are: N1=1 and N2 =2. This
**

implies that h = O(log Nh).

Thus, many operations (searching, insertion, deletion)

on an AVL tree will take O(log N) time.

AVL Trees / Slide 6

Rotations

**When the tree structure changes (e.g., insertion or
**

deletion), we need to transform the tree to restore the

AVL tree property.

This is done using single rotations or double rotations.

e.g. Single Rotation

y

x

x

y

C

A

B

B

A

Before Rotation

After Rotation

C

AVL Trees / Slide 7

Rotations

Since an insertion/deletion involves

adding/deleting a single node, this can only

increase/decrease the height of some subtree

by 1

Thus, if the AVL tree property is violated at a

node x, it means that the heights of left(x) ad

right(x) differ by exactly 2.

Rotations will be applied to x to restore the

AVL tree property.

AVL Trees / Slide 8

Insertion

**First, insert the new key as a new leaf just as in
**

ordinary binary search tree

Then trace the path from the new leaf towards the

root. For each node x encountered, check if heights

of left(x) and right(x) differ by at most 1.

If yes, proceed to parent(x). If not, restructure by

doing either a single rotation or a double rotation [next

slide].

For insertion, once we perform a rotation at a node x,

we won’t need to perform any rotation at any ancestor

of x.

AVL Trees / Slide 9

Insertion

Let x be the node at which left(x) and right(x)

differ by more than 1

Assume that the height of x is h+3

There are 4 cases

**Height of left(x) is h+2 (i.e. height of right(x) is h)
**

**Height of left(left(x)) is h+1 single rotate with left child
**

Height of right(left(x)) is h+1 double rotate with left child

**Height of right(x) is h+2 (i.e. height of left(x) is h)
**

**Height of right(right(x)) is h+1 single rotate with right child
**

Height of left(right(x)) is h+1 double rotate with right child

Note: Our test conditions for the 4 cases are different from the code shown in the

textbook. These conditions allow a uniform treatment between insertion and deletion.

AVL Trees / Slide 10

Single rotation

The new key is inserted in the subtree A.

The AVL-property is violated at x

height of left(x) is h+2

height of right(x) is h.

AVL Trees / Slide 11

Single rotation

The new key is inserted in the subtree C.

The AVL-property is violated at x.

**Single rotation takes O(1) time.
**

Insertion takes O(log N) time.

AVL Trees / Slide 12

AVL Tree

5

4

1

4

1

0.8

A

Insert 0.8

3

5

1

0.8

4

8

After rotation

8

y

3

8

3

x

5

B

C

AVL Trees / Slide 13

Double rotation

The new key is inserted in the subtree B1 or B2.

The AVL-property is violated at x.

x-y-z forms a zig-zag shape

also called left-right rotate

AVL Trees / Slide 14

Double rotation

The new key is inserted in the subtree B1 or B2.

The AVL-property is violated at x.

also called right-left rotate

AVL Trees / Slide 15

5

5

AVL Tree

y

8

3

A

4

1

8

3

4

1

B

3.5

Insert 3.5

4

5

3

1

3.5

8

After Rotation

x

z

C

AVL Trees / Slide 16

**An Extended Example
**

Insert 3,2,1,4,5,6,7, 16,15,14

Single rotation

3

3

3

2

2

Fig 1

2

1

3

Fig 4

Fig 2

1

2

Fig 3

1

3

Fig 5

2

1

3

Fig 6

4

Single rotation

4

5

AVL Trees / Slide 17

2

2

1

Single rotation

1

4

4

3

5

3

5

Fig 8

Fig 7

6

4

2

1

4

2

5

6

3

6

3

1

4

5

Fig 10

Fig 9

2

1

6

7

3

5

Fig 11

Single rotation

7

AVL Trees / Slide 18

4

2

6

7

3

1

16

5

Fig 12

4

Double rotation

2

1

6

16

5

Fig 13

2

7

3

15

4

1

6

3

Fig 14

5

15

7

16

AVL Trees / Slide 19

4

2

1

Double rotation

2

6

3

Fig 15

4

5

15

1

7

3

6

16

7

14

5

14

15

Fig 16

16

AVL Trees / Slide 20

**Node and height function
**

Struct AvlNode

// node declaration

{

Comparable element;

AvlNode *left;

AvlNode *right;

int height;

AvlNode(const Comparable & theElement, AvlNode *lt,AvlNode

*rt, int h=0) : element(theElement),left(lt),right(rt),height(h)

}

Int height(AvlNode *t) const

// height function

{

return t == NULL ? -1 : theigh;

}

AVL Trees / Slide 21

**void insert(const Comparable & x,AvlNode * &t)
**

{

if(t==NULL)

t= new AvlNode(x, NULL,NULL);

else if(x< telement)

{

insert (x,t->left);

if(height(t->left) – height(t->right) == 2)

if(x < t->left->element)

rotateWithLeftChild(t);

else

doubleWithLeftChild(t);

}

else if ( t->element < x)

{

insert (x,t->right);

if(height(t->right) – height(t->left) == 2)

if(x < t->right->element)

rotateWithRightChild(t);

else

doubleWithRightChild(t);

}

else

;

//duplicate ; do nothing

t->height = max (height( t->left), height(t->right))+1;

}

AVL Trees / Slide 22

k2

k1

k1

k2

z

x

y

y

**void rotateWithLeftChild( AvlNode * & k2) x
**

{

AvlNode *k1 = k2->left;

k2->left = k1->right;

k1->right = k2;

k2->height = max (height( k2->left), height (k2->right))+1;

k1->height = max(height(k1->left),k2->height)+1;

k2 = k1;

k3

}

Void doubleWithLeftChild(AvlNode * & k3)

{

rotateWithRightChild( k3->left);

a

rotateWithLeftChild (k3);

}

k1

b

k2

k1

d

k2

a

c

//try for rotateWithRightChild and doubleWithRightChild

z

k3

b

c

d

AVL Trees / Slide 23

Deletion

**Delete a node x as in ordinary binary search tree.
**

Note that the last node deleted is a leaf.

Then trace the path from the new leaf towards the

root.

For each node x encountered, check if heights of

left(x) and right(x) differ by at most 1. If yes, proceed

to parent(x). If not, perform an appropriate rotation at

x. There are 4 cases as in the case of insertion.

For deletion, after we perform a rotation at x, we may

have to perform a rotation at some ancestor of x.

Thus, we must continue to trace the path until we

reach the root.

AVL Trees / Slide 24

Deletion

**On closer examination: the single rotations for
**

deletion can be divided into 4 cases (instead

of 2 cases)

**Two cases for rotate with left child
**

Two cases for rotate with right child

AVL Trees / Slide 25

Single rotations in deletion

**In both figures, a node is deleted in subtree C, causing the height
**

to drop to h. The height of y is h+2. When the height of subtree A

is h+1, the height of B can be h or h+1. Fortunately, the same

single rotation can correct both cases.

rotate with left child

AVL Trees / Slide 26

**Single rotations in deletion
**

In both figures, a node is deleted in subtree A, causing the height

to drop to h. The height of y is h+2. When the height of subtree C

is h+1, the height of B can be h or h+1. A single rotation can

correct both cases.

rotate with right child

AVL Trees / Slide 27

Rotations in deletion

There are 4 cases for single rotations, but we

do not need to distinguish among them.

There are exactly two cases for double

rotations (as in the case of insertion)

Therefore, we can reuse exactly the same

procedure for insertion to determine which

rotation to perform

- InfoGlue - User Manual for Version 2.5.0Uploaded byTeodofilus Kumalo
- FaThumb: A facet-based interface for mobile searchUploaded bylisandro
- Final SolUploaded byRaghu Vamsi Potukuchi
- 10.1.1.30Uploaded byAhmed Alkashab
- Cs 301 Final Term Solved Paper 2Uploaded byRizwanIlyas
- MELJUN CORTES ALGORITHM_Coping With the Limitations of Algorithm Power_IIUploaded byMELJUN CORTES, MBA,MPA
- 29review(AVL)Uploaded byAndrei Pleșa
- ho1_prefinalsUploaded byJL Pegenia
- thispainedmetowriteUploaded byapi-278600611
- DS1211Uploaded byCSETUBE
- Predicate PushdownUploaded bymgmad
- avl suppUploaded byhamadi_298312254
- LPMoRS_1994Uploaded byAntihack F Fds Ds
- New Microsoft Word DocumentUploaded bychrispinfx
- Soloviev PhmlUploaded byLuis Miguelo
- Fast Data Collection in Tree-Based Wireless Sensor NetworksUploaded byManikanda Rajan
- best job for computer science major by salary potentialUploaded byapi-325727229
- anthony carmichaelUploaded byapi-267188210
- Technical Aptitude Questions.pdfUploaded byPerumal Namasivayam
- Convolutional CodesUploaded byHarsha
- Submitting Future Requests Via Package fnd_request.submit_requestUploaded bytracejm
- DAC e BrochureUploaded byvcvijaya
- moerkerken program 2017Uploaded byapi-352851812
- Data StructuresUploaded bymaksimmb
- DME Process in APUploaded byNaveen Kumar
- Business Process IntelligenceUploaded byricardofrancisco
- ploteo y graficacion en f90.pdfUploaded byMarco Marco
- Technical Questions PlacementUploaded byArvind Upadhyay
- huffmanUploaded byJack Dawson
- modifStoreIUTAUploaded byCarlos Yepez

- sql-server-hardware-ebook.pdfUploaded bySuruttaiyan Suru
- REEP001Uploaded bySuruttaiyan Suru
- Pump Parts Dimentional ToleranceUploaded bySuruttaiyan Suru
- Pump Parts Dimentional ToleranceUploaded bySuruttaiyan Suru
- CBT Nuggets 70 463.docxUploaded bySuruttaiyan Suru
- Pump Maintenance GuidelinesUploaded bySuruttaiyan Suru
- inside-the-sql-server-query-optimizer.pdfUploaded bySuruttaiyan Suru
- Testing and Commisioning Procedures for HVACUploaded bySuruttaiyan Suru
- Pump Grouting ArticleUploaded bySuruttaiyan Suru
- NyaaUploaded bySuruttaiyan Suru
- Reep 001Uploaded bySuruttaiyan Suru
- Bearing Failure on a Boiler Feed Water PumpUploaded bySuruttaiyan Suru
- Pump Grouting ArticleUploaded bySuruttaiyan Suru