CSE326:DataStructures Lecture#10 AmazinglyVexingLetters Bart - - PDF document

cse 326 data structures lecture 10 amazingly vexing
SMART_READER_LITE
LIVE PREVIEW

CSE326:DataStructures Lecture#10 AmazinglyVexingLetters Bart - - PDF document

CSE326:DataStructures Lecture#10 AmazinglyVexingLetters Bart Niswonger SummerQuarter2001 TodaysOutline AVLTrees Deletion buildTree ThinkingaboutAVLtrees SplayTrees 1


slide-1
SLIDE 1

1

CSE326:DataStructures Lecture#10 AmazinglyVexingLetters

Bart Niswonger SummerQuarter2001

Today’sOutline

  • AVLTrees

– Deletion – buildTree – ThinkingaboutAVLtrees

  • SplayTrees
slide-2
SLIDE 2

2

Deletion(ReallyEasyCase)

20 9 2 15 5 10 30 17 3 12

1 1 2 2 3

Delete(17)

Deletion(PrettyEasyCase)

20 9 2 15 5 10 30 17 3 12

1 1 2 2 3

Delete(15)

slide-3
SLIDE 3

3

Deletion(PrettyEasyCasecont.)

20 9 2 17 5 10 30 3 12

1 1 2 2 3

Delete(15)

Deletion(HardCase#1)

20 9 2 17 5 10 30 3 12

1 1 2 2 3

Delete(12)

slide-4
SLIDE 4

4

SingleRotationonDeletion

20 9 2 17 5 10 30 3

1 1 2 2 3

30 9 2 20 5 10 17 3

1 2 1 3

Whatisdifferent about deletionthaninsertion?

Deletion(HardCase)

Delete(9) 20 9 2 17 5 10 30 3 12

1 2 2 2 3 4

33 15 13

1

20 30 12 33 15 13

1

11 18

slide-5
SLIDE 5

5

DoubleRotationonDeletion

2 3 20 2 17 5 10 30 12

1 2 2 2 3 4

33 15 13

1 1

11 18 20 5 2 17 3 10 30 12

2 2 1 3 4

33 15 13

1 1

11 18

✂✁☎✄✝✆✟✞✡✠☎✞✡☛✌☞☎✍☎✎☎✏

DeletionwithPropagation

20 5 2 17 3 10 30 12

2 2 1 3 4

33 15 13

1 1

11 18 Whatrotationdoweapply?

slide-6
SLIDE 6

6

PropagatedSingleRotation

30 20 17 33 12 15 13

1

5 2 3 10

4 3 2 1 2 1

11 20 5 2 17 3 10 30 12

2 2 1 3 4

33 15 13

1 1

11 18 18

PropagatedDoubleRotation

17 12 11 5 2 3 10

4 2 3 1

20 5 2 17 3 10 30 12

2 2 1 3 4

33 15 13

1 1

11 18 15

1

20 30 33

1

18 13

2

slide-7
SLIDE 7

7

AVLDeletionAlgorithm

Recursive

1.Ifatnode,delete it 2.Otherwiserecurse tofindit 3.Correctheights a.Ifimbalance#1, singlerotate b.Ifimbalance#2 (ordon’tcare), doublerotate

Iterative

1.Searchdownwardfor node,stacking parentnodes 2.Deletenode 3.Unwindstack, correctingheights a.Ifimbalance#1, singlerotate b.Ifimbalance#2 (ordon’tcare) doublerotate

FunwithAVLTrees

ToInsertasequenceofnkeys(unordered) 1934187 intoinitiallyemptyAVLtreetakes Ifwethenprintusinginordertraversaltaking O(n) whatdoweget?

1 1

( lo l g )

  • g

log

n n i i

O n n i n

slide-8
SLIDE 8

8

Whatcanweimprove?

PrintingeverynodeisO(n),nothingtodo Whataboutbuildingatree?

– CanwedoitinlessthanO(nlogn)

  • Whatiftheinputissorted?

3471819

Ifitissorted,whybother? We’llseeinamoment!

AVLbuildTree

8 10 15 20 30 35 40 5 17 17 8 1015 5 20303540

Divide&Conquer

– Dividetheproblemintoparts – Solveeachpartrecursively – Mergethepartsintoa generalsolution Howlongdoes divide&conquertake?

slide-9
SLIDE 9

9

BuildTreeExample

35 17 15 5 8 10

3 2 2 1

30

1

40 20 8 10 15 20 30 35 40 5 17 8 10 15 5 8 5 30 35 40 20 30 20

BuildTreeAnalysis(Approximate)

T(1)=1 T(n)=2T(n/2)+1

slide-10
SLIDE 10

10

✂ ✂✄✆☎

2 1 n

  • ✂✞✂✆☎

2 1 n

✟ ✟ ✠ ✡ ✡☛✆☞

2 1 n

✟ ✌ ✟ ✡✍✡✆☞

2 1 n

BuildTreeAnalysis(Exact)

PreciseAnalysis:T(0)=b

T(n)=T()+T()+c Byinductiononn: T(n)=(b+c)n+b Basecase: T(0)=b=(b+c)0+b Inductionstep: T(n)=(b+c)+b+ (b+c)+b+c =(b+c)n+b QED:T(n)=(b+c)n+b=

✎ ✎ ✎ ✎ (n)

1 2 1 2 1

✏ ✑ ✒ ✓ ✒ ✔✕ ✔ ✏ ✖ ✒ ✒ ✗ ✔ ✔ ✘ ✏

n n n

Application:BatchDeletion

  • Supposeweareusinglazy deletion
  • Whentherearelots ofdeletednodes

(n/2),needtoflush themallout

  • Batchdeletion:

– Printnon-deletednodesintoanarray

How?

– Divide&conquerAVLTreebuild – Totaltime:

Whywecared!

slide-11
SLIDE 11

11

ThinkingAboutAVL

  • Observations

+WorstcaseheightofanAVLtreeisabout1.44logn +Insert,Find,DeleteinworstcaseO(logn) +Onlyone(singleordouble)rotationneededon insertion

  • O(logn)rotationsneededondeletion

+Compatiblewithlazydeletion

  • Heightfieldsmustbemaintained(or2-bitbalance)

AlternativestoAVLTrees

  • Changethebalancecriteria:

– Weightbalancedtrees

  • keepaboutthesamenumberofnodesineachsubtree
  • notnearlyasnice
  • Changethemaintenanceprocedure:

– Splaytrees

  • “blind”adjustingversionofAVLtrees

– noheightinformationmaintained!

  • insert/findalwaysrotatesnodetotheroot!
  • worstcasetimeisO(n)
  • amortizedtimeforalloperationsisO(logn)
  • mysterious,butoftenfasterthanAVLtreesinpractice

(betterlow-orderterms)

slide-12
SLIDE 12

12

SplayTrees

  • “blind”rebalancing

– noheightorbalanceinformationstored

  • amortizedtimeforalloperationsisO(logn)
  • worstcasetimeisO(n)
  • insert/findalwaysrotatesnodetotheroot!

– Goodlocality

  • mostcommonkeysmovehighintree

Idea

17 10

9 2 5 3

You’reforcedtomake areallydeepaccess: Sinceyou’redownthereanyway, fixupalotofdeepnodes!

slide-13
SLIDE 13

13

SplayOperations:Find

  • Find(x)
  • 1. doanormalBSTsearchtofindnsuch

that n->key=x

  • 2. moventorootbyseriesofzig-zag and

zig-zig rotations,followedbyafinalzig if necessary

Zig-Zag*

g X p Y

n

Z W

*Thisisjustadoublerotation

n

Y g W p Z X Helped Unchanged Hurt

slide-14
SLIDE 14

14

Zig-Zig

n

Z Y p X g W g W X p Y

n

Z

Zig

p X

n

Y Z

n

Z p Y X root root

slide-15
SLIDE 15

15

WhySplayingHelps

  • Nodenanditschildrenarealwayshelped(raised)
  • Exceptforfinalzig,nodesthatarehurtbyazig-

zagorzig-zigarelaterhelpedbyarotationhigher upthetree!

  • Result:

– shallow(zig)nodesmayincreasedepthbyoneortwo – helpednodesmaydecreasedepthbyalargeamount

  • Ifanoden ontheaccesspathisatdepthd before

thesplay,it’sataboutdepthd/2 afterthesplay

– Exceptionsaretheroot,thechildoftheroot,andthe nodesplayed

Locality

  • Assumem
  • n accessinatreeofsizen

– TotalamortizedtimeO(m logn) – O(logn)peraccessonaverage

  • Getsbetterwhenyouonlyaccessk

distinctitemsinthemaccesses.

– Exercise.

slide-16
SLIDE 16

16

SplayingExample

2 1 3 4 5 6 Find(6) 2 1 3 6 5 4 zig-zig

StillSplaying6

zig-zig 2 1 3 6 5 4 1 6 3 2 5 4

slide-17
SLIDE 17

17

AlmostThere,StayonTarget

zig 1 6 3 2 5 4 6 1 3 2 5 4

SplayAgain

Find(4) zig-zag 6 1 3 2 5 4 6 1 4 3 5 2

slide-18
SLIDE 18

18

ExampleSplayedOut

zig-zag 6 1 4 3 5 2 6 1 4 3 5 2

SplayTreeSummary

  • AlloperationsareinamortizedO(logn)time
  • Splayingcanbedonetop-down;better

because:

– onlyonepass – norecursionorparentpointersnecessary

  • InventedbySleatorandTarjan(1985),now

widelyusedinplaceofAVLtrees

  • Splaytreesarevery effectivesearchtrees

– relativelysimple – noextrafieldsrequired – excellentlocality properties:frequentlyaccessed keysarecheaptofind

slide-19
SLIDE 19

19

ToDo

  • Studyformidterm!
  • Readthroughsection4.7inthebook
  • Comments&Feedback
  • HomeworkIV(studying)
  • ProjectII– partB

ComingUp

  • MidtermnextWednesday
  • AHuge SearchTreeDataStructure

(notonthemidterm)