3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Node { public $data = null; public $parent = null; public $left = null; public $right = null; } #使用数组构造完全二叉树 function build_cbtree($a) { $root = new Node(); $root->data = $a[0]; for ($i = 1; $i < count($a); $i++) { $node = new Node(); $node->data = $a[$i]; insert_node($root, $node); } return $root; } #插入完全二叉树节点 function insert_node($root, $inode) { #使用树的广度优先遍历顺序取出节点,直到找到第一个左右子节点没满的节点,将待插入节点插入节点左边或右边 $queue = array(); array_unshift($queue, $root); while (!empty($queue)) { $cnode = array_pop($queue); if ($cnode->left == null) { $cnode->left = $inode; $inode->parent = $cnode; return $root; } else { array_unshift($queue, $cnode->left); } if ($cnode->right == null) { $cnode->right = $inode; $inode->parent = $cnode; return $root; } else { array_unshift($queue, $cnode->right); } } return $root; } #树的广度优先遍历 function bf_traverse($root) { $queue = array(); array_unshift($queue, $root); while (!empty($queue)) { $cnode = array_pop($queue); echo $cnode->data . " "; if ($cnode->left !== null) array_unshift($queue, $cnode->left); if ($cnode->right !== null) array_unshift($queue, $cnode->right); } echo "<br>"; } $a = array(9, 8, 7, 6, 8, 4, 3, 2, 1); $root = build_cbtree($a); bf_traverse($root); #广度优先遍历 ?>
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ro1oV
function name:  (null)
number of ops:  9
compiled vars:  !0 = $a, !1 = $root
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   65     0  E >   ASSIGN                                                   !0, <array>
   66     1        INIT_FCALL                                               'build_cbtree'
          2        SEND_VAR                                                 !0
          3        DO_FCALL                                      0  $3      
          4        ASSIGN                                                   !1, $3
   67     5        INIT_FCALL                                               'bf_traverse'
          6        SEND_VAR                                                 !1
          7        DO_FCALL                                      0          
   68     8      > RETURN                                                   1

Function build_cbtree:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 20
Branch analysis from position: 20
2 jumps found. (Code = 44) Position 1 = 23, Position 2 = 9
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
2 jumps found. (Code = 44) Position 1 = 23, Position 2 = 9
Branch analysis from position: 23
Branch analysis from position: 9
filename:       /in/ro1oV
function name:  build_cbtree
number of ops:  25
compiled vars:  !0 = $a, !1 = $root, !2 = $i, !3 = $node
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   RECV                                             !0      
   11     1        NEW                                              $4      'Node'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !1, $4
   12     4        FETCH_DIM_R                                      ~8      !0, 0
          5        ASSIGN_OBJ                                               !1, 'data'
          6        OP_DATA                                                  ~8
   14     7        ASSIGN                                                   !2, 1
          8      > JMP                                                      ->20
   15     9    >   NEW                                              $10     'Node'
         10        DO_FCALL                                      0          
         11        ASSIGN                                                   !3, $10
   16    12        FETCH_DIM_R                                      ~14     !0, !2
         13        ASSIGN_OBJ                                               !3, 'data'
         14        OP_DATA                                                  ~14
   17    15        INIT_FCALL_BY_NAME                                       'insert_node'
         16        SEND_VAR_EX                                              !1
         17        SEND_VAR_EX                                              !3
         18        DO_FCALL                                      0          
   14    19        PRE_INC                                                  !2
         20    >   COUNT                                            ~17     !0
         21        IS_SMALLER                                               !2, ~17
         22      > JMPNZ                                                    ~18, ->9
   20    23    > > RETURN                                                   !1
   21    24*     > RETURN                                                   null

End of function build_cbtree

Function insert_node:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 40
Branch analysis from position: 40
2 jumps found. (Code = 44) Position 1 = 43, Position 2 = 8
Branch analysis from position: 43
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 21
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
2 jumps found. (Code = 43) Position 1 = 29, Position 2 = 35
Branch analysis from position: 29
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 35
2 jumps found. (Code = 44) Position 1 = 43, Position 2 = 8
Branch analysis from position: 43
Branch analysis from position: 8
filename:       /in/ro1oV
function name:  insert_node
number of ops:  45
compiled vars:  !0 = $root, !1 = $inode, !2 = $queue, !3 = $cnode
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   24     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   26     2        ASSIGN                                                   !2, <array>
   27     3        INIT_FCALL                                               'array_unshift'
          4        SEND_REF                                                 !2
          5        SEND_VAR                                                 !0
          6        DO_ICALL                                                 
   29     7      > JMP                                                      ->40
   30     8    >   INIT_FCALL                                               'array_pop'
          9        SEND_REF                                                 !2
         10        DO_ICALL                                         $6      
         11        ASSIGN                                                   !3, $6
   31    12        FETCH_OBJ_R                                      ~8      !3, 'left'
         13        IS_EQUAL                                                 ~8, null
         14      > JMPZ                                                     ~9, ->21
   32    15    >   ASSIGN_OBJ                                               !3, 'left'
         16        OP_DATA                                                  !1
   33    17        ASSIGN_OBJ                                               !1, 'parent'
         18        OP_DATA                                                  !3
   34    19      > RETURN                                                   !0
         20*       JMP                                                      ->26
   36    21    >   INIT_FCALL                                               'array_unshift'
         22        SEND_REF                                                 !2
         23        FETCH_OBJ_R                                      ~12     !3, 'left'
         24        SEND_VAL                                                 ~12
         25        DO_ICALL                                                 
   38    26        FETCH_OBJ_R                                      ~14     !3, 'right'
         27        IS_EQUAL                                                 ~14, null
         28      > JMPZ                                                     ~15, ->35
   39    29    >   ASSIGN_OBJ                                               !3, 'right'
         30        OP_DATA                                                  !1
   40    31        ASSIGN_OBJ                                               !1, 'parent'
         32        OP_DATA                                                  !3
   41    33      > RETURN                                                   !0
         34*       JMP                                                      ->40
   43    35    >   INIT_FCALL                                               'array_unshift'
         36        SEND_REF                                                 !2
         37        FETCH_OBJ_R                                      ~18     !3, 'right'
         38        SEND_VAL                                                 ~18
         39        DO_ICALL                                                 
   29    40    >   ISSET_ISEMPTY_CV                                 ~20     !2
         41        BOOL_NOT                                         ~21     ~20
         42      > JMPNZ                                                    ~21, ->8
   47    43    > > RETURN                                                   !0
   48    44*     > RETURN                                                   null

End of function insert_node

Function bf_traverse:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 30
Branch analysis from position: 30
2 jumps found. (Code = 44) Position 1 = 33, Position 2 = 7
Branch analysis from position: 33
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 22
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 25, Position 2 = 30
Branch analysis from position: 25
2 jumps found. (Code = 44) Position 1 = 33, Position 2 = 7
Branch analysis from position: 33
Branch analysis from position: 7
Branch analysis from position: 30
Branch analysis from position: 22
filename:       /in/ro1oV
function name:  bf_traverse
number of ops:  35
compiled vars:  !0 = $root, !1 = $queue, !2 = $cnode
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   51     0  E >   RECV                                             !0      
   52     1        ASSIGN                                                   !1, <array>
   53     2        INIT_FCALL                                               'array_unshift'
          3        SEND_REF                                                 !1
          4        SEND_VAR                                                 !0
          5        DO_ICALL                                                 
   55     6      > JMP                                                      ->30
   56     7    >   INIT_FCALL                                               'array_pop'
          8        SEND_REF                                                 !1
          9        DO_ICALL                                         $5      
         10        ASSIGN                                                   !2, $5
   57    11        FETCH_OBJ_R                                      ~7      !2, 'data'
         12        CONCAT                                           ~8      ~7, '+'
         13        ECHO                                                     ~8
   58    14        FETCH_OBJ_R                                      ~9      !2, 'left'
         15        TYPE_CHECK                                  1020          ~9
         16      > JMPZ                                                     ~10, ->22
         17    >   INIT_FCALL                                               'array_unshift'
         18        SEND_REF                                                 !1
         19        FETCH_OBJ_R                                      ~11     !2, 'left'
         20        SEND_VAL                                                 ~11
         21        DO_ICALL                                                 
   59    22    >   FETCH_OBJ_R                                      ~13     !2, 'right'
         23        TYPE_CHECK                                  1020          ~13
         24      > JMPZ                                                     ~14, ->30
         25    >   INIT_FCALL                                               'array_unshift'
         26        SEND_REF                                                 !1
         27        FETCH_OBJ_R                                      ~15     !2, 'right'
         28        SEND_VAL                                                 ~15
         29        DO_ICALL                                                 
   55    30    >   ISSET_ISEMPTY_CV                                 ~17     !1
         31        BOOL_NOT                                         ~18     ~17
         32      > JMPNZ                                                    ~18, ->7
   62    33    >   ECHO                                                     '%3Cbr%3E'
   63    34      > RETURN                                                   null

End of function bf_traverse

Class Node: [no user functions]

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
169.05 ms | 1402 KiB | 19 Q