Advertisement
tsuedeun

Untitled

Jun 5th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.86 KB | None | 0 0
  1. <?php
  2. class treeNode {
  3.     public $data, $parent, $firstSon, $brother;
  4.  
  5.     public function __construct($data) {
  6.         $this->data = $data;
  7.         $this->parent = null;
  8.         $this->firstSon = null;
  9.         $this->brother = null;
  10.     }
  11. }
  12.  
  13. class Ntree {
  14.     public $koren;
  15.  
  16.     public function __construct() {
  17.         $this->koren = null;
  18.     }
  19.  
  20.     public function addTreeNode($node, $valParent = null) {
  21.         if ($this->koren == null) {
  22.             $this->koren = $node;
  23.             return "Корень успешно позажен со значением $node->data<br>";
  24.         } elseif ($valParent == null) {
  25.             return 'Введите значения родителя вставляемого элемента<br>';
  26.         } elseif ($this->koren->data == $valParent){
  27.             $node->parent = $this->koren;
  28.             if ($this->koren->firstSon == null) {
  29.                 $this->koren->firstSon = $node;
  30.                 return "Вставка прошла успешно к корню! Вставлен элемент $node->data<br>";
  31.             } else {
  32.                 $node->brother = $this->koren->firstSon->brother;
  33.                 $this->koren->firstSon->brother = $node;
  34.                 return "Вставка прошла успешно к корню! Вставлен элемент $node->data<br>";
  35.             }
  36.         } else {
  37.             $path = [];
  38.             $stack = new Stack();
  39.             $stack->put($this->koren);
  40.             while ($stack->last != null) {
  41.                 $curr = $stack->get();
  42.                 $path[$curr->data] = true;
  43.                 foreach ($this->getEdges($curr) as $node2) {
  44.                     if ($node2->data == $valParent) {
  45.                         $node->parent = $node2;
  46.                         if ($node2->firstSon == null) {
  47.                             $node2->firstSon = $node;
  48.                             return "Вставка прошла успешно! Вставлен элемент $node->data<br>";
  49.                         } else {
  50.                             $node->brother = $node2->firstSon->brother;
  51.                             $node2->firstSon->brother = $node;
  52.                             return "Вставка прошла успешно (в брата)! Вставлен элемент $node->data<br>";
  53.                         }
  54.                     } elseif (!$path[$node2->data] && !$stack->contains($node2)) {
  55.                         $stack->put($node2);
  56.                     }
  57.                 }
  58.             }
  59.             return 'Такого родителя нет! <br>';
  60.         }
  61.     }
  62.  
  63.     function getEdges($node) {
  64.         if ($node->firstSon)
  65.             $currentSon = $node->firstSon;
  66.             while ($currentSon != null) {
  67.                 yield $currentSon;
  68.                 $currentSon = $currentSon->brother;
  69.             }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement