• Post Reply Bookmark Topic Watch Topic
  • New Topic

count node in binary tree  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi i try to make a counter which count the number of nodes, from a node ι will choose  until it reaches root of the tree.
---------------------------------------6
------------------------------------4-------10
----------------------------------3------7 -----12
  -------------------------------------------8--11
For example in right subtree if i choose the 12 node the program give me the number 3 because exit tree nodes before of 12 the 7 ,8 and 10.
I try but the code i use is return only the child's , for example  12 return oone the node 11 but as i said i the number all of the nodes beafor of the 12 in the right subtree(10,7,12,8,11) only.
 
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you post the program's current output that shows the problem?

How are you debugging the code to see what it is doing and where it is going wrong?
 
Saloon Keeper
Posts: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To count all nodes you'll have to visit each node and keep track of the node count. When a tree of nodes is involved the typical solution involves recursively visiting each node. Recursion is not the easiest concept to wrap your head around but in this case avoiding recursion would be worse.


   
 
Carey Brown
Saloon Keeper
Posts: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Need more info. How is numberOfLeafs() implemented?
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!