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;

}

I'm sure there are some here who speak Portuguez - I speak Spanish and can guess what your comments mean, but others don't. Please translate your post into English as well as you can.

ok i translated the comments, i did realise they are not in english but i didnt want to spam and i cant find edit button, sorry for that. if you could help it would be of great importance for me.

// ar.cpp
#include <iostream>
#include <stdlib.h>
using namespace std;
class nod
{
 public:
char inf;
 int dim;
 nod *tab[100];

};
nod * crearb(); // function to create the tree
void plat(nod *); // function for level displaying the elements (dont know how to state it exactly but you can figure it out i guess)

class queue{
     public:
 queue(): size(0), end(0)
 {
 }
 void pop(); // function to delete from queue
 void push(nod *); // function to add into queue
 nod * front(); // function to return from queue without deleting
 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 << "how many sons does the node have?"; cin >> n;
 p->dim = n;
 for(i=0; i < n; i++)
 p->tab[i] = crearb();
 return p;
}


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()
{

 //creating the tree
 nod * root;
 root = crearb();
 cout << endl;
 cout << "Parcurgere in latime:" << endl;
 plat(root);
 return 0;

}

Edited 2 Years Ago by dan.doca

"Cati fii are?"; = how many sons does the node have?
i forgot this one...damn im tired.

also just to clarify i need only to display the leaf information not location or anything what so ever.

This article has been dead for over six months. Start a new discussion instead.