473,395 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Counting the number of the leaves in tree

momotaro
357 100+
This is my code is it correct?:

Expand|Select|Wrap|Line Numbers
  1. int CountLeaves(Tree *root){
  2. if (root == NULL) return 0;
  3. else if (root -> left == NULL && root -> right == NULL) return 1;
  4. else{
  5.        CountLeaves(root -> left);
  6.        CountLeaves(root -> right);
  7.       }
  8. }
THX!
Apr 25 '07 #1
10 6304
Ganon11
3,652 Expert 2GB
In your else branch, you should be returning values; instead, you are just calling the function and doing nothing with the return values.
Apr 25 '07 #2
momotaro
357 100+
is this one correct?

Expand|Select|Wrap|Line Numbers
  1. int CountLeaves(Tree *root){
  2. int count = 0;
  3. if (root){
  4.            CountLeaves(root -> left);
  5.            CountLeaves(root -> right);
  6.            count++;
  7.      return count;
  8.           }
  9. }

THX!
Apr 25 '07 #3
momotaro
357 100+
this latter don't work either!!!! am realy stuck plz help!
Apr 26 '07 #4
JosAH
11,448 Expert 8TB
Think recursively again: if a tree is empty it has zero leaves; if a tree is a leaf,
well the tree has one leaf; otherwise count the number of leaves in the left
subtree plus the number of leaves in the right subtree.

kind regards,

Jos

ps. I don't understand your confusion here: all the questions you have posted
deal with trees and all the algorithms you have shown us all have an (almost)
identical recursive structure. Some of the algorithms are just perfect while
the others are totally incorrect. Did you copy the correct algorithms from a
textbook or what?
Apr 26 '07 #5
momotaro
357 100+
thx for the hint!
concerning the accurency of my algos it is just because i dont know how to use the recrsion very well yet :)!
i' ve come up with this one for counting the leaves i hope it will be fine!

Expand|Select|Wrap|Line Numbers
  1. int CountLeaves(Tree *root){
  2. int count;
  3. if (root == NULL) return 0;
  4. else{
  5.        CountLeaves(root -> left);
  6.        countLeaves(root -> right);
  7.        count++;
  8.        }
  9. }
Apr 26 '07 #6
JosAH
11,448 Expert 8TB
thx for the hint!
concerning the accurency of my algos it is just because i dont know how to use the recrsion very well yet :)!
i' ve come up with this one for counting the leaves i hope it will be fine!

Expand|Select|Wrap|Line Numbers
  1. int CountLeaves(Tree *root){
  2. int count;
  3. if (root == NULL) return 0;
  4. else{
  5.        CountLeaves(root -> left);
  6.        countLeaves(root -> right);
  7.        count++;
  8.        }
  9. }
If the root is NULL you return 0 (zero) otherwise you don't return anything and
you discard the return values from your two recursive function calls.

The number of leaves is either 0 when the tree is empty, 1 when the tree is a
leaf itself, otherwise it's the number of leaves in the left subtree plus the
number of leaves in the right subtree.

hint: try to write that function without the 'count' variable; you don't need it.

kind regards,

Jos
Apr 26 '07 #7
momotaro
357 100+
thx a lot this one should be perfect...i hope :)


Expand|Select|Wrap|Line Numbers
  1. int CountLeaves(Tree *root){
  2. if (root == NULL) return 0;
  3. else if (root -> left == NULL && root -> right == NULL) return 1;
  4. else
  5.       return( CountLeaves(root -> left) +
  6.                 CountLeaves(root -> right));
  7. }
Apr 26 '07 #8
JosAH
11,448 Expert 8TB
thx a lot this one should be perfect...i hope :)


Expand|Select|Wrap|Line Numbers
  1. int CountLeaves(Tree *root){
  2. if (root == NULL) return 0;
  3. else if (root -> left == NULL && root -> right == NULL) return 1;
  4. else
  5.       return( CountLeaves(root -> left) +
  6.                 CountLeaves(root -> right));
  7. }
Yep, that's it. You can get rid of that explicit leaf test (the else-if) at the
expensive of a bit more function calls and a temporary variable like this:
Expand|Select|Wrap|Line Numbers
  1. int CountLeaves(Tree* root) {
  2.    if (root) {
  3.       int temp= CountLeaves(root->left)+CountLeaves(root->right);
  4.       return (temp == 0)?1:temp;
  5.    }
  6.    return 0;
  7. }
kind regards,

Jos
Apr 26 '07 #9
momotaro
357 100+
please can you explain me this line:

Expand|Select|Wrap|Line Numbers
  1. return (temp == 0)?1:temp;
thx! :)
Apr 26 '07 #10
momotaro
357 100+
WAW it is realy a good one i understood it!!!!
thx a lot!
Apr 26 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: David | last post by:
I have a simple tree structure where node x can have y children. Node x's children are stored in an array. I want to supply a node to a function and count the TOTAL number of children for that...
3
by: e01osama | last post by:
i want to add check boxes to the tree as leaves I know how to add normal texts.. pls help
5
by: Shane | last post by:
I wonder if someone has any ideas about the following. I am currently producing some reports for a manufacturing company who work with metal. A finished part can contain multiple sub-parts to...
18
by: ChadDiesel | last post by:
I appreciate the help on this group. I know I've posted a lot here the last couple of weeks, but I was thrown into a database project at my work with very little Access experience. No other...
19
by: Alex Vinokur | last post by:
Is there any tool to count C-program lines except comments? Thanks, ===================================== Alex Vinokur mailto:alexvn@connect.to http://mathforum.org/library/view/10978.html...
4
by: sun6 | last post by:
this is a program counting words from "text_in.txt" file and writing them in "text_out.txt". it uses binary tree search, but there is an error when i use insert () thanks for any help ...
8
by: mdoxdo | last post by:
Hi all, I was wondering if there is a way to get the number of references an objects have (ie, other objects reference that object)? I know that .Net GC uses a different mechanism than the...
3
by: IZZI | last post by:
This code is created to find all duplicates in a list of number by using a binary search tree implemented as an array. I want to add a function to count how many numbers in the list are duplicated...
5
by: Ouray Viney | last post by:
Hi All: I am looking at writing a python script that will let me parse a TestSuite xml file that contains n number of TestCases. My goal is to be able to count the <TestCaseelements base on a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.