Java return statement

I have a question about the following code

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

In fact, I do not understand the meaning of "return"; expression there. It looks like if node is null, the code returns nothing. but then without this line, the compiler throws an exception error.

+3
source share
8 answers

This is a recursive function (calling itself multiple times). The goal returnis to ensure that he does not try to do this forever, resulting in a null pointer exception when you run from the bottom of the tree.

, , node (, , node) node, node, node.

, node . .

null , , , node, , ( ).

, ( , , === ):

             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).

+16

void . return, . :

return;

Java LangSpec:

14.17 return

invoker (§8.4, §15.12) (§8.8, §15.9).

ReturnStatement:
        return Expressionopt ;

return , , void, - (§8.4) (§8.8). , (§8.7). return statement invoker . , return statement , .

return , (§8.4) . T, . T (. 5.2) , .

+4

, node null, , NullReferenceException. , , () "" .

0

, node.data. , null, .

0

'return' . , .

if(node==null) return;

, node ( node ) .

, , , , node, printTree(node.left); printTree(node.right); .

0

:

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

, . . , , , . :

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

, " node, !!

0

return :

, , , .

, , , .

0

Source: https://habr.com/ru/post/1728004/


All Articles