views:

240

answers:

6

i need to write a program that counts the number of nodes from a certain level given in binary tree

i mean < numberofnodes(int level){} >

i tried writing it without any success because i don't how to get to a certain level then go to count number of nodes

A: 

Well, there are many ways you can do this. Best is to have a single dimensional array that keep track of the number of nodes that you add/remove at each level. Considering your requirement that would be the simplest way.

However, if provided with just a binary tree, you WILL have to traverse and go to that many levels and count the nodes, I do not see any other alternative.

To go to a certain level, you will typically need to have a variable called as 'current_depth' which will be tracking the level you are in. Once you reach your level of interest and that the nodes are visited once (typically a In order traversal would suffice) you can increment your count. Hope this helped.

Bragboy
can u plz post some code example
sadatwins
Although it does not answer your question directly, you may use this as a reference : http://www.technicalypto.com/2010/02/trace-path-of-binary-tree.html
Bragboy
+1  A: 

Do it with a recursive function which only descends to a certain level.

Yorirou
I think the OP is asking to count **from** a certain level, not **to** a certain level.
Robert Harvey
I think the OP is asking to count **at** a certain level, but who knows.
sth
A: 

I'm assuming that your binary tree is not necessarily complete (i.e., not every node has two or zero children or this becomes trivial). I'm also assuming that you are supposed to count just the nodes at a certain level, not at that level or deeper.

There are many ways to do what you're asked, but you can think of this as a graph search problem - you are given a starting node (the root of the tree), a way to traverse edges (the child links), and a criteria - a certain distance from the root.

At this point you probably learned graph search algorithms - which algorithm sounds like a natural fit for your task?

Uri
A: 

In general terms:
Recursion.
In each iteration of the recursion, you need to measure somehow what level are you on, and therefore to know how far down the tree you need to go beyond where you are now.
Recursion parts:

  1. What are you base cases? at what conditions do you say "Okay, time to stop the recursion" ?
  2. How can you count something in a recursion without any global count?
Rubys
A: 

I think the easiest is to simply follow the recursive nature of the tree with an accumulator to track the current level (or maybe the number of levels remaining to be descended).

The non-recursive branch of that function is when you hit the level in question. At that point you simply return 1 (because you found one node at that level).

The recursive branch, simply sums the number of nodes at that level returned from the left and right recursive invocations.

Martinho Fernandes
A: 

This is the pseudo-code, this assumes the root has level 0

int count(x,currentLevel,desiredLevel)
    if currentLevel = desiredLevel
        return 1
    left = 0
    right = 0
    if x.left != null
        left = count(x.left, currentLevel+1, desiredLevel
    if x.right != null
        right = count(x.right, currentLevel+1, desiredLevel
    return left + right

So to get the number of nodes for level 3 you call

count(root,0,3)
Enrique