1
I would like to know how to go through and display the values of the tree in post-order, tried various shapes and could not.
#include <cstdlib>
#include <iomanip>
#include <iostream>
using namespace std;
struct arvore {
int x;
arvore *dir;
arvore *esq;
};
struct pilha {
arvore *num;
pilha *prox;
};
int main() {
arvore *raiz;
arvore *aux;
arvore *novo;
pilha *topo;
pilha *aux_pilha;
int op;
bool flag;
raiz = new arvore();
raiz = NULL;
do {
cout << "1 - Inserir na árvore binária" << endl;
cout << "2 - Exibir árvore em ordem" << endl;
cout << "3 - Exibir árvore em pre ordem" << endl;
cout << "4 - Exibir árvore em pos ordem" << endl;
cout << "5 - Sair" << endl;
cout << "Opção: ";
cin >> op;
if (op == 1) {
cout << "Inserir na árvore binária ..." <<endl;
cout << "Digite o valor: " <<endl;
novo = new arvore();
cin >> novo -> x;
novo -> dir = NULL;
novo -> esq = NULL;
cout << "Valor "<< novo -> x <<" adicionado na arvore "<<endl;
if(raiz == NULL) {
raiz = novo;
} else {
flag = false;
aux = raiz;
while(flag == false) {
if(novo -> x >= aux -> x) {
if (aux -> dir == NULL) {
aux -> dir = novo;
flag = true;
} else {
aux = aux -> dir;
}
} else {
if (aux -> esq == NULL) {
aux -> esq = novo;
flag = true;
} else {
aux = aux -> esq;
}
}
}
}
} else if (op == 2) {
if (raiz == NULL){
cout << " Arvore vazia " <<endl;
}else {
cout <<"Exibindo os valores na árvore em ordem..." <<endl;
aux = raiz;
topo = NULL;
do {
while(aux != NULL) {
aux_pilha = new pilha();
aux_pilha -> num = aux;
aux_pilha -> prox = topo;
topo = aux_pilha;
aux = aux -> esq;
}
if(topo != NULL) {
aux_pilha = topo;
cout << aux_pilha -> num -> x << " ";
aux = topo -> num -> dir;
topo = topo -> prox;
}
} while(topo != NULL or aux != NULL);
}
cout << endl;
} else if (op == 3) {
cout << "Exibindo os valores na arvore em pre ordem... " <<endl;
if (raiz == NULL ){
cout << "Arvore vazia " <<endl;
}else {
aux = raiz;
while(aux != NULL ) {
aux_pilha = new pilha();
aux_pilha -> num = aux;
aux_pilha -> prox = topo;
topo = aux_pilha;
aux = aux ->dir;
cout << aux_pilha -> num -> x << " ";
topo = topo -> prox;
}
if(topo != NULL) {
aux_pilha = topo;
cout << aux_pilha -> num -> x << " ";
aux = topo -> num -> esq;
topo = topo -> prox;
}
}
cout <<endl;
} else if (op == 4) {
cout << "Exibindo os valores na arvore em pos ordem... " <<endl;
aux = raiz ;
topo = NULL;
if(raiz == NULL){
cout << "Arvore vazia "<<endl;
}else{
while (aux != NULL ){
}
}
cout <<endl;
}else if (op == 5){
cout << "Até mais obrigado !" << endl;
} else {
cout << "Opção Inválida!" << endl;
}
} while(op != 5);
}
If you don’t tell me what the doubts are you can’t help yourself.
– Jéf Bueno
in the post order part, I am not succeeding, displaying right side of the tree, following this logic taught in college, not being able to go through both sides...
– Leoo
Try adding this information to your question.
– Flávio Granato
yes, I would like a help in the part to scroll through and display the values in the order, because the ways I’ve done is going through more by displaying the values only on the left side....
– Leoo
You have to do it like this, using these batteries and everything in
main()
? To traverse trees is much easier to do using recursion...– Wtrmute
yes @Wtrmute is the way my teacher is teaching in college...
– Leoo
@Leo may be easier to start from the recursive algorithm to apply the recursive removal procedure. Also because the recursive algorithm is already inherent to the data type and is more intuitive than using the stack. Have you tried this?
– Jefferson Quesado
@Jeffersonquesado tried with function, Dae I got,, but this code I have to do following the logic and structure of op 1 and op 2 menu,why in the test I will do today will be that way...
– Leoo