valley,
if you made sure that a node has only leafs (and not a mix of sub branches and leafs) the
node.children.size()
would give you the number of leafs.
Main Topics
Browse All TopicsI want to calculate number of leafs on a same level on a tree. Please give me hints on how to start. Tks a lot.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
>>>> More acurate to state number of keys at each level .
Ok. When traversing the tree you have to sum up children.size() for each level. You may use the 'ident' (+++) to know which level it is. Best use a global vector<int> for the counters.
vector<int> counters(100, 0); // would provide 100 slots initialized with 0
i finish it by working around :) . However, I still need how to fix this error because i don't know how to use constructor for vector. Thanks.
when i declare
vector<int> counters(100,0); // would provide 100 slots initialized with 0
in main(), it is ok. However, it has error when i declare it in Tree() class with this error message
D:\OPENGL\Cpp3b.cpp(25) : error C2059: syntax error : 'constant'
please advise. tks.
>>>> vector<int> counters(100,0);
- If that doesn't compile you probably have forgotten to insert
#include <vector>
using namespace std;
to the include part of the source.
- Or the variable 'counters' was redefined, i. e. there is another counters somewhere.
- Or you defined counters as member in a class. Then initialize it like
Tree::Tree()
: counters(100, 0) // that is called an initializer
{
// put here your current code for the constructor
...
}
here is my code. It works :) by using an array counters[100]
=================
void Tree::drawNode ( TreeNode &parentNode, int indent)
{
int i=0,j=0;
indent ++;
counters[indent] = counters[indent]++;
for( j =0; j <indent; j++)
{
//cout<<"+";
fout<<"+";
}
//cout<<"\""<<parentNode.key<<"\
fout<<"\""<<parentNode.key<<"\""
if( parentNode.children.size()
{
return;
}
else
{
for(int i = 0; i <parentNode.children.size()
{
drawNode(parentNode.childr
}
}
}
void Tree::draw()
{
int indent = 0;
cout<<endl<<"start display tree"<<endl;
fout.open("Treeoutput.txt"
drawNode(root,indent);
//cout<<endl<<"Number of keys on each level"<<endl;
fout<<endl<<"Number of keys on each level"<<endl;
for(int i =1; counters[i] != 0;i++)
{
for( int j =0; j <i; j++)
{
//cout<<"+";
fout<<"+";
}
//cout<<":"<<counters[i]<<endl;
fout<<":"<<counters[i]<<endl;
}
fout.close();
}
=================
Let me change to use vector as you suggested.
I implement this idea
you defined counters as member in a class. Then initialize it like
Tree::Tree()
: counters(100, 0) // that is called an initializer
{
// put here your current code for the constructor
...
}
and get this error message
D:\OPENGL\Cpp3b.cpp(47) : error C2614: 'Tree' : illegal member initialization: 'acounter' is not a base or member
Let me post my completed code
>>>> error C2614: 'Tree' : illegal member initialization: 'acounter' is not a base or member
Yes, as I told you would need to define the acounter a member of the Tree class:
class Tree
{
// in the header use the std:: prefix and no 'using namespace std;'
// That way you won't get name conflicts
std::vector<int> acounter;
// put here the rest
....
};
>>>> vector<int> counters(100,0); // would provide 100 slots initialized with 0
>>>> in main(), it is ok. However, it has error when i declare it in Tree() class with this error
>>>> D:\OPENGL\Cpp3b.cpp(25) : error C2059: syntax error : 'constant'
class Tree
{
std::vector<int> counters; // you can't give a size here but only in the initializer
// of teh constructor(s)
// here the rest
....
};
// tree.cpp
Tree::Tree()
: counters(100, 0) // here the size can given
{
...
}
>>>> It works :) by using an array counters[100]
Probably you defined a global array in the cpp ?
Note, you better make all global variables members of the Tree class. That allows two handle two or more trees with your code (what actually is a precondition for OOP - object oriented programming).
Business Accounts
Answer for Membership
by: itsmeandnobodyelsePosted on 2009-11-06 at 10:40:12ID: 25761774
It is a follow-up of http:Q_24862961.html where the last valid code was
Select allOpen in new window