3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php /* 1 / \ 2 3 / \ \ 4 5 6 \ 7 */ $a = ['d'=>1, 'l'=>&$b, 'r'=>&$c]; $b = ['d'=>2, 'l'=>&$d, 'r'=>&$e]; $c = ['d'=>3, 'l'=>null, 'r'=>&$f]; $d = ['d'=>4, 'l'=>null, 'r'=>&$g]; $e = ['d'=>5, 'l'=>null, 'r'=>null]; $f = ['d'=>6, 'l'=>null, 'r'=>null]; $g = ['d'=>7, 'l'=>null, 'r'=>null]; bfs($a); echo "\n"; dfs($a); echo "\n"; traverse($t); echo "\n"; function bfs($t) { $queue = [$t]; while ($queue) { $node = array_shift($queue); echo $node['d']; if (is_array($node['l'])) { array_push($queue, $node['l']); } if (is_array($node['r'])) { array_push($queue, $node['r']); } } } function dfs($t) { $stack = [$t]; while ($stack) { $node = array_pop($stack); echo $node['d']; if (is_array($node['r'])) { array_push($stack, $node['r']); } if (is_array($node['l'])) { array_push($stack, $node['l']); } } } function traverse($t) { if (isset($t['d'])) { echo $t['d']; } if (is_array($t['l'])) { traverse($t['l']); } if (is_array($t['r'])) { traverse($t['r']); } } $a = [5,1,2,8,3,4,1]; qsort($a); var_dump($a); function qsort(&$arr, $l = 0, $h = null) { if (is_null($h)) $h = count($arr) - 1; if ($l >= $h) return; $v = $arr[$l]; $i = $l; $j = $h; while ($i < $j) { while ($arr[$j] >= $v && $i < $j) { $j--; } while ($arr[$i] <= $v && $i < $j) { $i++; } if ($i == $j) { $arr[$l] = $arr[$i]; $arr[$i] = $v; break; } $tmp = $arr[$i]; $arr[$i] = $arr[$j]; $arr[$j] = $tmp; } qsort($arr, $l, $i - 1); qsort($arr, $i + 1, $h); } var_dump(binsearch($a, 0)); var_dump(binsearch($a, 1)); var_dump(binsearch($a, 6)); var_dump(binsearch($a, 8)); var_dump(binsearch($a, 9)); function binsearch($arr, $val) { $l = 0; $h = count($arr) - 1; while ($l <= $h) { $m = intval(($l + $h) / 2); if ($arr[$m] == $val) { return $m; } if ($arr[$m] < $val) { $l = $m + 1; } else { $h = $m - 1; } } return -1; }
based on FTIfo
Output for 5.6.0 - 5.6.30, hhvm-3.15.4, 7.0.0 - 7.2.0
1234567 1247536 Notice: Undefined variable: t in /in/oBVq8 on line 23 array(7) { [0]=> int(1) [1]=> int(1) [2]=> int(2) [3]=> int(3) [4]=> int(4) [5]=> int(5) [6]=> int(8) } int(-1) int(1) int(-1) int(6) int(-1)