简体   繁体   中英

Counting with recursion in binary trees confuse me

I am trying to learn as much as i can about tree's and their algorithm's. And it seems like i can't really learn how recursion works when i want to count something in binary tree. For example if i want to count nodes or leaves or something else. When i look in the solution i don't get it how counter increases and so on.I can remember solution for that particular problem but, when i get another problem which includes counting i dont know how to start my function.

Do you have any advice about my problem ? How did you learn different counting algorithms with recursion. I perfectly understand every iterative solution and i know how to use it .

Thanks in advance for your response

For counting something in a binary tree, its recursive definition comes in quite handy:

A tree consists of three elements: a node N which is the root, the subtree L , which is a tree itself and the subtree R , which is a tree as well.

Using this definition, we can for example count the leaves of a tree in the following manner:
The number of leaves of a tree is

  • 0 if the tree is empty ( N is null )
  • 1 if both L and R are empty
  • the number of leaves in L and R otherwise

The basic idea is that we can use the property that a leave has no children (both L and R are empty) and the fact that an empty tree has no leaves as base case. From that point we can simply say a tree has either one of the above properties and thus the root itself is a leave or there are no nodes in the tree, or the leaves are distributed over L and R , and we simply need to count and sum up the number of leaves in both subtrees.

Or as pseudocode:

countLeaves(node N):
    //the tree is empty
    if N == null:
        return 0

    //N is a leave
    if L == null && R == null:
        return 1

    //count leaves in both subtrees
    return countLeaves(L) + countLeaves(R)

Many people face difficulty in counting in trees using recursion (or in general with recursive solutions). So I will give suggestions without taking any particular example.

Problem with tree and recursion is - the flow of control in not straight forward like iterative solutions. In tree you have to visit all branches and in particular all the nodes and based on the demand of the problem count something. Whatever traversal you use, you need to figure out first what is the condition for any node to be counted, like if you are looking for leaves then what condition defines any node - a leaf. Once you figure it out, you get the acknowledgement of base condition in your recursion and then you can just keep visiting every node, increase counter on meeting that particular condition and terminate or return from recursive calls on meeting returning/terminating conditions.

You need to train your mind a bit to visualise the recursive structure, and for that just do manual run of recursive functions for the problems you read or memorised. Do it for few problems with your created examples. Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM