-
Notifications
You must be signed in to change notification settings - Fork 2
/
lowest_common_ancestor.php
117 lines (103 loc) · 3.01 KB
/
lowest_common_ancestor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
class Node
{
public $parent = null;
public $left = null;
public $right = null;
public $data = null;
public function __construct($data)
{
$this->data = $data;
}
public function __toString()
{
return $this->data;
}
}
class BinaryTree
{
protected $_root = null;
protected function _insert(&$new, &$node)
{
if ($node == null) {
$node = $new;
return;
}
if ($new->data <= $node->data) {
if ($node->left == null) {
$node->left = $new;
$new->parent = $node;
} else {
$this->_insert($new, $node->left);
}
} else {
if ($node->right == null) {
$node->right = $new;
$new->parent = $node;
} else {
$this->_insert($new, $node->right);
}
}
}
protected function _search($target, &$node, $parents)
{
if ($target == $node->data) {
return $parents;
} else if ($target > $node->data && isset($node->right)) {
array_push($parents, $node->data);
return $this->_search($target, $node->right, $parents);
} else if ($target <= $node->data && isset($node->left)) {
array_push($parents, $node->data);
return $this->_search($target, $node->left, $parents);
}
return 0;
}
public function insert($node)
{
$this->_insert($node, $this->_root);
}
public function search($item)
{
$parents = array();
return $this->_search($item, $this->_root, $parents);
}
}
function lewest_common($parents1, $parents2) {
$arr1 = array_reverse($parents1);
$arr2 = array_reverse($parents2);
for ($i = 0; $i <= count($arr1); $i++) {
for ($j = 0; $j <= count($arr2); $j++) {
if ($arr1[$i] == $arr2[$j]) {
return $arr1[$i];
}
}
}
}
$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);
?>