Doubt how to display binary tree by width?

Asked

Viewed 1,998 times

5

Good to use this method of flaunt need a line there all right, I tried this logic worked until the third level of the tree.

bool marcador = true;
void buscaLargura(No *raiz)
{

   if(raiz != NULL)
   {
        if (marcador == true)
        {
            Enqueve(raiz->Elemento);
        }


       if (raiz->Esq != NULL)
       {
           Enqueve(raiz->Esq->Elemento);
       }
        if(raiz->Dir != NULL)
       {
           Enqueve(raiz->Dir->Elemento);
       }
        marcador= false;
        buscaLargura(raiz->Esq);
        buscaLargura(raiz->Dir);


   }

}
    • display using width search, incomplete in reward comment

1 answer

4


What you want, as the name of your function indicates, can be obtained using the traverse algorithm Breadth-first search (BFS) or Search in Width

To the following binary tree

                 15
               /    \
             6       20
           /   \       \  
         3      7       22
       /   \     \        \
     2       5     8       25
   /       /             /     
 1       4             23

I assume the desired result is:

15 6 20 3 7 22 2 5 8 25 1 4 23

Here is a possible definition for a binary (search) tree and an iterative version for viewing the tree.

#include<queue>
#include<iostream>   

class ArvoreB {
protected:
    struct no {
            no *esquerda;  
            no *direita; 
            int v;   

            no() {}
            no( 
               int v,
               no *l = nullptr,
               no *r = nullptr)
             : esquerda(l), direita(r), v(v) {}
    };

    no* raiz_;      
    size_t numElementos_;    

public:
    ArvoreB(): raiz_(nullptr), numElementos_(0) {}

    void inserir(int v) {
        no *novoNo = new no(v);
            no *pai = nullptr;

        if(!raiz_) {
            raiz_ = novoNo;
            ++numElementos_;
            return;
        }
        no* actual = raiz_;

        while(actual) {
            pai = actual;
            actual = novoNo->v > actual->v ? actual->direita : actual->esquerda;
        }

        if(novoNo->v < pai->v) {
            pai->esquerda = novoNo;
        }
        else {
            pai->direita = novoNo;
        }
        ++numElementos_;
    }

    void inserir(std::vector<int> v) {
        for(std::vector<int>::iterator it = v.begin(); it != v.end(); ++it) {
            inserir(*it); 
        }
    }

    void buscaLargura() {      
        std::queue<no*> q;
        q.push(raiz_);  

        while (q.size() > 0)
        {
            no* n = q.front();
            q.pop();
            std::cout << n->v << " ";

            if (n->esquerda !=nullptr) {
                    q.push(n->esquerda);
            }
            if (n->direita !=nullptr) {
                q.push(n->direita);
            }
        }

        std::cout << std::endl;
    }   

    void mostraArvore() {
        std::queue<no*> nivelActual, nivelSeguinte;
        nivelActual.push(raiz_);
        while(!nivelActual.empty()) {
            no* noActual = nivelActual.front();
            nivelActual.pop();

            if(noActual) {
                std::cout << noActual->v << " ";
                nivelSeguinte.push(noActual->esquerda);
                nivelSeguinte.push(noActual->direita);
            }
            if(nivelActual.empty()) {
                std::cout << std::endl;
                swap(nivelActual, nivelSeguinte);
            }
        }
    }

};

So let’s test

int main(int argc, char *argv[]) {
    std::vector<int> v = {15, 6, 3, 7, 2, 5, 8, 1, 4, 20, 22, 25, 23};
    BTree t;
    t.inserir(v);

    //t.inserir(15);
    //t.inserir(6);
    //t.inserir(3);
    //t.inserir(7);
    //t.inserir(2);
    //t.inserir(5);
    //t.inserir(8);
    //t.inserir(1);
    //t.inserir(4);
    //t.inserir(20);
    //t.inserir(22);
    //t.inserir(25);
    //t.inserir(23);

    t.buscaLargura();

    t.mostraArvore();
    return 0;
}

The result of the function bfs will be:

15 6 20 3 7 22 2 5 8 25 1 4 23 

The function mostraArvore creates a different line for each level

15 
6 20 
3 7 22 
2 5 8 25 
1 4 23 

Editing:

  • Add alternate version of the insert method. This summer allows taking as a parameter an Std:vector
  • 1

    -1. Your reply uses unnecessarily English words (output, display) uses the term Breadth-first Search when you could use "Search in width" (and linking to a text in Portuguese wouldn’t hurt either) and your code is all in English, making it potentially indecipherable for readers, while the OP code is in Portuguese. We are in the OS in Portuguese. Let’s speak our language?

  • Thank you so much for the comment @Pablo. I changed according to your suggestion.

  • 1

    Thank you, @Runo. + 1. :)

  • @Bruno thank you for your reply, but I needed a search width function using a queue, it seems to me that your code is object oriented, is a part that I do not have much knowledge need something simpler as I tried to do.

  • Rodolfo, both the query function and the function displayArvore use a queue (Queue). If you don’t want to use Std::It can be replaced by a queue created by you.

  • @Runo this line is already ready?

  • @Rodolfo, yes, it is already ready. But nothing prevents you from using a queue created by you. Just replace the Std:::Ueue<no *> line with your queue.

  • @Runo be able to use with my queue all right I made her line up type In run, well my tree was integer, now it is a type that creates with struct , but the error queue now error: 'No' does not name a type

  • 1

    You probably need to copy the "No" type setting to the same file in your Queue.

Show 4 more comments

Browser other questions tagged

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