avl tree basic lecture notes, Lecture notes of Data Structures and Algorithms

which of the following relational algebra query has output “ Find the Supplier ids of the suppliers who supply a red part that costs less than 100 dollars and a green part that costs less than 100 dollars.” Finds the Supplier names of the suppliers who supply a red part that costs less than 100 dollars and a green part that costs less than 100 dollars Finds the Supplier names of the suppliers who supply a red part that costs less than 100 dollars and a green part that co

Typology: Lecture notes

2019/2020

Uploaded on 08/01/2020

ashish-b-1
ashish-b-1 🇮🇳

1 document

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CSE 326: Data Structures
AVL Trees
Brian Curless
Spring 2008
2
Potential Balance Conditions
What are some candidate balance conditions on BST’s?
1. Left and right subtrees
of the root have
equal number of nodes
2. Left and right subtrees
of the root have
equal height
3
Potential Balance Conditions
3. Left and right subtrees
of every node have
equal number of nodes
4. Left and right subtrees
of every node
have equal height
4
Balancing Trees
Many algorithms exist for keeping trees balanced
Adelson-Velskii and Landis (AVL) trees
Splay trees and other self-adjusting trees
B-trees and other multiway search trees (for
very large trees)
Today we will talk about AVL trees
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download avl tree basic lecture notes and more Lecture notes Data Structures and Algorithms in PDF only on Docsity!

1

CSE 326: Data Structures

AVL TreesBrian CurlessSpring 2008

Potential Balance ConditionsWhat are some candidate balance conditions on BST’s?1. Left and right subtreesof the root haveequal number of nodes2. Left and right subtreesof the root haveequal^ height

3

Potential Balance Conditions3. Left and right subtreesof every node

have

equal number of nodes4. Left and right subtreesof^ every node have equal

height

Balancing Trees

-^ Many algorithms exist for keeping trees balanced– Adelson-Velskii and Landis (AVL) trees– Splay trees and other

self-adjusting

trees

– B-trees and other multiway search trees (for^ very^ large trees)• Today we will talk about

AVL trees

5

The AVL Tree Data Structure

Ordering property–^ Same as for BSTStructural properties1. Binary tree property(0,1, or 2 children)2. Heights of left and rightsubtrees of

every node differ by at most 1 Result:Worst case depth of anynode is: O(log

n )^

Recursive Height Calculation Recall : height is max numberof edges from root to a leafWhat is the height at A?Note: height(null) = -

A

7

11

AVL trees or not? 10

Proving Shallowness Bound

AA^0

1

A^2

A^3

A^4

Ais smallest AVL tree of height h. h^ Built from A

and A h -

attached to a new root. h -

(Note: these A

’s are not unique; e.g., could swap children.) h

13

Testing the Balance Property

We need to be able to:1. Track Balance2. Detect Imbalance3. Restore Balance

What if we insert(30)?

An AVL Tree

0 (^01) 2

1 3

data 10 height 3 children

15

AVL trees: find, insert

-^ AVL find

:

  • same as BST find.• AVL insert

:

  • same as BST insert,

except^

may need to “fix”

the AVL tree after inserting new value.We will consider the 4 fundamental insertioncases…

Case #1: left-left insertion (zig-zig)

a

Z

b Y h^ X

h

h a

Z

b^ h Y

h

X

Insert on left child’s left h+

h+

17

Case #1: repair with single rotation

a ZY b h+

h^

h

X < b < Y < a < Z

h+

a

Z

b^ h Y

h

h+2 X X

h+3 Single rotation

Height of tree before/after?

Effect on Ancestors?

Cost?^

Single rotation example^104

19

Case #2: left-right insertion (zig-zag)

a

Z

b Y h+1 X

h

h a

Z

b h

h

X^

Insert on left child’s rightY h

h+

Case #2: repair with single rotation

a Z b h+ h

h

X < b < Y < a < Z^ Are we better off now?

a

Z

b h

h

X X

h+1 Y^ Single rotation Y h+

h+

25

Double rotation, step 2

Case #3: right-left insertion (zig-zag)

a X

b

Z

c^ h-^

h

h

c

Z

a X

b h-^

h

h^

h h

V

U

V

h+1 U

h+ h+3 h+^

h+ h+

Double rotation

27

Case #4: right-right insertion (zig-zig)

a X

b Y

h+ h h

Z

a X

b h Y

h+

h^

h+2 Z h+ h+

h+ Single rotation

AVL tree case summary Let^ x^ be the node where an imbalance occurs.Four cases to consider. The insertion below

a^ is in the

1.^ left^ child’s^ left

subtree_._

(zig-zig)

2.^ left^ child’s^ right

subtree_._ (zig-zag)

3.^ right

child’s^ left

subtree_._ (zig-zag)

4.^ right

child’s^ right

subtree_._ (zig-zig)

Cases 1 & 4 are solved by a single rotation:1.^ Rotate between

a^ and child Cases 2 & 3 are solved by a double rotation:1.^ Rotate between

a ’s child and grandchild

2.^ Rotate between

a^ and^ a ’s new child

29 (^95) 2

Single and Double Rotations:Inserting what integer values wouldcause the tree to need a:1. single rotation?2. double rotation?3. no rotation?

Insertion procedure

1.^ Find spot for new key2.^ Hang new node there with this key3.^ Search back up the path for imbalance4.^ If there is an imbalance:cases #1,#4: Perform single rotation and exitcases #2,#3: Perform double rotation and exitBoth rotations keep the subtree height unchanged.Hence only one rotation is sufficient!

31

More insert examples

Insert(33)

Unbalanced?How to fix?

0

(^100) 1 0

2 3

0

Single Rotation^20 9 2

0

(^200) 1 0

3 4

1 0

0

(^310)

37

More insert examples

0

(^110) 1 0

2 3

0 0 12

Insert(3) Unbalanced?How to fix?

Insert into an AVL tree: 5, 8, 9, 4, 2, 7, 3, 1

39

AVL complexity

What is the worst case complexity of an insert?What is the worst case complexity of a find?