tell me how to solve "counting Problem" in a BTree ?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2007
Posts: 85
Reputation: Zay is an unknown quantity at this point 
Solved Threads: 0
Zay's Avatar
Zay Zay is offline Offline
Junior Poster in Training

tell me how to solve "counting Problem" in a BTree ?

 
0
  #1
Jun 1st, 2007
Hello;

i have this Question and need your help, i do not know how to solve it;

Problem:
Write a member function called countNodeWithNoRchild that will return the number of nodes in a BT(binary Tree) does not have a right child. (Hint: a leaf node also does not have a right child)





i try to do it but the ways i followed are wrong , because there are some missing node not visited yet ..
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,606
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: tell me how to solve "counting Problem" in a BTree ?

 
0
  #2
Jun 1st, 2007
Have you written a function that tells you the height of a binary search tree? If you have, you can use the same principles to solve this problem. If you haven't, you need to do a complete traversal of the tree (recursive is easiest) and for each node, return the count of the nodes below it that have no right child. The hint in your problem gives you the base case for recursion.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 85
Reputation: Zay is an unknown quantity at this point 
Solved Threads: 0
Zay's Avatar
Zay Zay is offline Offline
Junior Poster in Training

Re: tell me how to solve "counting Problem" in a BTree ?

 
0
  #3
Jun 2nd, 2007
see my code , i do it using recursion

  1.  
  2. int count(nodeType<T> *p)
  3. { if (p==NULL) // when reach the end or this is an empty tree
  4. return 0;
  5.  
  6. else if (p->rlink==NULL && p->llink!=NULL)
  7. return (1+count(p->llink));
  8.  
  9. else if(p->rlink==NULL&&p->llink==NULL)//leaf node no R & no L
  10. return(1+count(p->llink));
  11.  
  12. else // node with R and L
  13. return (1+count(p->rlink), count(p->llink));
  14.  
  15. }


correct ??
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,606
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: tell me how to solve "counting Problem" in a BTree ?

 
0
  #4
Jun 2nd, 2007
Basically what you're doing is a typical node count, but you only increment the count when p->rlink is null. Something along these lines:
  1. int count ( nodeType<T> *p )
  2. {
  3. if ( p == NULL )
  4. return 0;
  5. else {
  6. int left = count ( p->llink );
  7. int right = count ( p->rlink );
  8.  
  9. return right + left + ( p->rlink == NULL );
  10. }
  11. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC