简体   繁体   English

用二进制树中的递归计数让我感到困惑

[英]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. 一棵树由三个元素组成:节点N是树的根,子树L是树本身,子树R也是树。

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 ) 如果树为空( Nnull ),则为0
  • 1 if both L and R are empty 如果LR都为空,则为1
  • the number of leaves in L and R otherwise 否则LR的叶子数

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. 基本思想是,我们可以使用休假没有子代( LR都是空的)和空树没有叶子的事实作为基本情况。 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. 从这一点出发,我们可以简单地说一棵树具有上述属性之一,因此根本身就是叶子,或者树中没有节点,或者叶子分布在LR ,我们只需要计算和总结两个子树中的叶子数。

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! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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