Build a knot tree

Asked

Viewed 253 times

0

I need to build a structure similar to a family tree, but with only one Father element that can have "n" children, and each child can also have "n" children. The structure below illustrates a test that would power this tree.

Here is the code that should test the tree structure:

$arvore = new No('Thiago'); //Aqui cria-se a arvore, ou elemento Pai

$no = $arvore->addFilho('Alan'); //Alan deveria ser um filho de Thiago, seguindo a estrutura
$no->addFilho('Thiago'); //Já nessa linha, a ideia era que fosse adicionado a Alan, um filho chamado Thiago
//assim como a linha de baixo, resultando em dois filhos para Alan.
// Mas me resulta um erro no PHP:
// Fatal error: Uncaught Error: Call to a member function addFilho() on null 
$no->addFilho('Thiago');

$no = $arvore->addFilho('Robson'); //Robson também seria filho de Thiago, que é a Arvore/elemento Pai
$no->addFilho('Maurício'); //E mauricio seria filho de Robson, assim como Bruno e Xuxa
$no1 = $no->addFilho('Bruno');
// Rodolpho e Guilherme são filhos do Bruno
$no1->addFilho('Rodolpho');
$no1->addFilho('Guilherme');
$no->addFilho('Xuxa');

$arvore->addFilho('Eduardo');
$arvore->addFilho('Alexandre')->addFilho('Gabriel');

echo "\n".$arvore->busca('Thiago'); //Deve retornar true, pois existe no nó da arvore
echo "\n".$arvore->busca('Xuxa'); //Deve retornar true, pois existe no nó da arvore, como filho
echo "\n".$arvore->busca('Fábio'); //Deve retornar false pois não existe no nó da arvore

Now enter the part I’m not knowing how to develop, which is Class No:

class No {

public $arvore = array();
public $filho = array();

function __construct($arvore){
    $this->$arvore[] = $arvore;
}

public function getArvore(){
    return $this->arvore[0];
}

public function addFilho($filho){
    if(!isset($this->filho)){
        $this->filho[] = $filho;
        $this->arvore[getArvore()][] = $filho;
    } else{
        $this->arvore[][$filho] = $filho;
        $this->filho[$filho] = $filho;
    }
} 

public function busca($elemento){

    foreach ($this->arvore as $node) {
        if($node == $elemento){
            return true;
        }
    } 
    return false; 
  }
}

I understand that there must be some errors of OO, and perhaps some incorrect logic too, but I can not think of any way to solve, beyond what I have done so far.

Uma ideia da estrutura seria isso

  • 1

    What is your specific question? I saw some problems in your code. One thing that people never understand about O is that you can only get it right with all the very detailed requirements, harvested correctly and already specified everything you should do. Without this anything you do can be wrong because there is a previous mistake. OOP doesn’t make bad projects magically become good.

  • my doubt is how to implement, correctly, the No class, so that the first piece of code works as expected

1 answer

2


Your problem has nothing to do with object orientation, it’s just a simple imperative algorithm that is wrong. At least the main focus. In the original question had not that want the whole solution, which makes the question wide.

public function busca($elemento){
    foreach ($this->arvore as $node) if($node == $elemento) return true;
    return false;
}

I put in the Github for future reference.

When doing a simple sequential search you should actually terminate the algorithm as soon as it finds something, then the return true in this case is correct. But what should you do if looking at that element is not found? The original code is ending the search saying you didn’t find it, only you only looked at one element, and the others won’t? The correct is to continue the loop until you find one, you should not take any action if the element is not based on what you are looking for. Only if the loop goes to the end and looks at all the elements of the data collection is that we can state that the data does not exist there, all comparisons failed and closed without finding anything, then yes it should close the algorithm with a return false`.

The whole concept of this class seems to be wrong, but as I said in the comments, without understanding exactly what you need it is difficult to say something.

  • Thanks for your help with the search. But what I really need is to generate a structure similar to a family tree, but with a 'Father' who can have n children, and each child can have n children as well. In this case, $tree = new No('Thiago') says that the tree itself, or parent element, is called Thiago. In $no = $arvore->addFilho('Alan') defines the first child named Alan, and Alan would have two children Thiago. The logic table should apply to the other test examples, but when I declare $no->addFilt('Thiago') PHP results in Uncaught Error: Call to a Member Function addFilt() on null

  • Now you are already making another mistake, the question has already become unclear or broad.

  • I’ll rephrase it then

  • I believe my doubt has now become clearer

Browser other questions tagged

You are not signed in. Login or sign up in order to post.