加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java return语句

发布时间:2020-12-15 05:22:13 所属栏目:Java 来源:网络整理
导读:我对以下代码有疑问 private void printTree(Node node){ if(node==null) return; printTree(node.left); System.out.print(node.data+" "); printTree(node.right);} 我真的不明白’回归’.那里的声明.看起来如果node为null,则代码不返回任何内容.但是如果
我对以下代码有疑问

private void printTree(Node node){
    if(node==null) return;
    printTree(node.left);
    System.out.print(node.data+" ");
    printTree(node.right);
}

我真的不明白’回归’.那里的声明.看起来如果node为null,则代码不返回任何内容.但是如果没有那一行,编译器会生成异常错误.

解决方法

这是一个递归函数(一个自称重复的函数).返回的目的是确保它不会永远尝试这样做,从而在您从树的底部运行时导致空指针异常.

会发生的事情是,第一次使用节点(通常但不总是根节点)调用此函数时,它将首先打印出该节点的左子树,然后打印出节点本身的值,然后该节点的右侧子树.

打印出子树的方式是通过该子树的顶级节点调用自身.这是一种优雅处理递归结构的常用方法.

对null的测试是这样的,它具有这样的条件:当树到达你正在检查的特定一侧没有子节点的节点(向左或向右)时,向下搜索树的层次.

举例来说,假设您有以下树(大写字母的数字是真实节点,数字是它们的值,===标记是空的):

A26                Level 0
              |
       +------+------+
       |             |
      B14           C84         Level 1
       |             |
    +--+--+       +--+--+
    |     |       |     |
   D11   ===     ===   E99      Level 2
    |                   |
 +--+--+             +--+--+
 |     |             |     |
===   ===           ===   ===   Level 3

当你用A调用函数时,会发生什么.

You call the function (level 0) with A.
  The function will call itself (level 1) with B (A left).
    The function will call itself (level 2) with D (B left).
      The function will call itself (level 3) with null (D left).
        The function will return to level 2.
      The function will print out 11 from D.
      The function will call itself (level 3) with null (D right).
        The function will return to level 2.
      The function will return to level 1.
    The function will print out 14 from B.
    The function will call itself (level 2) with null (B right).
      The function will return to level 1.
    The function will return to level 0.
  The function will print out 26 from A.
  The function will call itself (level 1) with C (A right).
    The function will call itself (level 2) with null (C left).
      The function will return to level 1.
    The function will print out 84 from C.
    The function will call itself (level 2) with E (C right).
      The function will call itself (level 3) with null (E left).
        The function will return to level 2.
      The function will print out 99 from E.
      The function will call itself (level 3) with null (E right).
        The function will return to level 2.
      The function will return to level 1.
    The function will return to level 0.
  The function will return to you.

结果是它打印出序列DBACE,在排序的树中,它是按排序顺序排列的元素(11,14,26,84,99).

或者更简单的版本,如果你不愿意阅读上面我的大量解释:

A26                Level 0
              |
       +------+------+
       |             |
      B14           ===         Level 1
       |
    +--+--+
    |     |
   ===   ===                    Level 2

You call the function (level 0) with A.
  The function will call itself (level 1) with B (A left).
    The function will call itself (level 2) with null (B left).
      The function will return to level 1.
    The function will print out 14 from B.
    The function will call itself (level 2) with null (B right).
      The function will return to level 1.
    The function will return to level 0.
  The function will print out 26 from A.
  The function will call itself (level 1) with null (A right).
    The function will return to level 0.
  The function will return to you.

在这种情况下,你会得到BA或(14,26).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读