CS#61B#Data#Structures#and#Programming#Methodology##Aug#7,#2008#David#Sun#Announcements#• Final#will#be#held#next#Thursday#14th#of#Aug.#Exact#Jme#TBA.#• Two#review#sessions#next#week,#Tuesday#and#Wednesday#during#class#with#mock‐exams.#• If#you#are#taking#CS61C#and#plan#to#take#the#CS61C#exam#on#14th#of#Aug,#please#send#me#an#email#with#your#name#and#student#ID.##Recap:#Binary#Search#Tree#Running#Times#• In#a#perfectly#(full)#balanced#binary#tree#with#height/depth#h,#the#number#of#nodes#n#=#2(h+1)#‐#1.#• Therefore,#no#node#has#depth#greater#than#log2#n.##• The#running#Jmes#of#find(),#insert(),#and#remove()#are#all#proporJonal#to#the#depth#of#the#last#node#encountered,#so#they#all#run#in#O(log#n)#worst‐case#Jme#on#a#perfectly#balanced#tree.#Recap:#Binary#Search#Tree#Running#Times#• What’s#the#running#Jme#for#this#binary#tree?#• The#running#Jmes#of#find(),#insert(),#and#remove()#are#all#proporJonal#to#the#depth#of#the#last#node#encountered,#but#d#=#n#–#1,##so#they#all#run#in#O(n)#worst‐case#Jme.#Recap:#Binary#Search#Tree#Running#Times#• The#Middle#ground:#reasonably#well‐balanced#binary#trees#– Search#tree#operaJons#will#run#in#O(log#n)#Jme.#• You#may#need#to#resort#to#experiment#to#determine#whether#any#parJcular#applicaJon#will#use#binary#search#trees#in#a#way#that#tends#to#generate#balanced#trees#or#not.##2‐3‐4#Trees#• A#2‐3‐4#tree#is#perfectly#balanced:#– find,#insert,#and#remove#operaJons#take#O(log#n)#Jme,#in#the#worst#case.#• Every#node#in#the#tree#has#2,#3,#or#4#children,#except#leaves,#which#are#all#at#the#boYom#level#of#the#tree.###– Each#node#stores#1,#2,#or#3#entries,#which#determine#how#other#entries#are#distributed#among#its#children's#subtrees.#• Each#internal#(non‐leaf)#node#has#one#more#child#than#keys.###– For#example,#a#node#with#keys#[20,#40,#50]#has#four#children.###• 2‐3‐4#tree#invariant:#– Eack#key#k#in#the#subtree#rooted#at#the#first#child#saJsfies#k#<=#20;#at#the#second#child,#20#<=#k#<=#40;#at#the#third#child,#40#<=#k#<=#50;#and#at#the#fourth#child,#k#>=#50.#find#1. Start#at#the#root.##At#each#node,##check#for#the#key#k.#If#k#is#found,#we#are#done.#2. If#it's#not##present,#and#if#k#<#k1,#where#k1#is#the#first#key#stored#in#the#node,#then#look#in#the#subtree#rooted#at#k1.#Otherwise,#if#k1#<#k#<#k2,#then#look#in#the#subtree#rooted#at#k2.#ConJn#insert 1. insert() walks#down#the#tree#in#search#of#the#key#k#(like#find()).###2. If#it#find#an#entry#with#key#k,#it#proceeds#to#that#entry's#"lec#child"#and#conJnues.#3. When#insert()#encounters#a#3‐key#node,#the#middle#key#is#ejected#from#the#current#node,#and#placed#in#the#parent#node.#The#other#two#keys#in#the#3‐key#node#are#split#into#two#separate#1‐key#nodes,#which#are#divided#underneath#the#old#middle#key.#insert • The#splieng#of#every#3‐key#node#is#to#make#sure#that:#– there's#room#for#the#new#key#in#the#leaf#node#(if#splieng#happens#at#a#leave#node)#– there's#room#for#the#ejected#keys#that#are#moved#into#the#the#internal#nodes#(if#splieng#happens#at#an#internal#node).#• Insert#may#increase#the#depth#of#the#tree#by#one#if#the#root#of#the#tree#was#a#3‐key#node.##remove 1. Find#a#node#containing#key#k#using#the#same#algorithm#as#find().##2. Return#null#if#k#is#not#in#the#tree;##3. If#k#is#found#in#a#leaf#node,#remove#it.#4. If#k#is#found#in#an#internal#node,#replace#it#with#the#entr y#with#the#next#higher#key.#remove • Remove#could#potenJally#empty#a#1‐key#leaf#node,#so#we#need#to#eliminate#all#1‐key#nodes#by#turning#it#into#either#a#2‐key#node#through#rotaBon#or#a#3‐key#node#through#fusion.#• Case#1#if#there#is#an#adjacent#sibling#with#more#than#1#key:#– Steal#a#key#from#this#adjacent#sibling.#– To#maintain#the#2‐3‐4#tree#invariant,#perform#a#rotaBon#by#moving#a#key#from#the#sibling#to#the#parent#and#moving#a#key#from#the#parent#to#the#1‐key#node.###– If#the#moved#key#in#the#sibling#has#a#subtree#S,#then#that#subtree#is#moved#to#the#1‐key#node#(now#a#2‐key#node).#remove • Case#2#if#no#adjacent#siblings#have#more#than#1#child:#– Steal#a#key#from#the#parent#and#fuse#the#1‐key#node,#the#key#from#the#parent#node,#and#the#1‐key#sibling#node#into#one#node.###– Since#the#parent##must#be#visited#before#the#current#node,#it#must#have#at#least#2#keys#(unless#it’s#the#root).###remove • Case#3#if#the#parent#is#the#root#and#contains#only#one#key,#no#adjacent#sibling#has#more#than#one#key:#– Fuse#the#current#1‐key#node,#its#1‐key#sibling,#and#the#1‐key#root#into#one#3‐key#node#that#serves#as#the#new#root.###– The#only#case#where#the#depth#of#the#tree#decreases#by#one.#Running#Time#• A#2‐3‐4#tree#with#depth#d#has#between#2d#(if#all#the#nodes#are#1‐key#nodes)#and#4d#(if#all#the#nodes#are#3‐key#nodes)#leaves.###• If#n#is#the#total#number#of#nodes,#then#n#>=#2(d+1)#‐#1.#Taking#the#logarithm#of#both#sides,#d#is#in#O(log#n).#• 2‐3‐4#tree#operaJons#are#more#complicated#than#corresponding#operaJons#for#binary#search#trees,#so#the#Jme#required#to#process#a#single#node#is#longer,#but#sJll#in#O(1)#Jme.#• The#number#of#nodes#visited#is#proporJonal#to#the#depth#of#the#tree.##– #Hence,#the#running#Jmes#of#the#find(),#insert(),#and#remove()#operaJons#are#in#O(d)#and#hence#in#O(log#n),#even#in#the#worst#case.#– Compare#this#with#the#Theta(n)#worst‐case#Jme#of#ordinary#binary#search#trees.#Splay#Tree#• A#splay#tree#is#a#type#of#balanced#binary#search#tree.##Structurally,#it#is#idenJcal#to#an#ordinary#binary#search#tree;#the#only#difference#is#in#the#algorithms#for#finding,#inserJng,#and#deleJng#entries.#• All#splay#tree#operaJons#run#in#O(log#n)#Jme#on#average,#where#n#is#the#number#of#entries#in#the#tree.##Any#single#operaJon#can#take#Theta(n)#Jme#in#the#worst#case.##But#any#sequence#of#k#splay#tree#operaJons,#with#the#tree#iniJally#empty#and#never#exceeding#n#items,#takes#O(k#log#n)#worst‐case#Jme.#RotaJon#• RotaJon#is#a#procedure#that#keeps#splay#tree#(and#other#balanced#search#trees)#balanced.#• There#are#two#types#‐‐#a#lec#rotaJon#and#a#right#rotaJon#‐‐#and#each#is#the#other’s#reverse.#• The#rotaJon#procedure#preser ves#the#binary#search#tree#invariant,#i.e.#acer#the#rotaJon,#the#lec#subtree#is#less#than#or#equal#to#the#root#and#the#right#subtree#is#more#than#or#equal#to#the#root.##find 1.
View Full Document