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.

Bb29f6afb2ea244a12c25e04d46af19c?s=128

Tomasz Kowalczyk

April 03, 2017
Tweet

Transcript

  1. solving problems using Trees @tmmx

  2. None
  3. Tree?

  4. unidirectional acyclic graph

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

  6. data structure

  7. recursion

  8. tree with other data structures

  9. tree-linear correspondence

  10. Why?

  11. context reduction

  12. extensibility

  13. Examples

  14. AST

  15. simple PHP obfuscator nikic/PHP-Parser

  16. <?php 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));
  17. <?php 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));
  18. AllowedYearsValidator isYearAllowed $dt $currentYear $isCurrentYear $validator $dt AST

  19. <?php 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));
  20. <?php 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));
  21. search tree

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

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

  24. naive approach linear search

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

  26. tree approach trie / radix tree

  27. retrieval

  28. t r i e trie e e tree

  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
  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
  31. trie vs radix tree vs PATRICIA vs ...

  32. tr ie ee trie tree

  33. parser

  34. shortcode parser thunderer/shortcode

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

  36. shortcode name BBCode parameters content arg value

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

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

  39. $handlers = new HandlerContainer(); $handlers->add('foo', function(ShortcodeInterface $s) { return '<foo>'.$s->getContent().'</foo>';

    }); $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 = '<foo> data 2017.04.03 code </foo>'; assert($expected === $processor->process($text));
  40. XML structure

  41. <root> <level attr=”val” /> <item /> </root>

  42. <xsd:element name="root"> <xsd:complexType> <xsd:sequence> <xsd:element name="level"> <xsd:complexType> <xsd:attribute name="attr"/> </xsd:complexType>

    </xsd:element> <xsd:element name="item"/> </xsd:sequence> </xsd:complexType> </xsd:element>
  43. <root> <level attr=”val” /> <item /> </root>

  44. <xsd:element name="root"> <xsd:complexType> <xsd:sequence> <xsd:element name="level"> <xsd:complexType> <xsd:attribute name="attr"/> </xsd:complexType>

    </xsd:element> <xsd:element name="item"/> </xsd:sequence> </xsd:complexType> </xsd:element>
  45. <XSDragon/> say hello to

  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);
  47. approval tree

  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
  49. recursive approach recursive node traversal

  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
  51. linear approach sequential node traversal

  52. A B C D E filter root nodes filter already

    decided nodes filter already notified nodes map to contact information F G failed
  53. object graph

  54. User Category Tag Article Category Tag User Hydration Normalization

  55. Summary

  56. ?$questions

  57. treehanks! @tmmx

  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)