1
CSE326:DataStructures Lecture#21 OneLastGasp
BartNiswonger SummerQuarter2001
Today’sOutline
- AlgorithmDesign(fromFriday)
– DynamicProgramming – Randomized – Backtracking
- “Advanced”DataStructures
CSE326:DataStructures Lecture#21 OneLastGasp BartNiswonger - - PDF document
CSE326:DataStructures Lecture#21 OneLastGasp BartNiswonger SummerQuarter2001 TodaysOutline AlgorithmDesign(fromFriday) DynamicProgramming Randomized Backtracking
1
BartNiswonger SummerQuarter2001
– DynamicProgramming – Randomized – Backtracking
2
binarysearchtree
– binarytreeproperty – searchtreeproperty
heap-orderproperty!
– randomlyassigned priorities 15 12 10 30 9 15 7 8 4 18 6 7 2 9 heapinyellow;searchtreeinblue
priority key
Legend:
Insertdatainsortedorderintoatreap; whatshapetreecomesout?
6 7 insert(7) 6 7 insert(8) 7 8 6 7 insert(9) 7 8 2 9 6 7 insert(12) 7 8 2 9 15 12
priority key
Legend:
3
6 7 insert(15) 7 8 2 9 15 12 6 7 7 8 2 9 15 12 9 15 6 7 7 8 2 9 9 15 15 12
delete(9) 6 7 7 8 2 9 9 15 15 12 7 8 6 7
9 15 15 12 7 8 6 7 9 15
15 12 7 8 6 7 9 15 15 12
7 8 6 7 9 15 15 12
4
– insertinexpectedO(logn)time – deleteinexpectedO(logn)time – findinexpectedO(logn)time
– O(1)pernode – aboutthecostofAVLtrees
– create – destroy – find – insert – delete – rangequeries
searchtree
allthekeysoronrangesofthekeys
9,1 3,6 4,2 5,7 8,2 1,9 4,4 8,4 2,5 5,2
5
dimensions
dimensions
Arangequery isasearchinadictionary inwhichtheexactkeymaynotbe entirelyspecified. Rangequeriesaretheprimaryinterface withmulti-Ddatastructures.
6
keys:e.g.,acircular rangequery x
Findeverythingintherectangle…
7 x
Findeverythingintherectangle… x y
8 x y
succeedinglevel
alongthecurrentdimensionateachlevel
– guaranteeslogarithmicheightandbalanced tree
k-Dtreenode dimension left right keys value Thedimensionthat thisnodesplitson
9 x y
x y
10 x y
x y
11
m f k g h i e c j d l a b
a d l c b e h f m j g i k x y
Searcheverypartitionthatintersectstherectangle. Checkwhethereachnode(includingleaves)fallsintotherange.
12 x y
Searcheverypartitionthatintersectstheshape(circle). Checkwhethereachnode(includingleaves)fallsintotheshape.
find(<x1,x2,…,xk>,root)
findsthenodewhichhasthe givensetofkeysinitor returnsnull ifthereisnosuch node
Node*&find(const keyVector &keys, Node*&root){ intdim=root->dimension; if(root==NULL) returnroot; elseif(root->keys==keys) returnroot; elseif(keys[dim]<root->keys[dim]) returnfind(keys,root->left); else returnfind(keys,root->right); }
runtime:
13
(butnotwhenbuiltinbatch!)
insert(<5,0>) insert(<6,9>) insert(<9,3>) insert(<6,5>) insert(<7,7>) insert(<8,6>)
6,9 5,0 6,5 9,3 8,6 7,7 suckfactor:
find(<3,6>) find(<0,10>) 5,7 8,2 1,9 4,4 8,4 2,5 5,2 9,1 3,6 4,2
14
(quadrants)
leafisalreadyoccupied,splituntilonlyonenode perleaf
quadtreenode Quadrants:
quadrant 0,01,00,11,1 keys value Center x y Center: x y
15 x y
x y
16 x y
x y
17
a g b e f d c g a f e d c b
x y
18
Node*&find(Keyx,Keyy,Node*&root){ if(root==NULL) returnroot;//Emptytree if(root->isLeaf) returnroot;//Keymaynotactuallybe here int quad= getQuadrant(x,y,root); returnfind(x,y,root->quadrants[quad]); }
find(<x,y>,root) findsthenodewhichhas
thegivenpairofkeysinitorreturnsquadrant wherethepointshouldbeifthereisnosuch node
runtime:
Compares againstcenter; alwaysmakes thesamechoice
b a suckfactor:
19
a g b e f d c g a f e d c b find(<10,2>)(i.e.,c) find(<5,6>)(i.e.,d)
g g g a insert(<10,7>,x)
a g b e f d c x g x
separatesfromthenewone.
20
a g b e f d c g a f e d c b delete(<10,2>)(i.e.,c)
child,deleteit.
g a f e d c b getNearestNeighbor(<1,4>) g b f d c
a e Workson k-DTrees,too!
21
– Densitybalancedtrees – NumberofnodesisO(n)wheren isthenumberofpoints – HeightofthetreeisO(logn)withbatchinsertion – Supportsinsert,find,nearestneighbor,rangequeries
– NumberofnodesisO(n(1+log(/n)))wheren isthe numberofpointsand istheratioofthewidth(orheight)
points – HeightofthetreeisO(logn+log) – Supportsinsert,delete,find,nearestneighbor,range queries
– Packageupyourexecutableandturnitin!
22