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 full-size slide

  2. unidirectional
    acyclic graph

    View full-size slide

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

    View full-size slide

  4. data structure

    View full-size slide

  5. tree with other
    data structures

    View full-size slide

  6. tree-linear
    correspondence

    View full-size slide

  7. context
    reduction

    View full-size slide

  8. extensibility

    View full-size slide

  9. simple PHP obfuscator
    nikic/PHP-Parser

    View full-size slide

  10. 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 full-size slide

  11. 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 full-size slide

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

    View full-size slide

  13. 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 full-size slide

  14. 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 full-size slide

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

    View full-size slide

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

    View full-size slide

  17. naive approach
    linear search

    View full-size slide

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

    View full-size slide

  19. tree approach
    trie / radix tree

    View full-size slide

  20. t
    r
    i e
    trie
    e e
    tree

    View full-size slide

  21. 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 full-size slide

  22. 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 full-size slide

  23. trie vs radix tree vs PATRICIA vs ...

    View full-size slide

  24. tr
    ie ee
    trie tree

    View full-size slide

  25. shortcode parser
    thunderer/shortcode

    View full-size slide

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

    View full-size slide

  27. shortcode
    name BBCode parameters content
    arg value

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  30. $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 full-size slide

  31. XML structure

    View full-size slide


  32. say hello to

    View full-size slide

  33. $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 full-size slide

  34. approval tree

    View full-size slide

  35. 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 full-size slide

  36. recursive approach
    recursive node traversal

    View full-size slide

  37. 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 full-size slide

  38. linear approach
    sequential node traversal

    View full-size slide

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

    View full-size slide

  40. object graph

    View full-size slide

  41. User
    Category Tag Article
    Category Tag User
    Hydration
    Normalization

    View full-size slide

  42. treehanks!
    @tmmx

    View full-size slide

  43. 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 full-size slide