fork download
  1. <?php
  2.  
  3. class Node {
  4.  
  5. private $parentNode = NULL;
  6. private $child = array();
  7. protected $name;
  8.  
  9.  
  10. public function __construct($name) {
  11. $this->name = $name;
  12. }
  13. public function __toString() {
  14. $node = "\nNameNode: " . $this->name . "\n";
  15. $node .= "ParentName: " . $this->parentNode->getName() . "\n";
  16. foreach ($this->child as $child) {
  17. $node .= "ChildName: " . $child->getName() ."\n";
  18. }
  19. return $node;
  20. }
  21. public function getName() {
  22. return $this->name;
  23. }
  24.  
  25. public function getParentNode(){
  26. if (isset($this->parentNode)) {
  27. return $this->parentNode;
  28. }
  29. return NULL;
  30. }
  31. public function getChildrenNode() {
  32. return $this->child;
  33. }
  34.  
  35. public function getAllParents(){
  36. if (NULL == $this->getParentNode()) {
  37. return array();
  38. } else {
  39. $parent = $this->getParentNode();
  40. $parents = $parent->getAllParents();
  41. $parents[] = $parent;
  42. return $parents;
  43. }
  44. }
  45. public function getAllChildren(){
  46. $childrenNode = $this->getChildrenNode();
  47. $children = $childrenNode;
  48. foreach ($childrenNode as $child) {
  49. $children = array_merge($children, $child->getAllChildren());
  50. }
  51.  
  52. return $children;
  53. }
  54.  
  55. public function getDepth() {
  56. $children = $this->child;
  57. $depth = -1;
  58. //echo $this->name() . '->';
  59. foreach ($children as $child) {
  60. $depth = max($depth, $seed->getDepth());
  61. }
  62. $depth++;
  63. return $depth;
  64. }
  65.  
  66. public function getDescendantsCount(){
  67. $DescendantsCount = 0;
  68. $children = $this->child;
  69. echo $this->getName() . '->';
  70. foreach ($children as $child) {
  71. $DescendantsCount++;
  72. $DescendantsCount += $child->getDescendantsCount();
  73. }
  74. return $DescendantsCount;
  75. }
  76.  
  77. public function appendChild(Node $child) {
  78. if ($this == $child) {
  79. try {
  80. throw new Exception("ОШИБКА! Нельзя делать обьект " . $this->getName() . " родителем самого себя.\n");
  81. } catch(Exception $e) {
  82. echo $e->getMessage();
  83. }
  84. }
  85. $child->remove();
  86.  
  87. $this->child[] = $child;
  88. $child->parentNode = $this;
  89. }
  90.  
  91. public function remove(){
  92. if (isset($this->parentNode)) {
  93. echo "\nУдаление\n";
  94. $parentNode = $this->parentNode;
  95.  
  96. $children = $parentNode->child;
  97. $key = array_search($this , $children);
  98. unset($parentNode->child[$key]);
  99.  
  100. $this->parentNode = null;
  101. }
  102. }
  103.  
  104. public function getNextSibling(){
  105. $parentNode = $this->parentNode;
  106. if (NULL ==$parentNode) {
  107. return NULL;
  108. }
  109. $siblings = $parentNode->getChildrenNode();
  110.  
  111. $key = array_search( $siblings, $this);
  112. if (isset($siblings[$key+1])) {
  113. return $siblings[$key+1];
  114. } else {
  115. return NULL;
  116. }
  117. }
  118. public function getPreviousSibling(){
  119. $parentNode = $this->parentNode;
  120. if (NULL ==$parentNode) {
  121. return NULL;
  122. }
  123. $siblings = $parentNode->getChildrenNode();
  124. $key = array_search( $siblings, $this);
  125. if (isset($siblings[$key-1])) {
  126. return $siblings[$key-1];
  127. } else {
  128. return NULL;
  129. }
  130. }
  131.  
  132.  
  133. public function walk($function) {
  134. $allChildren = $this->getAllChildren();
  135. $return = array();
  136. $callback =
  137. function ( $child ) use ( $function, &$return) {
  138. $return[] = "Имя узла: " . $child->getName() . " Количество потомков: "
  139. . $child->$function();
  140. };
  141. array_walk( $allChildren, $callback );
  142. print_r($return);
  143. }
  144. // обходит всех потомков $node, для каждого вызывая функцию
  145.  
  146.  
  147.  
  148. }
  149.  
  150.  
  151. //создаём узлы 1 -> 2
  152. // -> 3 -> 4
  153. // -> 5
  154. $node1 = new Node('node1');
  155.  
  156. $node2 = new Node('node2');
  157. $node1->appendChild($node2);
  158.  
  159. $node3 = new Node('node3');
  160. $node2->appendChild($node3);
  161.  
  162. $node4 = new Node('node4');
  163. $node3->appendChild($node4);
  164.  
  165. $node5 = new Node('node5');
  166. $node1->appendChild($node5);
  167.  
  168. $node6 = new Node('node6');
  169. $node4->appendChild($node6);
  170.  
  171. // Выводем имя всех нод с отступом, чтобы получилось дерево
  172. $node1->walk(function ($child) {
  173. // найдем глубину потомка
  174. $parentsCount = count($child->getAllParents());
  175. echo str_repeat(' ', $parentsCount) . $child->getName() . "\n";
  176. });
  177.  
  178.  
Runtime error #stdin #stdout #stderr 0.01s 20568KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
PHP Fatal error:  Method name must be a string in /home/zuanPb/prog.php on line 140