Skip to content

Instantly share code, notes, and snippets.

@drupol drupol/Example1.php
Last active Dec 30, 2018

Embed
What would you like to do?
PHPTree examples
<?php
declare(strict_types = 1);
use Graphp\GraphViz\GraphViz;
use drupol\phptree\Node\ValueNode;
use drupol\phptree\tests\Exporter\ValueGraph;
include './vendor/autoload.php';
$data = [
'is',
'a',
'fast',
'and',
'fun',
'library',
'to',
'manipulate',
'trees',
'data',
'structure',
];
$tree = new ValueNode('PHPTree');
foreach ($data as $value) {
$tree->add(new ValueNode($value));
}
$exporterGraph = new ValueGraph();
(new GraphViz())->setFormat('svg')->display($exporterGraph->export($tree));
<?php
declare(strict_types = 1);
use Graphp\GraphViz\GraphViz;
use drupol\phptree\Node\ValueNode;
use drupol\phptree\tests\Exporter\ValueGraph;
use drupol\phptree\Exporter\SimpleArray;
use drupol\phptree\Exporter\Text;
use drupol\phptree\Exporter\Ascii;
include './vendor/autoload.php';
$data = [
'is',
'a',
'fast',
'and',
'fun',
'library',
'to',
'manipulate',
'trees',
'data',
'structure',
];
$tree = new ValueNode('PHPTree', 2);
foreach ($data as $value) {
$tree->add(new ValueNode($value, 2));
}
$exporterAscii = new Ascii();
echo $exporterAscii->export($tree);
$exporterText = new Text();
echo $exporterText->export($tree);
$exporterArray = new SimpleArray();
\var_export($exporterArray->export($tree));
$exporterGraph = new ValueGraph();
(new GraphViz())->setFormat('svg')->display($exporterGraph->export($tree));
<?php
declare(strict_types = 1);
use Graphp\GraphViz\GraphViz;
use drupol\phptree\Node\ValueNode;
use drupol\phptree\tests\Exporter\ValueGraph;
use drupol\phptree\Exporter\SimpleArray;
use drupol\phptree\Exporter\Text;
include './vendor/autoload.php';
$data = [
'is',
'a',
'fast',
'and',
'fun',
'library',
'to',
'manipulate',
'trees',
'data',
'structure',
];
$tree = new ValueNode('PHPTree', 1);
foreach ($data as $value) {
$tree->add(new ValueNode($value, 1));
}
$exporterGraph = new ValueGraph();
(new GraphViz())->setFormat('svg')->display($exporterGraph->export($tree));
<?php
declare(strict_types = 1);
use Graphp\GraphViz\GraphViz;
use drupol\phptree\Node\ValueNode;
use drupol\phptree\tests\Exporter\ValueGraph;
use drupol\phptree\Exporter\SimpleArray;
use drupol\phptree\Exporter\Text;
include './vendor/autoload.php';
$data = [
'1006' => 'Conseil de la Commission communautaire flamande',
'1007' => 'Parlement francophone bruxellois',
'1008' => 'Chambre des représentants',
'1009' => 'Sénat de Belgique',
'1010' => 'Cité administrative de l\'État',
'1011' => 'Parlement flamand',
'1012' => 'Parlement de la Communauté française',
'1020' => 'Laeken',
'1030' => 'Schaerbeek',
'1031' => 'Organisations sociales chrétiennes',
'1033' => 'RTL-TVI',
'1035' => 'Ministère de la Région de Bruxelles-Capitale',
'1040' => 'Etterbeek',
'1041' => 'International Press Center',
'1043' => 'VRT',
'1044' => 'RTBF',
'1045' => 'Direction d\'Immatriculation des Véhicules (DIV)',
'1046' => 'Service Européen pour l\'action extérieure',
'1047' => 'Parlement européen',
'1048' => 'Conseil de l\'Union européenne',
'1049' => 'Commission européenne',
'1050' => 'Ixelles',
'1060' => 'Saint-Gilles',
'1070' => 'Anderlecht',
'1080' => 'Molenbeek-Saint-Jean',
'1081' => 'Koekelberg',
'1082' => 'Berchem-Sainte-Agathe',
'1083' => 'Ganshoren',
'1090' => 'Jette',
'1100' => 'Chèques postaux',
'1105' => 'Service social de la Poste',
'1110' => 'Organisation du traité de l\'Atlantique nord (OTAN)',
'1120' => 'Neder-Over-Heembeek',
'1130' => 'Haren',
'1140' => 'Evere',
'1150' => 'Woluwe-Saint-Pierre',
'1160' => 'Auderghem',
'1170' => 'Watermael-Boitsfort',
'1180' => 'Uccle',
'1190' => 'Forest',
'1200' => 'Woluwe-Saint-Lambert',
'1210' => 'Saint-Josse-ten-Noode',
'1212' => 'Service public fédéral Mobilité et Transports',
];
$tree = new ValueNode('Bruxelles');
foreach (['10', '11', '12'] as $prefix) {
$codes = [];
foreach ($data as $key => $value) {
if (0 === \strpos((string) $key, $prefix)) {
$tree->add(new ValueNode($key . ' ' . $value));
$codes[$key] = $value;
}
}
}
$exporterGraph = new ValueGraph();
(new GraphViz())->setFormat('svg')->display($exporterGraph->export($tree));
<?php
declare(strict_types = 1);
namespace drupol\phptree;
use drupol\phptree\Modifier\Reverse;
use drupol\phptree\Node\ValueNode;
use drupol\phptree\tests\Exporter\ValueGraph;
use drupol\phptree\Traverser\InOrder;
use Graphp\GraphViz\GraphViz;
include 'vendor/autoload.php';
$data = \range('A', 'Z');
$tree = new ValueNode('root', 2);
foreach ($data as $key => $value) {
$node = new ValueNode($value, 2);
$tree->add($node);
}
$graphViz = new GraphViz();
$exporterGraph = new ValueGraph();
$graphViz->setFormat('svg')->display($exporterGraph->export($tree));
$modifier = new Reverse();
$tree = $modifier->modify($tree);
$graphViz = new GraphViz();
$exporterGraph = new ValueGraph();
$graphViz->setFormat('svg')->display($exporterGraph->export($tree));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.