






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 10
This page cannot be seen from the preview
Don't miss anything!







1
3
-^ Many algorithms exist for keeping trees balanced– Adelson-Velskii and Landis (AVL) trees– Splay trees and other
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) = -
7
11
AVL trees or not? 10
Proving Shallowness Bound
1
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
:
:
the AVL tree after inserting new value.We will consider the 4 fundamental insertioncases…
Case #1: left-left insertion (zig-zig)
a
b Y h^ X
h
h a
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
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
b Y h+1 X
h
h a
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
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
c^ h-^
h
h
c
a X
b h-^
h
h^
h h
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
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
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