Click here to load reader

Datastrukturer och algoritmer - IDA > Home · PDF file1 Inledning Det här kompendiet ger en översikt över vanligt förekommande datastrukturer och algoritmer, i praktisk användning,

  • View
    215

  • Download
    0

Embed Size (px)

Text of Datastrukturer och algoritmer - IDA > Home · PDF file1 Inledning Det här kompendiet...

  • Datastrukturer och algoritmerKompendium/komplettering

    Tommy Olsson, Institutionen fr datavetenskap, 2013

  • Innehll

    Inledning ................................................................................................. 1

    1 Skiplista .................................................................................................. 31.1 Skiplistans uppbyggnad ................................................................................ 31.2 Skapa skiplista .............................................................................................. 41.3 Ska i skiplista .............................................................................................. 41.4 Insttning i skiplista ...................................................................................... 51.5 Borttagning ur skiplista ................................................................................ 51.6 Analys ........................................................................................................... 51.7 Litteratur ....................................................................................................... 5

    2 Trd ......................................................................................................... 72.1 Terminologi .................................................................................................. 72.2 Trdtraversering ........................................................................................ 102.3 Uttryckstrd ............................................................................................... 112.3.1 Generering av uttryckstrd .................................................................... 112.4 Sktrd ........................................................................................................ 132.4.1 Binra sktrd ........................................................................................ 142.4.2 Balanserade binra sktrd .................................................................... 152.4.3 AVL-trd ............................................................................................... 162.4.4 AVL-trdsrotationer .............................................................................. 172.4.5 Rd-svarta trd ...................................................................................... 192.4.6 Insttning i bottom-up rd-svarta trd ................................................... 202.4.7 Insttning i top-down rd-svarta trd .................................................... 212.4.8 Splaytrd ................................................................................................ 222.4.9 Bottom-up-splaytrd .............................................................................. 222.4.10 Top-down-splaytrd ............................................................................... 232.4.11 B-trd ..................................................................................................... 262.4.12 Insttning i B-trd .................................................................................. 262.4.13 Borttagning ur B-trd ............................................................................. 28

    3 Hashtabeller .......................................................................................... 313.1 Hashfunktioner ........................................................................................... 313.1.1 Frbearbetning av nycklar ..................................................................... 313.1.2 Val av hashfunktion ............................................................................... 323.1.3 Divisionsmetoden .................................................................................. 323.2 Kollisionshantering .................................................................................... 333.2.1 Linjr sondering ..................................................................................... 333.2.2 Kvadratisk sondering ............................................................................. 333.2.3 Dubbelhashning ..................................................................................... 343.2.4 Separat lnkning .................................................................................... 353.3 Omhashning ................................................................................................ 353.4 Linjrhashning ............................................................................................ 353.5 versikt av hashfunktioner ........................................................................ 373.5.1 Sifferurvalsmetoden, sifferanalysmetoden ............................................ 373.5.2 Divisionsmetoden .................................................................................. 373.5.3 Mittkvadratmetoden ............................................................................... 383.5.4 Folding-metoder .................................................................................... 383.5.5 Lngdberoendemetoder ......................................................................... 393.5.6 Slumpmetoden ....................................................................................... 39

    iii

  • 3.5.7 Radixmetoden ........................................................................................ 393.5.8 Perfekta hashfunktioner ......................................................................... 39

    4 Prioritetsk och heap ............................................................................ 414.1 Heap ............................................................................................................ 414.1.1 Binr heap .............................................................................................. 424.1.2 Insttning i binr min-heap .................................................................... 424.1.3 Borttagning ur binr min-heap .............................................................. 44

    5 Sortering ............................................................................................... 455.1 Interna sorteringsmetoder ........................................................................... 465.1.1 Bubblesort .............................................................................................. 465.1.2 Shakersort .............................................................................................. 475.1.3 Selectionsort .......................................................................................... 475.1.4 Insertionsort ........................................................................................... 485.1.5 Shellsort ................................................................................................. 495.1.6 Heapsort ................................................................................................. 515.1.7 Mergesort ............................................................................................... 525.1.8 Quicksort ............................................................................................... 545.2 Distributiva sorteringsmetoder ................................................................... 585.2.1 Count Sort .............................................................................................. 585.2.2 Radix Sort .............................................................................................. 595.3 Externa sorteringsmetoder .......................................................................... 605.3.1 Balanserad tvvgs samsortering ........................................................... 605.3.2 Naturlig tvvgs samsortering ............................................................... 615.3.3 Mngvgs samsortering ......................................................................... 625.3.4 Polyphase Merge ................................................................................... 625.3.5 Replacement Selection .......................................................................... 635.4 Indirekt sortering ........................................................................................ 64

    6 Interpolationsskning ........................................................................... 656.1 Litteratur ..................................................................................................... 66

    iv

  • 1

    InledningDet hr kompendiet ger en versikt ver vanligt frekommande datastrukturer och algoritmer, i praktisk anvndning, teoretiskt eller historiskt.

    Underrubriken Komplettering avspeglar att kompendiet har en del luckor d det gller innehll. Till exempel behandlas inte enkla listor, stack eller k, utan endast den avancerade liststrukturen skiplista tas upp. Inte heller ingr mngder, grafer och annat som kan frvntas i en mer heltck-ande versikt. Analys av datastrukturer och algoritmer har antingen utelmnats eller genomfrs mer resonemangsmssigt n teoretiskt.

  • 2

  • 1 SkiplistaSkiplistan bygger p en sannolikhetsbaserad balansering, i stllet fr de striktare former av balan-sering som anvnds i vissa trd. Det gr att algoritmerna fr insttning och borttagning i en skiplista blir bde enklare och snabbare n motsvarande operationer i balanserade trd.

    En skiplista r i grunden en ordnad, enkellnkad lista men vissa listnoder har flera framtpekare, vilket mjliggr snabbare skning n i en vanlig enkellnkad lista. Skiplistan r, trots de extra pekarna, minneseffektiv och kan konstrueras med en marginell kning av antalet pekare per list-element. Ingen annan information behver heller lagras i noderna.

    Figur 1. Exempel p enkellnkad lista och skiplistor.

    1.1 Skiplistans uppbyggnad

    I en enkellnkad lista

Search related