fork download
  1. <?php
  2. class TreeElement
  3. {
  4. protected $nodeName;
  5. protected $childNodes=[];
  6. protected $parentNode;
  7.  
  8.  
  9. public function __construct($nodeName){
  10. $this->nodeName = $nodeName;
  11. }
  12.  
  13. public function getNodeName(){
  14. return $this->nodeName;
  15. }
  16.  
  17. public function findDescendant($name){
  18. foreach($this->childNodes as $child){
  19. if($child->getNodeName()==$name){
  20. return $child;
  21. }
  22. else{
  23. $desc = $child->findDescendant($name);
  24. if($desc && $desc->getNodeName()==$name){
  25. return $desc;
  26. }
  27. }
  28. }
  29. return null;
  30. }
  31.  
  32. public function countChildNodes(){
  33. return count($this->childNodes);
  34. }
  35.  
  36. public function countDescendants(){
  37. $descCount = 0;
  38. foreach ($this->childNodes as $child) {
  39. $descCount++;
  40. $descCount+= $child->countDescendants();
  41. }
  42. return $descCount;
  43. }
  44.  
  45.  
  46. public function addChild(TreeElement $child){
  47. $this->childNodes[] = $child;
  48. $child->setParentNode($this);
  49. }
  50.  
  51. public function removeChild($node){
  52. foreach ($this->childNodes as $key => $child) {
  53. if($child == $node){
  54. unset($this->childNodes[$key]);
  55. }
  56. }
  57. }
  58.  
  59. public function isRoot(){
  60. if(!$this->parentNode){
  61. return true;
  62. }
  63. return false;
  64. }
  65.  
  66. public function getDepth(){
  67. $depth = 0;
  68. if($this->parentNode){
  69. $depth++;
  70. $depth+= $this->parentNode->getDepth();
  71. }
  72. return $depth;
  73. }
  74.  
  75. public function getChildNodes(){
  76. return $this->childNodes;
  77. }
  78.  
  79. public function setChildNodes($newNodes){
  80. $this->childNodes = $newNodes;
  81. }
  82.  
  83. public function getParentNode(){
  84. return $this->parentNode;
  85. }
  86.  
  87. public function setParentNode($node){
  88. $this->parentNode = $node;
  89. }
  90.  
  91. public function isDescendant($node){
  92. if($node->findDescendant($this->nodeName)){
  93. return true;
  94. }
  95. return false;
  96. }
  97.  
  98. public function isAncestor($node){
  99. if($this->findDescendant($node->getNodeName())){
  100. return true;
  101. }
  102. return false;
  103. }
  104.  
  105. public function getNextSibling(){
  106. $parent = $this->parentNode;
  107. $previous = NULL;
  108. foreach ($parent->getChildNodes() as $child) {
  109. if($previous==$this){
  110. return $child;
  111. }
  112. $previous = $child;
  113. }
  114. return false;
  115. }
  116.  
  117. public function getPreviousSibling(){
  118. $parent = $this->parentNode;
  119. $previous = NULL;
  120. foreach ($parent->getChildNodes() as $child) {
  121. if($this == $child){
  122. return $previous;
  123. }
  124. $previous = $child;
  125. }
  126. return false;
  127. }
  128.  
  129. public function moveSibling($n){
  130. $parent = $this->parentNode;
  131. $newArray = [];
  132. for($i = 0; $i<count($parent->getChildNodes()); $i++){
  133. if($n===$i){
  134. $newArray[] = $this;
  135. $parent->removeChild($this->nodeName);
  136. $n = NULL;
  137. $i--;
  138. }
  139. else{
  140. $newArray[] = $parent->getChildNodes()[$i];
  141. }
  142. }
  143. $parent->setChildNodes($newArray);
  144. }
  145.  
  146. public function moveNode($parent){
  147. $this->parentNode->removeChild($this->nodeName);
  148. $parent->addChild($this);
  149. }
  150.  
  151. public function displayAsCatalog(){
  152. $children = "";
  153. $depth = "";
  154. if($this->countDescendants()>0){
  155. $children = " (".$this->countDescendants().") ";
  156. }
  157.  
  158. for($i = 0; $i<$this->getDepth();$i++){
  159. $depth.="...";
  160. }
  161. echo $depth.$this->nodeName.$children."\n";
  162. foreach ($this->childNodes as $child) {
  163. $child->displayAsCatalog();
  164. }
  165. }
  166. }
  167.  
  168.  
  169. $tech = new TreeElement("Бытовая техника");
  170. $tech->addChild(new TreeElement('Телевизоры'));
  171. $televisors = $tech->findDescendant('Телевизоры');
  172. $televisors->addChild(new TreeElement('LCD-телевизоры'));
  173. $televisors->addChild(new TreeElement('Плазменные'));
  174. $tech->addChild(new TreeElement('Холодильники'));
  175. $refrigirators = $tech->findDescendant('Холодильники');
  176. $refrigirators->addChild(new TreeElement('Маленькие'));
  177. $refrigirators->addChild(new TreeElement('Средние'));
  178. $refrigirators->addChild(new TreeElement('Большие'));
  179. $tech->displayAsCatalog();
Success #stdin #stdout 0.03s 52480KB
stdin
Standard input is empty
stdout
Бытовая техника (7) 
...Телевизоры (2) 
......LCD-телевизоры
......Плазменные
...Холодильники (3) 
......Маленькие
......Средние
......Большие