Tomasz Kowalczyk
April 03, 2017
380

# 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

1. solving problems using
Trees
@tmmx

2. Tree?

3. unidirectional
acyclic graph

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

5. data structure

6. recursion

7. tree with other
data structures

8. tree-linear
correspondence

9. Why?

10. context
reduction

11. extensibility

12. Examples

13. AST

14. simple PHP obfuscator
nikic/PHP-Parser

15. 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. 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. AllowedYearsValidator
isYearAllowed
\$dt \$currentYear \$isCurrentYear
\$validator \$dt
AST

18. 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. 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. search tree

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

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

23. naive approach
linear search

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

25. tree approach

26. retrieval

27. t
r
i e
trie
e e
tree

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

30. trie vs radix tree vs PATRICIA vs ...

31. tr
ie ee
trie tree

32. parser

33. shortcode parser
thunderer/shortcode

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

35. shortcode
name BBCode parameters content
arg value

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

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

38. \$handlers = new HandlerContainer();
return ''.\$s->getContent().'';
});
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));

39. XML structure

40. say hello to

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

42. approval tree

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

44. recursive approach
recursive node traversal

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

46. linear approach
sequential node traversal

47. A
B C
D E
filter root nodes
map to contact information
F G
failed

48. object graph

49. User
Category Tag Article
Category Tag User
Hydration
Normalization

50. Summary

51. ?\$questions

52. treehanks!
@tmmx

53. Resources