Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Solving problems using trees

Solving problems using trees

Tree is one of the most powerful data structures, and it is present in various kinds of problems. I want to show you how trees helped me design certain solutions so that you can do the same when the right time comes.

Tomasz Kowalczyk

April 03, 2017
Tweet

More Decks by Tomasz Kowalczyk

Other Decks in Programming

Transcript

  1. solving problems using
    Trees
    @tmmx

    View Slide

  2. View Slide

  3. Tree?

    View Slide

  4. unidirectional
    acyclic graph

    View Slide

  5. binary
    red-black
    B-tree
    T-tree
    2-3-4 tree
    AVL

    View Slide

  6. data structure

    View Slide

  7. recursion

    View Slide

  8. tree with other
    data structures

    View Slide

  9. tree-linear
    correspondence

    View Slide

  10. Why?

    View Slide

  11. context
    reduction

    View Slide

  12. extensibility

    View Slide

  13. Examples

    View Slide

  14. AST

    View Slide

  15. simple PHP obfuscator
    nikic/PHP-Parser

    View Slide

  16. declare(strict_types=1);
    final class AllowedYearsValidator
    {
    public function isYearAllowed(DateTimeImmutable $dt): bool
    {
    $currentYear = $dt->format('Y');
    $isCurrentYear = '2017' === $currentYear;
    return $isCurrentYear && in_array($currentYear, ['2016', '2018']);
    }
    }
    $validator = new AllowedYearsValidator();
    $dt = DateTimeImmutable::createFromFormat('Y', '2016');
    assert(false === $validator->isYearAllowed($dt));

    View Slide

  17. declare(strict_types=1);
    final class AllowedYearsValidator
    {
    public function isYearAllowed(DateTimeImmutable $dt): bool
    {
    $currentYear = $dt->format('Y');
    $isCurrentYear = '2017' === $currentYear;
    return $isCurrentYear && in_array($currentYear, ['2016', '2018']);
    }
    }
    $validator = new AllowedYearsValidator();
    $dt = DateTimeImmutable::createFromFormat('Y', '2016');
    assert(false === $validator->isYearAllowed($dt));

    View Slide

  18. AllowedYearsValidator
    isYearAllowed
    $dt $currentYear $isCurrentYear
    $validator $dt
    AST

    View Slide

  19. declare(strict_types=1);
    final class c0
    {
    public function m0(DateTimeImmutable $v0): bool
    {
    $v1 = $v0->format('Y');
    $v2 = '2017' === $v1;
    return $v2 && in_array($v1, ['2016', '2018']);
    }
    }
    $v3 = new c0();
    $v0 = DateTimeImmutable::createFromFormat('Y', '2016');
    assert(false === $v3->m0($v0));

    View Slide

  20. class c0 { public function
    m0(DateTimeImmutable $v0): bool { $v1 =
    $v0->format('Y'); $v2 = '2017' === $v1;
    return $v2 && in_array($v1, ['2016',
    '2018']); }} $v3 = new c0(); $v0 =
    DateTimeImmutable::createFromFormat('Y',
    '2016'); assert(false === $v3->m0($v0));

    View Slide

  21. search tree

    View Slide

  22. N non-overlapping ranges
    and a number
    find matching range

    View Slide

  23. 004604-004660
    213674-213701
    ...
    960101-961999
    962387-962414
    004651? 213702? 961446?

    View Slide

  24. naive approach
    linear search

    View Slide

  25. O(1) memory-heavy approach
    precomputed list

    View Slide

  26. tree approach
    trie / radix tree

    View Slide

  27. retrieval

    View Slide

  28. t
    r
    i e
    trie
    e e
    tree

    View Slide

  29. 0
    0
    4
    6
    0 6
    4 0
    2
    1
    3
    6
    7 0
    4 1
    7
    9
    6
    0
    1
    0 9
    1 9
    9
    1 2
    8
    7
    3
    1
    4
    4
    start end start end start end start end

    View Slide

  30. 0
    0
    4
    6
    0 6
    4 0
    2
    1
    3
    6
    7 0
    4 1
    7
    9
    6
    0
    1
    0 9
    1 9
    9
    1 2
    8
    7
    3
    1
    4
    4
    004651
    213702
    961446
    962001
    start end start end start end start end

    View Slide

  31. trie vs radix tree vs PATRICIA vs ...

    View Slide

  32. tr
    ie ee
    trie tree

    View Slide

  33. parser

    View Slide

  34. shortcode parser
    thunderer/shortcode

    View Slide

  35. [foo arg=val] data [inner=x /] code [/foo]

    View Slide

  36. shortcode
    name BBCode parameters content
    arg value

    View Slide

  37. shortcode
    “foo” null parameters data [inner /] code
    “arg” “val”

    View Slide

  38. shortcode
    “inner” “x” parameters null
    null

    View Slide

  39. $handlers = new HandlerContainer();
    $handlers->add('foo', function(ShortcodeInterface $s) {
    return ''.$s->getContent().'';
    });
    $handlers->add('inner', function(ShortcodeInterface $s) {
    return date('Y.m.d');
    });
    $processor = new Processor(new RegularParser(), $handlers);
    $text = '[foo arg=val] data [inner=x /] code [/foo]';
    $expected = ' data 2017.04.03 code ';
    assert($expected === $processor->process($text));

    View Slide

  40. XML structure

    View Slide





  41. View Slide













  42. View Slide





  43. View Slide













  44. View Slide


  45. say hello to

    View Slide

  46. $schemaDir = __DIR__.'/xsd';
    $targetDir = __DIR__.'/xml';
    $log = new NullLogger();
    $ctr = new Counter();
    $fs = new FilePutContentsFilesystem($targetDir);
    $ns = new CustomNamespaceResolver();
    $analyzer = new XsdAnalyzer($log);
    $schemas = $analyzer->createFromDirectories([$schemaDir]);
    $generator = new PrimitivePhpGenerator($fs, $ns, $log, $ctr);
    $generator->generate($schemas);

    View Slide

  47. approval tree

    View Slide

  48. A
    B C
    D E
    A requires B and C
    B requires D and E
    E requires F and G
    ...but D and E can’t
    confirm before B,
    F and G must wait
    for E, so...
    Who can vote?
    Did vote pass?
    F G

    View Slide

  49. recursive approach
    recursive node traversal

    View Slide

  50. A
    B C
    D E
    Check B and C,
    when in B check D
    and E, when in E
    check F and G…
    and if something
    failed somewhere
    then return all the
    information down.
    F G
    failed

    View Slide

  51. linear approach
    sequential node traversal

    View Slide

  52. A
    B C
    D E
    filter root nodes
    filter already decided nodes
    filter already notified nodes
    map to contact information
    F G
    failed

    View Slide

  53. object graph

    View Slide

  54. User
    Category Tag Article
    Category Tag User
    Hydration
    Normalization

    View Slide

  55. Summary

    View Slide

  56. ?$questions

    View Slide

  57. treehanks!
    @tmmx

    View Slide

  58. Resources
    https://en.wikipedia.org/wiki/Radix_tree
    https://en.wikipedia.org/wiki/Trie
    http://stackoverflow.com/questions/14708134/what-is-the-difference-between-trie-and-radix-trie-data-structures
    Repositories
    https://github.com/nikic/PHP-Parser
    https://github.com/thunderer/Shortcode
    https://github.com/thunderer/Serializard
    https://github.com/thunderer/XSDragon
    Images
    https://www.flickr.com/photos/ghor/8394379683 (forest)
    https://www.flickr.com/photos/skyseeker/14404947216 (lightning)

    View Slide