Algorithms - String Processing - 01 - Trie

  • Upload
    -

  • View
    230

  • Download
    0

Embed Size (px)

Citation preview

  • 7/23/2019 Algorithms - String Processing - 01 - Trie

    1/11

    Trie (Letter Tree)

    Mostafa Saad

  • 7/23/2019 Algorithms - String Processing - 01 - Trie

    2/11

    TrieA simple but powerful data structure.

    It adds/searches for string in O(L)

    It is a tree with branches as letters

  • 7/23/2019 Algorithms - String Processing - 01 - Trie

    3/11

    Initiall! root e"ist

  • 7/23/2019 Algorithms - String Processing - 01 - Trie

    4/11

    Add abcd

    a

    d

    c

    b

  • 7/23/2019 Algorithms - String Processing - 01 - Trie

    5/11

    Add "#

    a

    d

    c

    b

    "

    #

  • 7/23/2019 Algorithms - String Processing - 01 - Trie

    6/11

    Add abf

    a

    d

    c

    b

    "

    #

    f

  • 7/23/2019 Algorithms - String Processing - 01 - Trie

    7/11

    Add "n

    a

    d

    c

    b

    "

    #

    f

    n

  • 7/23/2019 Algorithms - String Processing - 01 - Trie

    8/11

    Add ab

    a

    d

    c

    b

    "

    #

    f

    n

  • 7/23/2019 Algorithms - String Processing - 01 - Trie

    9/11

    Add bcd

    a

    d

    c

    b

    "

    #

    f

    n

    d

    c

    b

  • 7/23/2019 Algorithms - String Processing - 01 - Trie

    10/11

    SoSimple tree

    $ommon pre%"es are created once

    &as to 'now the nodes (red ones)

    $ould answer uestions such as*oes word e"ist+ ,re-uenc+

    *oes pre%" e"ist+ ,re-uenc+

    An -uestion is answered b a trace from root toma"imum a leaf

    So O(L) where L is word length

    $ould also print all strings on O(S)! where S ofwords

  • 7/23/2019 Algorithms - String Processing - 01 - Trie

    11/11

    Implementationode is similar to binar search tree! but we

    ha0e man children$aring with memor+ &ither ha0e lin'ed list for the set of nodes

    Or more e1cient. 2se maps

    Memor not issue3as an arra of nodes! its si#e the of characters

    ode could contain whate0er needed&.g. boolean to 'now full words

    &.g. integer for fre-uenc