# Finding leaves in a Binary Search Tree

Hello,

I need a method that will find the # of leaves in a Binary SEarch Tree -- I'm using a Red Black Tree.  I need it ASAP! Thanks!

-Taylor
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
just a thought ...
traverse the tree using whichever method you like.  For each node you visit, check if it has any children.  If not, count it as a leaf, because it is.

At the end of the algorithm, you will have the total number of children.  This code works for any tree.

For example for a binary tree you can do in-order traversal using a recursive function

This is pseudocode adapted from a C/C++ version at http://datastructures.itgo.com/trees/traversal.htm

inorder(struct NODE curr)
{
if(curr->left != NULL)
inorder(curr->left);
if (both leaves are null)
count++;

if(curr->right != NULL)
inorder(curr->right);
}

You call this function by passing it root, i.e.
inorder(root);
Also have a global or some such variable called count.  Initialize it to 0.  It will be incremented by 1 for every leaf.
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
Or to do it without using a global variable:

public int numLeaves( Node n )
{
// if we've been given a null reference, return 0
if ( n == null )
return 0;

// if we're at a leaf, return 1
if( n.hasLeftChild() == false && n.hasRightChild() == false )
return 1;

// if we're not at a leaf, count all the leaves in this subtree and return that value
if ( n.hasLeftChild() && n.hasRightChild() )
return numLeaves( n.leftChild ) + numLeaves( n.rightChild );
}

baboo_
0
Commented:
Oops.  Use this instead of the above...

public int numLeaves( Node n )
{
// if we've been given a null reference, return 0
if ( n == null )
return 0;

// if we're at a leaf, return 1
if( n.hasLeftChild() == false && n.hasRightChild() == false )
return 1;

// if we're not at a leaf, count all the leaves in this subtree and return that value
else
return numLeaves( n.leftChild ) + numLeaves( n.rightChild );
}

baboo_
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.