3v4l.org

run code in 300+ PHP versions simultaneously
<?php // Tree data structure class BinaryNode { public $value = null; // node value public $left = null; // left child public $right = null; // right child public function __construct($value) { $this->value = $value; } } /** * invertTree function goes here */ function invertTree($node) { if($node == null){ return null; } $left = invertTree($node->left); $right = invertTree($node->right); $node->left = $right; $node->right = $left; return $node; } $root = new BinaryNode(1); $rootLeftChild = new BinaryNode(2); $rootRightChild = new BinaryNode(3); $rootLeftChildLeftChild = new BinaryNode(4); $rootLeftChildRightNode = new BinaryNode(5); $rootRightChildLeftChild = new BinaryNode(6); $rootRightChildRightNode = new BinaryNode(7); $rootLeftChild->left = $rootLeftChildLeftChild; $rootLeftChild->right = $rootLeftChildRightNode; $rootRightChild->left = $rootRightChildLeftChild; $rootRightChild->right = $rootRightChildRightNode; $root->left = $rootLeftChild; $root->right = $rootRightChild; $invertedTree = invertTree($root); var_dump($invertedTree->value == 1); var_dump($invertedTree->left->value == 3); var_dump($invertedTree->right->value == 2); var_dump($invertedTree->left->left->value == 7); var_dump($invertedTree->left->right->value == 8); var_dump($invertedTree->right->left->value == 5); var_dump($invertedTree->right->right->value == 4);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/tTKoQ
function name:  (null)
number of ops:  90
compiled vars:  !0 = $root, !1 = $rootLeftChild, !2 = $rootRightChild, !3 = $rootLeftChildLeftChild, !4 = $rootLeftChildRightNode, !5 = $rootRightChildLeftChild, !6 = $rootRightChildRightNode, !7 = $invertedTree
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   35     0  E >   NEW                                              $8      'BinaryNode'
          1        SEND_VAL_EX                                              1
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !0, $8
   36     4        NEW                                              $11     'BinaryNode'
          5        SEND_VAL_EX                                              2
          6        DO_FCALL                                      0          
          7        ASSIGN                                                   !1, $11
   37     8        NEW                                              $14     'BinaryNode'
          9        SEND_VAL_EX                                              3
         10        DO_FCALL                                      0          
         11        ASSIGN                                                   !2, $14
   38    12        NEW                                              $17     'BinaryNode'
         13        SEND_VAL_EX                                              4
         14        DO_FCALL                                      0          
         15        ASSIGN                                                   !3, $17
   39    16        NEW                                              $20     'BinaryNode'
         17        SEND_VAL_EX                                              5
         18        DO_FCALL                                      0          
         19        ASSIGN                                                   !4, $20
   40    20        NEW                                              $23     'BinaryNode'
         21        SEND_VAL_EX                                              6
         22        DO_FCALL                                      0          
         23        ASSIGN                                                   !5, $23
   41    24        NEW                                              $26     'BinaryNode'
         25        SEND_VAL_EX                                              7
         26        DO_FCALL                                      0          
         27        ASSIGN                                                   !6, $26
   42    28        ASSIGN_OBJ                                               !1, 'left'
         29        OP_DATA                                                  !3
   43    30        ASSIGN_OBJ                                               !1, 'right'
         31        OP_DATA                                                  !4
   44    32        ASSIGN_OBJ                                               !2, 'left'
         33        OP_DATA                                                  !5
   45    34        ASSIGN_OBJ                                               !2, 'right'
         35        OP_DATA                                                  !6
   46    36        ASSIGN_OBJ                                               !0, 'left'
         37        OP_DATA                                                  !1
   47    38        ASSIGN_OBJ                                               !0, 'right'
         39        OP_DATA                                                  !2
   48    40        INIT_FCALL                                               'inverttree'
         41        SEND_VAR                                                 !0
         42        DO_FCALL                                      0  $35     
         43        ASSIGN                                                   !7, $35
   49    44        INIT_FCALL                                               'var_dump'
         45        FETCH_OBJ_R                                      ~37     !7, 'value'
         46        IS_EQUAL                                         ~38     ~37, 1
         47        SEND_VAL                                                 ~38
         48        DO_ICALL                                                 
   50    49        INIT_FCALL                                               'var_dump'
         50        FETCH_OBJ_R                                      ~40     !7, 'left'
         51        FETCH_OBJ_R                                      ~41     ~40, 'value'
         52        IS_EQUAL                                         ~42     ~41, 3
         53        SEND_VAL                                                 ~42
         54        DO_ICALL                                                 
   51    55        INIT_FCALL                                               'var_dump'
         56        FETCH_OBJ_R                                      ~44     !7, 'right'
         57        FETCH_OBJ_R                                      ~45     ~44, 'value'
         58        IS_EQUAL                                         ~46     ~45, 2
         59        SEND_VAL                                                 ~46
         60        DO_ICALL                                                 
   52    61        INIT_FCALL                                               'var_dump'
         62        FETCH_OBJ_R                                      ~48     !7, 'left'
         63        FETCH_OBJ_R                                      ~49     ~48, 'left'
         64        FETCH_OBJ_R                                      ~50     ~49, 'value'
         65        IS_EQUAL                                         ~51     ~50, 7
         66        SEND_VAL                                                 ~51
         67        DO_ICALL                                                 
   53    68        INIT_FCALL                                               'var_dump'
         69        FETCH_OBJ_R                                      ~53     !7, 'left'
         70        FETCH_OBJ_R                                      ~54     ~53, 'right'
         71        FETCH_OBJ_R                                      ~55     ~54, 'value'
         72        IS_EQUAL                                         ~56     ~55, 8
         73        SEND_VAL                                                 ~56
         74        DO_ICALL                                                 
   54    75        INIT_FCALL                                               'var_dump'
         76        FETCH_OBJ_R                                      ~58     !7, 'right'
         77        FETCH_OBJ_R                                      ~59     ~58, 'left'
         78        FETCH_OBJ_R                                      ~60     ~59, 'value'
         79        IS_EQUAL                                         ~61     ~60, 5
         80        SEND_VAL                                                 ~61
         81        DO_ICALL                                                 
   55    82        INIT_FCALL                                               'var_dump'
         83        FETCH_OBJ_R                                      ~63     !7, 'right'
         84        FETCH_OBJ_R                                      ~64     ~63, 'right'
         85        FETCH_OBJ_R                                      ~65     ~64, 'value'
         86        IS_EQUAL                                         ~66     ~65, 4
         87        SEND_VAL                                                 ~66
         88        DO_ICALL                                                 
         89      > RETURN                                                   1

Function inverttree:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 4
Branch analysis from position: 3
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 4
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/tTKoQ
function name:  invertTree
number of ops:  22
compiled vars:  !0 = $node, !1 = $left, !2 = $right
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   18     0  E >   RECV                                             !0      
   20     1        IS_EQUAL                                                 !0, null
          2      > JMPZ                                                     ~3, ->4
   21     3    > > RETURN                                                   null
   24     4    >   INIT_FCALL_BY_NAME                                       'invertTree'
          5        CHECK_FUNC_ARG                                           
          6        FETCH_OBJ_FUNC_ARG                               $4      !0, 'left'
          7        SEND_FUNC_ARG                                            $4
          8        DO_FCALL                                      0  $5      
          9        ASSIGN                                                   !1, $5
   25    10        INIT_FCALL_BY_NAME                                       'invertTree'
         11        CHECK_FUNC_ARG                                           
         12        FETCH_OBJ_FUNC_ARG                               $7      !0, 'right'
         13        SEND_FUNC_ARG                                            $7
         14        DO_FCALL                                      0  $8      
         15        ASSIGN                                                   !2, $8
   26    16        ASSIGN_OBJ                                               !0, 'left'
         17        OP_DATA                                                  !2
   27    18        ASSIGN_OBJ                                               !0, 'right'
         19        OP_DATA                                                  !1
   29    20      > RETURN                                                   !0
   31    21*     > RETURN                                                   null

End of function inverttree

Class BinaryNode:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/tTKoQ
function name:  __construct
number of ops:  4
compiled vars:  !0 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    8     0  E >   RECV                                             !0      
    9     1        ASSIGN_OBJ                                               'value'
          2        OP_DATA                                                  !0
   10     3      > RETURN                                                   null

End of function __construct

End of class BinaryNode.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
170.85 ms | 1411 KiB | 16 Q