} } } } $a = new Node(30); $b = new Node(52); $c = new Node(8); $d = new Node(3); $e = new Node(20); $f = new Node(10); $g = new Node(29); $t = new BinaryTree(); $t->insert($a); $t->insert($b); $t->insert($c); $t->insert($d); $t->insert($e); $t->insert($f); $t->insert($g); $fh = fopen($argv[1], "r"); while (!feof($fh)) { $test = trim(fgets($fh)); $node_values = explode(" ", $test); if ($node_values[0] != "") { $parents1 = $t->search($node_values[0]); $parents2 = $t->search($node_values[1]); echo lewest_common($parents1, $parents2) . "\n"; } // echo $test; } fclose($fh);
<?php ini_set("memory_limit", "-1"); set_time_limit(0); require_once "src/BinaryTree.class.php"; $tree = new BinaryTree(); $test_values = array(); $limit = 1000000; for ($i = 0; $i < $limit; $i++) { $test_values[] = mt_rand(1, 1000000); } $test_values = array_values(array_unique($test_values)); foreach ($test_values as $value) { $node = new BTNode($value); $tree->add($node); } $needle = $test_values[mt_rand(0, count($test_values) - 1)]; $time1 = -microtime(true); array_search($needle, $test_values); $time1 += microtime(true); echo $time1 * 1000; echo "</br>------------------------</br>"; $time2 = -microtime(true); $tree->search($needle); $time2 += microtime(true); echo $time2 * 1000; echo "</br>------------------------</br>"; echo $time1 * 100 / $time2 . "%";