Advertisement
Guest User

Untitled

a guest
Jun 15th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. $arvore = new No('Thiago');
  2.  
  3. $no = $arvore->addFilho('Alan');
  4. $no->addFilho('Thiago');
  5. $no->addFilho('Thiago');
  6.  
  7. $no = $arvore->addFilho('Robson');
  8. $no->addFilho('Maurício');
  9. $no1 = $no->addFilho('Bruno');
  10. $no1->addFilho('Rodolpho');
  11. $no1->addFilho('Guilherme');
  12. $no->addFilho('Xuxa');
  13.  
  14. $arvore->addFilho('Eduardo');
  15. $arvore->addFilho('Alexandre')->addFilho('Gabriel');
  16.  
  17. echo "n".$arvore->busca('Thiago'); //Deve retornar true, pois existe no nó da arvore
  18. echo "n".$arvore->busca('Xuxa'); //Deve retornar true, pois existe no nó da arvore, como filho
  19. echo "n".$arvore->busca('Fábio'); //Deve retornar false pois não existe no nó da arvore
  20.  
  21. class No {
  22.  
  23. public $arvore = array();
  24. public $filho = array();
  25.  
  26. function __construct($arvore){
  27. $this->$arvore[] = $arvore;
  28. }
  29.  
  30. public function getArvore(){
  31. return $this->arvore[0];
  32. }
  33.  
  34. public function addFilho($filho){
  35. if(!isset($this->filho)){
  36. $this->filho[] = $filho;
  37. $this->arvore[getArvore()][] = $filho;
  38. } else{
  39. $this->arvore[][$filho] = $filho;
  40. $this->filho[$filho] = $filho;
  41. }
  42. }
  43.  
  44. public function busca($elemento){
  45.  
  46. foreach ($this->arvore as $node) {
  47. if($node == $elemento){
  48. return true;
  49. }else{
  50. return false;
  51. }
  52. }
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement