|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Fhaculty\Graph\Algorithm\Tree; |
| 4 | + |
| 5 | +use Fhaculty\Graph\Algorithm\BaseGraph; |
| 6 | +use Fhaculty\Graph\Graph; |
| 7 | +use Fhaculty\Graph\Vertex; |
| 8 | +use Fhaculty\Graph\Exception\UnderflowException; |
| 9 | +use Fhaculty\Graph\Exception\UnexpectedValueException; |
| 10 | +use Fhaculty\Graph\Algorithm\Search\StrictDepthFirst; |
| 11 | + |
| 12 | +/** |
| 13 | + * Abstract base class for tree algorithms |
| 14 | + * |
| 15 | + * This abstract base class provides the base interface for working with |
| 16 | + * graphs that represent a tree. |
| 17 | + * |
| 18 | + * A tree is a connected Graph (single component) with no cycles. Every Tree is |
| 19 | + * a Graph, but not every Graph is a Tree. |
| 20 | + * |
| 21 | + * A |
| 22 | + * / \ |
| 23 | + * B C |
| 24 | + * / \ |
| 25 | + * D E |
| 26 | + * |
| 27 | + * Special cases are undirected trees (like the one pictured above), handled via |
| 28 | + * Tree\Undirected and directed, rooted trees (InTree and OutTree), handled via |
| 29 | + * Tree\BaseDirected. |
| 30 | + * |
| 31 | + * @link http://en.wikipedia.org/wiki/Tree_%28graph_theory%29 |
| 32 | + * @link http://en.wikipedia.org/wiki/Tree_%28data_structure%29 |
| 33 | + * @see Undirected for an implementation of these algorithms on (undirected) trees |
| 34 | + * @see BaseDirected for an abstract implementation of these algorithms on directed, rooted trees |
| 35 | + */ |
| 36 | +abstract class Base extends BaseGraph |
| 37 | +{ |
| 38 | + /** |
| 39 | + * checks whether the given graph is actually a tree |
| 40 | + * |
| 41 | + * @return boolean |
| 42 | + */ |
| 43 | + abstract public function isTree(); |
| 44 | + |
| 45 | + /** |
| 46 | + * checks if the given $vertex is a leaf (outermost vertext) |
| 47 | + * |
| 48 | + * leaf vertex is also known as leaf node, external node or terminal node |
| 49 | + * |
| 50 | + * @param Vertex $vertex |
| 51 | + * @return boolean |
| 52 | + */ |
| 53 | + abstract public function isVertexLeaf(Vertex $vertex); |
| 54 | + |
| 55 | + /** |
| 56 | + * checks if the given $vertex is an internal vertex (somewhere in the "middle" of the tree) |
| 57 | + * |
| 58 | + * internal vertex is also known as inner node (inode) or branch node |
| 59 | + * |
| 60 | + * @param Vertex $vertex |
| 61 | + * @return boolean |
| 62 | + */ |
| 63 | + abstract public function isVertexInternal(Vertex $vertex); |
| 64 | + |
| 65 | + /** |
| 66 | + * get array of leaf vertices (outermost vertices with no children) |
| 67 | + * |
| 68 | + * @return Vertex[] |
| 69 | + * @uses Graph::getVertices() |
| 70 | + * @uses self::isVertexLeaf() |
| 71 | + */ |
| 72 | + public function getVerticesLeaf() |
| 73 | + { |
| 74 | + return array_filter($this->graph->getVertices(), array($this, 'isVertexLeaf')); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * get array of internal vertices |
| 79 | + * |
| 80 | + * @return Vertex[] |
| 81 | + * @uses Graph::getVertices() |
| 82 | + * @uses self::isVertexInternal() |
| 83 | + */ |
| 84 | + public function getVerticesInternal() |
| 85 | + { |
| 86 | + return array_filter($this->graph->getVertices(), array($this, 'isVertexInternal')); |
| 87 | + } |
| 88 | +} |
0 commit comments