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