1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ptypes; |
4
|
|
|
|
5
|
|
|
use Ptypes\TreeNode; |
6
|
|
|
use Ptypes\Exceptions\UnexpectedType; |
7
|
|
|
use Ptypes\Exceptions\InvalidArgument; |
8
|
|
|
|
9
|
|
|
class BinaryTree |
10
|
|
|
{ |
11
|
|
|
public const IN_ORDER = 0; |
12
|
|
|
public const PRE_ORDER = 1; |
13
|
|
|
public const POST_ORDER = 2; |
14
|
|
|
|
15
|
|
|
public $root; |
16
|
|
|
|
17
|
|
|
public function __construct($root=null) |
18
|
|
|
{ |
19
|
|
|
$this->root = $root; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function insert($node) //$node is a treenode |
23
|
|
|
{ |
24
|
|
|
$this->validate_parameter($node); |
25
|
|
|
|
26
|
|
|
if($this->root == null) |
27
|
|
|
{ |
28
|
|
|
$this->root = $node; |
29
|
|
|
return $this; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$n = $this->root; |
33
|
|
|
while(1) |
34
|
|
|
{ |
35
|
|
|
if($node->value < $n->value) |
36
|
|
|
{ |
37
|
|
|
if($n->left == null) |
38
|
|
|
{ |
39
|
|
|
$n->left = $node; |
40
|
|
|
return $this; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$n = $n->left; |
44
|
|
|
} |
45
|
|
|
else if($node->value > $n->value) |
46
|
|
|
{ |
47
|
|
|
if($n->right == null) |
48
|
|
|
{ |
49
|
|
|
$n->right = $node; |
50
|
|
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$n = $n->right; |
54
|
|
|
} |
55
|
|
|
else if($node->value == $n->value) //node is already in the tree, we do not create duplicates! |
56
|
|
|
{ |
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function search($value) |
63
|
|
|
{ |
64
|
|
|
if(gettype($value) != "integer" && gettype($value) != "int" && gettype($value) != "double") |
65
|
|
|
{ |
66
|
|
|
throw new InvalidArgument("Expected a number, got: " . gettype($value) . " !\n"); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $this->recursive_search($value, $this->root); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
private function recursive_search($value, $node) |
73
|
|
|
{ |
74
|
|
|
if($node == null) |
75
|
|
|
{ |
76
|
|
|
return null; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$eval = $value - $node->value; |
80
|
|
|
if($eval == 0) |
81
|
|
|
{ |
82
|
|
|
return $node; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if($eval < 0) |
86
|
|
|
{ |
87
|
|
|
return $this->recursive_search($value, $node->left); |
88
|
|
|
} |
89
|
|
|
else if($eval > 0) |
90
|
|
|
{ |
91
|
|
|
return $this->recursive_search($value, $node->right); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function delete($value) |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
//TODO |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function is_complete() |
101
|
|
|
{ |
102
|
|
|
//TODO |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function traverse($order) |
106
|
|
|
{ |
107
|
|
|
switch($order) |
108
|
|
|
{ |
109
|
|
|
case self::IN_ORDER: |
110
|
|
|
$this->in_order_traversal($this->root); |
111
|
|
|
break; |
112
|
|
|
|
113
|
|
|
case self::PRE_ORDER: |
114
|
|
|
//TODO |
115
|
|
|
break; |
116
|
|
|
|
117
|
|
|
case self::POST_ORDER: |
118
|
|
|
//TODO |
119
|
|
|
break; |
120
|
|
|
|
121
|
|
|
default: |
122
|
|
|
throw new InvalidArgument("Invalid order: " . $order . "! Valid orders: IN_ORDER (BinaryTree::IN_ORDER), PRE_ORDER (BinaryTree::PRE_ORDER), and POST_ORDER (BinaryTree::POST_ORDER).\n"); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
private function in_order_traversal($node) |
127
|
|
|
{ |
128
|
|
|
//TODO RETURN ARRAY OF RESULTS |
129
|
|
|
if ($node == null) |
130
|
|
|
{ |
131
|
|
|
return null; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$this->in_order_traversal($node->left); |
135
|
|
|
|
136
|
|
|
echo $node->value . " "; |
137
|
|
|
|
138
|
|
|
$this->in_order_traversal($node->right); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
private function validate_parameter($node) |
142
|
|
|
{ |
143
|
|
|
if(gettype($node) != "object") |
144
|
|
|
{ |
145
|
|
|
throw new UnexpectedType("Expected a Ptypes\TreeNode, got: " . gettype($node)); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
if(get_class($node) != "Ptypes\TreeNode") |
149
|
|
|
{ |
150
|
|
|
throw new UnexpectedType("Expected a Ptypes\TreeNode, got: " . get_class($node)); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
} |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.