![]() |
|
#1
|
|||
|
|||
Counting leaves in a Binary TreeI have an assignment that requires me to count the number of leaves in a binarytree. I am using recursion to solve this, but my leafCnt algorithm keeps coming up with zeros and it shouldn't. I'd appreciate any insight as to where I am going wrong.
In this assignment, the bulk of the ADT is given. I created the ::leavesCount function of binarysearch.h, and added the protected variable leafCnt. The p->link code should process the left side elements of the tree, and the p->rlink should process the right side elements. Included is my main. and the course provide ADT's binaryTree.h and binarySearchTree.h Here is the main: CPP / C++ / C Code:
binaryTree.h: CPP / C++ / C Code:
binarySearchTree.h: CPP / C++ / C Code:
|
|
#2
|
||||
|
||||
Re: Counting leaves in a BinaryTreeCPP / C++ / C Code:
I don't see where you increment the counter when a leaf id encountered. Notice that leafCnt initial value is 0 (and you should initialize it explicitly, in my opinion ...) and wherever you call recursively to the function leavesCount, you actually don't increment your counter. You can solve this by adding an if statement that adds 1 to your counter if a leaf is encountered. I'll leave this up to you as you display high capabilities :-> Kobi Hikri. __________________
It's actually a one time thing (it just happens alot). |
|
#3
|
|||
|
|||
Re: Counting leaves in a BinaryTreeQuote:
You have a debug cout<< statement in your leavesCount. Good idea; now put some more. Something like the following (but change it according to your taste): CPP / C++ / C Code:
If the ending count is zero, why? Where should the links should have been set? Look at that code. If you can't see the problem, some cout<< statements there should lead to enlightenment. Regards, Dave -------------------------------- Trivia test for today: Who was it who said the following? "Putting debug statements in code is faster, I claim, than posting a request for help and waiting for a helpful response." |
|
#4
|
||||
|
||||
Re: Counting leaves in a BinaryTreeQuote:
Now, I think it was rhetorical but I know this guy, Dave, who always say stuff like this __________________
It's actually a one time thing (it just happens alot). |
|
#5
|
|||
|
|||
Re: Counting leaves in a BinaryTreeThanks for the replies from both davekw7x and kobi hikri. From the 2 of you a think I have a resolution.
To kobi: I thought the presence of code: CPP / C++ / C Code:
Davekw7x's suggestion of expanding my debug displays gave a better clue of what was happening. There are many solutions, but the one I have chosen is: CPP / C++ / C Code:
Also, how would I explicitly initialize leafCnt if the variable is within a recursive loop, except in the default constructor like I've done? |
|
#6
|
||||
|
||||
Re: Counting leaves in a BinaryTreeQuote:
You can initialize a counter outside the function and pass a pointer to it as an argument. But, actually, what I ment was : When you enter the recursion, it means that you didnt have any leaves at the previous level (do you understand why ?), therefore the current value of leafCnt (per this instance of the recursion) is 0. I recommend showing that you understand this point by assigning leafCnt the value 0 when you start the current instance of the recursion. It is also more readable. Best regards, Kobi Hikri. __________________
It's actually a one time thing (it just happens alot). |
Recent GIDBlog
Toyota - 2008 November Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| printing binary search tree | nkhambal | C Programming Language | 2 | 26-Mar-2005 04:01 |
| Please help! Dynamic binary tree problem | robsmith | C Programming Language | 3 | 15-Mar-2005 22:20 |
| Displaying node attributes in an XML tree display | njp01u | MS Visual C++ / MFC Forum | 2 | 07-Feb-2005 18:42 |
| Binary Tree Trouble | neufunk | C Programming Language | 4 | 06-Dec-2004 10:52 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The