Slide 1

Slide 1 text

solving problems using Trees @tmmx

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

Tree?

Slide 4

Slide 4 text

unidirectional acyclic graph

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

data structure

Slide 7

Slide 7 text

recursion

Slide 8

Slide 8 text

tree with other data structures

Slide 9

Slide 9 text

tree-linear correspondence

Slide 10

Slide 10 text

Why?

Slide 11

Slide 11 text

context reduction

Slide 12

Slide 12 text

extensibility

Slide 13

Slide 13 text

Examples

Slide 14

Slide 14 text

AST

Slide 15

Slide 15 text

simple PHP obfuscator nikic/PHP-Parser

Slide 16

Slide 16 text

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));

Slide 17

Slide 17 text

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));

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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));

Slide 20

Slide 20 text

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));

Slide 21

Slide 21 text

search tree

Slide 22

Slide 22 text

N non-overlapping ranges and a number find matching range

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

naive approach linear search

Slide 25

Slide 25 text

O(1) memory-heavy approach precomputed list

Slide 26

Slide 26 text

tree approach trie / radix tree

Slide 27

Slide 27 text

retrieval

Slide 28

Slide 28 text

t r i e trie e e tree

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

trie vs radix tree vs PATRICIA vs ...

Slide 32

Slide 32 text

tr ie ee trie tree

Slide 33

Slide 33 text

parser

Slide 34

Slide 34 text

shortcode parser thunderer/shortcode

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

shortcode name BBCode parameters content arg value

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

shortcode “inner” “x” parameters null null

Slide 39

Slide 39 text

$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));

Slide 40

Slide 40 text

XML structure

Slide 41

Slide 41 text

Slide 42

Slide 42 text

Slide 43

Slide 43 text

Slide 44

Slide 44 text

Slide 45

Slide 45 text

say hello to

Slide 46

Slide 46 text

$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);

Slide 47

Slide 47 text

approval tree

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

recursive approach recursive node traversal

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

linear approach sequential node traversal

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

object graph

Slide 54

Slide 54 text

User Category Tag Article Category Tag User Hydration Normalization

Slide 55

Slide 55 text

Summary

Slide 56

Slide 56 text

?$questions

Slide 57

Slide 57 text

treehanks! @tmmx

Slide 58

Slide 58 text

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)