so i got a class going for making and showing tree elements, i need some help with the following:
i need to display the leaves and the height of the tree. here is the code:
// ar.cpp
#include <iostream>
#include <stdlib.h>
using namespace std;
class nod
{
public:
char inf;
int dim;
nod *tab[100];
};
nod * crearb(); // Functia pentru crearea arborelui
void padinc(nod *); // Functia pentru parcurgerea in adincime
void plat(nod *); // Functia pentru parcurgerea in latime
class queue{
public:
queue(): size(0), end(0)
{
}
void pop(); // functia elimina un element din coada
void push(nod *); // aduaga un element in coada
nod * front(); // returneaza un element din coada fara sa-l elimine
int empty();
private:
int size, end;
nod *t[100];
};
void queue::pop()
{
if (end>0)
end--;
else
{
cerr << "queue empty" << endl;
exit(1);
}
}
void queue::push(nod *p)
{
for(int i=end; i>0; i--)
t[i]=t[i-1];
t[0]=p;
end++;
}
nod * queue::front()
{
if (end>0)
return t[end-1];
else
{
cerr << "queue empty" << endl;
exit(1);
}
}
int queue::empty()
{
if (end == 0)
return 1;
else
return 0;
}
nod * crearb()
{
char ch;
int n,i;
nod *p;
cout << "Inf="; cin >> ch;
p = new nod;
p->inf = ch;
cout << "Cati fii are?"; cin >> n;
p->dim = n;
for(i=0; i < n; i++)
p->tab[i] = crearb();
return p;
}
// Parcurgerea in latime utilizeaza structura de coada
// Algoritmul este nerecursiv si poate fi inteles destul de
// simplu din codul de mai jos;
void plat(nod *p)
{
queue Q; nod *t; int i;
Q.push(p);
while (!Q.empty())
{
t = Q.front();
Q.pop();
cout << t->inf<<" ";
for(i=0; i<t->dim; i++)
Q.push(t->tab[i]);
}
}
int main()
{
//Creez arborele
nod * root;
root = crearb();
cout << endl;
cout << "Parcurgere in latime:" << endl;
plat(root);
return 0;
}