Augmenting AVL trees - PowerPoint PPT Presentation

About This Presentation
Title:

Augmenting AVL trees

Description:

Augmenting AVL trees How we ve thought about trees so far Good for determining ancestry Can be good for quickly finding an element Other kinds of uses? – PowerPoint PPT presentation

Number of Views:75
Avg rating:3.0/5.0
Slides: 21
Provided by: Trevo98
Category:
Tags: avl | augmenting | trees | week

less

Transcript and Presenter's Notes

Title: Augmenting AVL trees


1
Augmenting AVL trees
2
How weve thought about trees so far
Good for determining ancestry Can be good for
quickly finding an element
3
Other kinds of uses?
  • Any thoughts?
  • Finding a minimum/maximum
  • (heaps are probably just as good or better)
  • Finding an average?
  • More complicated things?!!!11one

Enter idea of augmenting a tree
4
Augmenting
  • Can quickly compute many global properties that
    seem to need knowledge of the whole tree!
  • Examples
  • size of any sub-tree
  • height of any sub-tree
  • averages of keys/values in a sub-tree
  • minmax of keys/values in any sub-tree,
  • Can quickly compute any function f(x) so long as
    you only need to know f(x.left) and f(x.right)!

5
Augmenting an AVL tree
  • Can augment any kind of tree
  • Only balanced trees are guaranteed to be fast
  • After augmenting an AVL tree to compute f(x), we
    can still do all operations in O(lg n)!

6
Simple first example
  • We are going to do one simple example
  • Then, you will help with a harder one!
  • Problem augment an AVL tree so we can do
  • Insert(key) add key in O(lg n)
  • Delete(key) remove key in O(lg n)
  • Height(node) get height of sub-tree rooted at
    node in O(1)

A regular AVL tree already does this
Store some extra data at each node but what?
How do we do this?
7
Can we compute this function quickly?
  • Function we want to compute Height(u) H(u)
  • If someone gives us H(uL) and H(uR),can we
    compute H(u)?
  • What formula should we use?
  • If u null then
  • H(u) 0
  • Else
  • H(u) maxH(uL), H(uR)1

u
uL
uR
H(u)?
H(uL)
H(uR)
8
Augmenting AVL tree to compute H(u)
  • Each node u contains
  • key the key
  • left, right child pointers
  • h height of sub-tree rooted at u
  • How?

The usual stuff
Secret sauce!
d, 0
d, 1
d, 2
d, 2
d, 1
d, 2
Insert(d)
a, 0
a, 1
b, 0
b, 1
e, 0
e, 0
Insert(a)
Insert(e)
b, 0
c, 0
c, 0
a, 1
a, 0
Insert(b)
c, 0
Insert(c)
9
Algorithm idea
  • From the last slide, we develop an algorithm
  • Insert(u)
  • 1 BST search for where to put u
  • 2 Insert u into place like in a regular AVL
    tree
  • 3 Fix balance factors and rotate as you would
    in AVL insert, but fix heights at the same
    time.
  • (Remember to fix heights all the way to the
    root. Dont stop before reaching the root!)
  • (When you rotate, remember to fix heights of all
    nodes involved, just like you fix balance
    factors!)

10
Harder problem overpaid employees!
  • You have a record for each employee in your
    company with salary and age.
  • We want to quickly find overpaid employees of age
    lt A.

11
Breaking the problem down
  • You must design a data structure D of employee
    records to efficiently do
  • Insert(D x) If x is a pointer to a new employee
    record, insert the record pointed to by x into D.
  • Delete(D x) If x is a pointer to an existing
    employee record in D, remove that record from D.
  • MaxSal(D A) Return the largest salary, amongst
    all employees in D whose age is lt A if there is
    no such employee, then return -1.
  • All functions must run in O(lg n)

12
Part 1
  • Give a precise and full description of your data
    structure
  • Illustrate this data structure by giving an
    example of it on some collection of employee
    records of your own choice.

13
Figuring out the data structure
  • Iterative processhard to get it right the first
    time!
  • What function do we want to compute?
  • Maximum salary for employees with age lt A
  • What info should we store at each node u?
  • Maybe maximum salary for all employees in the
    sub-tree rooted at u? Lets call this value
    MS(u).
  • How can we compute MS(u) quickly (i.e., by
    looking at a small number of descendents of u)?

14
Organizing nodes inside the tree
  • How can we turn maximum salary in a sub-tree into
    maximum salary for those with age lt A?
  • For sure, need to relate age with sub-trees!
  • Things are organized into sub-trees by key
  • So, age must be related to key.
  • Let age be the key of each node
  • May have 2 nodes with same key, but thats okay.

15
Answer to part 1
Age Salary 1100 31000 1247 29000 1590 28000 1590 4
8000 3801 4100017000 36000 30000 90000 30000 7500
0
Age17000, MS
Age17000, salary36000, MS
Age17000, salary36000, MS90000
Age30000, salary75000, MS90000
Age1590, MS
Age24100, MS
Age1590, salary28000, MS
Age30000, salary75000, MS
Age1590, salary28000, MS48000
Age3801, MS
Age1100, MS
Age30000, MS
Age3801, salary41000, MS
Age1100, salary31000,MS
Age30000, salary90000, MS
Age1100, salary31000,MS31000
Age3801, salary41000, MS48000
Age30000, salary90000, MS90000
Age1247, MS
Age1247, salary29000,MS
Age1247, salary29000,MS29000
Age1590, MS
Age1590, salary48000, MS
Age1590, salary48000, MS48000
16
Part 2
  • Explain how to implement Insert(D, x), Delete(D,
    x) and MaxSal(D, A) in O(log n) time
  • Insert
  • 1 Do regular AVL-insert(D, ltx.age, x.salary,
    MS0gt), except
  • As you move up the tree fixing balance factors,
    fix MS(u) for every node on the path to the root
  • After each rotation, fix each MS(u) for each
    nodethat was involved in the rotation
  • Remember to fix heights all the way to the
    root.(Dont stop early, even if done balance
    factors/rotations!)
  • Delete
  • Same as Insert, but Do regular AVL-delete
  • How do we do MaxSal???

What does fix MS(u) mean?What formula do we
use?
Set MS(u) maxu.salary, MS(uL), MS(uR)
17
How can we compute MaxSal?
  • Write a recursive function!
  • Recursion is often very natural for trees
  • Big problem want MaxSal(D, A) largest salary
    for age lt A in tree
  • Smaller problem MaxSal(u, A) largest salary
    for age lt A in sub-tree rooted at u
  • Recursive/inductive measure of problem size size
    of sub-tree
  • Therefore, smaller problem smaller sub-tree.

18
Code for MaxSal
  • // u is a node, A is an age
  • MaxSal(u, A)
  • if u null then
  • return -1
  • else if A lt u.age then
  • return MaxSal(uL, A)
  • else
  • return maxu.salary, MaxSal(uR,A),
    MaxSal(uR,A)

u salary, age, MS
uR salary, age, MS
uL salary, age, MS
Calling MaxSal on BOTH sub-trees can take O(n)
time!
19
Code for MaxSal
  • // u is a node, A is an age
  • MaxSal(u, A)
  • if u null then
  • return -1
  • else if A lt u.age then
  • return MaxSal(uL, A)
  • else
  • return maxu.salary, MaxSal(uR,A), MS(uL)

u salary, age, MS
uR salary, age, MS
uL salary, age, MS
20
Why O(lg n) time?
  • Insert/Delete normal AVL operation O(lg n)
  • PLUS update MS(u) for each u on path to the root
  • Length of this path lt tree height, so O(lg n) in
    an AVL tree
  • PLUS update MS(u) for each node involved in a
    rotation
  • At most O(lg n) rotations (one per node on the
    path from the root lt tree height)
  • Each rotation involves a constant number of nodes
  • Therefore, constant O(lg n), which is O(lg n).
  • MaxSal
  • Constant work recursive call on ONE child
  • Single recursive call means O(tree height) O(lg
    n)
Write a Comment
User Comments (0)
About PowerShow.com