Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 26th, 2005, 9:48 PM   #1
BaroN NighT
Programmer
 
Join Date: Mar 2005
Location: USA
Posts: 60
Rep Power: 4 BaroN NighT is on a distinguished road
Counting Number of Leaf Nodes Recursively(Binary Search Tree)

I'm trying to write a function that counts the total number of leaf nodes in a binary search tree:
int tree::sumLeaf(node* node1) {
  int sumnodes = 0;
  
  if(node1==NULL){
   return (0);                
  }
  sumnodes=sumLeaf(node1->left);
  if(node1!=NULL && node1->left==NULL && node1->right==NULL){
   return sumnodes++;
  }
You can see that I'm trying to figure out to count it by going through all the way to the left first. Considering a binary search tree with the order of insertion: 15,7,85,10,2,99,66,50,43,15...the function should print 1 (for the leaf node containing the number 2). But it prints 0. Any help would be highly appreciated .
BaroN NighT is offline   Reply With Quote
Old Apr 26th, 2005, 9:51 PM   #2
uman
Expert Programmer
 
Join Date: Dec 2004
Posts: 794
Rep Power: 4 uman is on a distinguished road
//UNTESTED!
int tree::sumLeaf(node* node1)
{       
        if(node1 == NULL) return 0;
        return sumLeaf(node1->left) + sumLeaf(node1->right) + 1;
}
uman is offline   Reply With Quote
Old Apr 26th, 2005, 9:56 PM   #3
BaroN NighT
Programmer
 
Join Date: Mar 2005
Location: USA
Posts: 60
Rep Power: 4 BaroN NighT is on a distinguished road
Thank you for your prompt reply uman, but this function counts the total number of all the nodes, not the number of leaf nodes.
BaroN NighT is offline   Reply With Quote
Old Apr 26th, 2005, 10:03 PM   #4
uman
Expert Programmer
 
Join Date: Dec 2004
Posts: 794
Rep Power: 4 uman is on a distinguished road
whoops :-P. Sorry. Let me finish cleaning my room and I'll see what I can do.
uman is offline   Reply With Quote
Old Apr 26th, 2005, 10:12 PM   #5
BaroN NighT
Programmer
 
Join Date: Mar 2005
Location: USA
Posts: 60
Rep Power: 4 BaroN NighT is on a distinguished road
I don't even know if this is right(now I'm trying to count all the leaf nodes,right and left, not like the first one above):
int tree::sumLeaf(node* node1) {
  int sumnodes = 0;
  
  if(node1==NULL){
   return (0);                
  }
  sumnodes=sumLeaf(node1->left);
  sumnodes=sumLeaf(node1->right);
  if(node1!=NULL && node1->left==NULL && node1->right==NULL){
   return sumnodes++;
  }                 
  else
   return sumnodes;
}
I can understand simple recursive functions, but these binary search tree recursive functions are making me crazy!
BaroN NighT is offline   Reply With Quote
Old Apr 26th, 2005, 10:23 PM   #6
uman
Expert Programmer
 
Join Date: Dec 2004
Posts: 794
Rep Power: 4 uman is on a distinguished road
Try this.
//UNTESTED!
int tree::sumLeaf(node* node1)
{
        if(node1 == NULL) return 0;
        if(node1->left == NULL && node1->right == NULL) return 1;
        else return sumLeaf(node1->left) + sumLeaf(node1->right);
}
uman is offline   Reply With Quote
Old Apr 26th, 2005, 10:27 PM   #7
BaroN NighT
Programmer
 
Join Date: Mar 2005
Location: USA
Posts: 60
Rep Power: 4 BaroN NighT is on a distinguished road
Damn dude, you rock! It prints 4, and yeah, the leaf nodes are 4 when I tried to draw the tree. Thx!
BaroN NighT is offline   Reply With Quote
Old Apr 26th, 2005, 10:28 PM   #8
uman
Expert Programmer
 
Join Date: Dec 2004
Posts: 794
Rep Power: 4 uman is on a distinguished road
heh, no problem
uman is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:19 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC