Tomasz Kowalczyk
April 03, 2017
410

# 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.

April 03, 2017

## Transcript

15. ### <?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));
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));

18. ### <?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));
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));

28. ### 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
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 004651 213702 961446 962001 start end start end start end start end

38. ### \$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));

41. ### <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. ### <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. ### \$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. ### 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. ### 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. ### A B C D E filter root nodes filter already

decided nodes filter already notified nodes map to contact information F G failed

57. ### 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)