@@ -28,7 +28,7 @@ discard block |
||
28 | 28 | { |
29 | 29 | $tree = new BinaryTree(); |
30 | 30 | $tree->insert(new TreeNode(8))->insert(new TreeNode(5))->insert(new TreeNode(9))->insert(new TreeNode(4))->insert(new TreeNode(14))->insert(new TreeNode(6))->insert(new TreeNode(12)); |
31 | - $this->assertEquals($tree->traverse(BinaryTree::IN_ORDER), array(4,5,6,8,9,12,14)); |
|
31 | + $this->assertEquals($tree->traverse(BinaryTree::IN_ORDER), array(4, 5, 6, 8, 9, 12, 14)); |
|
32 | 32 | } |
33 | 33 | |
34 | 34 | /** @test */ |
@@ -47,7 +47,7 @@ discard block |
||
47 | 47 | $root->right->right->left = new TreeNode(3); |
48 | 48 | |
49 | 49 | $tree = new BinaryTree($root); |
50 | - $this->assertEquals($tree->traverse(BinaryTree::IN_ORDER), array(9,5,1,7,2,12,8,4,3,11)); |
|
50 | + $this->assertEquals($tree->traverse(BinaryTree::IN_ORDER), array(9, 5, 1, 7, 2, 12, 8, 4, 3, 11)); |
|
51 | 51 | } |
52 | 52 | |
53 | 53 | /** @test */ |
@@ -66,7 +66,7 @@ discard block |
||
66 | 66 | $root->right->right->left = new TreeNode(3); |
67 | 67 | |
68 | 68 | $tree = new BinaryTree($root); |
69 | - $this->assertEquals($tree->traverse(BinaryTree::PRE_ORDER), array(8,5,9,7,1,12,2,4,11,3)); |
|
69 | + $this->assertEquals($tree->traverse(BinaryTree::PRE_ORDER), array(8, 5, 9, 7, 1, 12, 2, 4, 11, 3)); |
|
70 | 70 | } |
71 | 71 | |
72 | 72 | /** @test */ |
@@ -85,7 +85,7 @@ discard block |
||
85 | 85 | $root->right->right->left = new TreeNode(3); |
86 | 86 | |
87 | 87 | $tree = new BinaryTree($root); |
88 | - $this->assertEquals($tree->traverse(BinaryTree::POST_ORDER), array(9,1,2,12,7,5,3,11,4,8)); |
|
88 | + $this->assertEquals($tree->traverse(BinaryTree::POST_ORDER), array(9, 1, 2, 12, 7, 5, 3, 11, 4, 8)); |
|
89 | 89 | } |
90 | 90 | |
91 | 91 | /** @test */ |
@@ -104,7 +104,7 @@ discard block |
||
104 | 104 | |
105 | 105 | $tree = new BinaryTree($root); |
106 | 106 | $tree->delete(9); |
107 | - $this->assertEquals($tree->traverse(BinaryTree::IN_ORDER), array(1,3,4,5,8,11,12)); |
|
107 | + $this->assertEquals($tree->traverse(BinaryTree::IN_ORDER), array(1, 3, 4, 5, 8, 11, 12)); |
|
108 | 108 | } |
109 | 109 | |
110 | 110 | /** @test */ |
@@ -167,9 +167,9 @@ discard block |
||
167 | 167 | */ |
168 | 168 | |
169 | 169 | $nodes = $tree->traverse(BinaryTree::LEVEL_ORDER); |
170 | - foreach($nodes as $i => $level) |
|
170 | + foreach ($nodes as $i => $level) |
|
171 | 171 | { |
172 | - foreach($level as $j => $node) |
|
172 | + foreach ($level as $j => $node) |
|
173 | 173 | { |
174 | 174 | echo $node->value . " "; |
175 | 175 | } |
@@ -187,6 +187,6 @@ discard block |
||
187 | 187 | $tree->insert(new TreeNode(80)); |
188 | 188 | $tree->insert(new TreeNode(90)); |
189 | 189 | $tree->delete(50); |
190 | - $this->assertEquals((count($tree) + $tree->size()), 12); |
|
190 | + $this->assertEquals((count($tree)+$tree->size()), 12); |
|
191 | 191 | } |
192 | 192 | } |
193 | 193 | \ No newline at end of file |
@@ -18,9 +18,9 @@ discard block |
||
18 | 18 | |
19 | 19 | private $count = 0; |
20 | 20 | |
21 | - public function __construct(&$root=null) |
|
21 | + public function __construct(&$root = null) |
|
22 | 22 | { |
23 | - if($root != null) |
|
23 | + if ($root != null) |
|
24 | 24 | { |
25 | 25 | $this->validate_parameter($root); |
26 | 26 | } |
@@ -35,18 +35,18 @@ discard block |
||
35 | 35 | |
36 | 36 | $this->count++; |
37 | 37 | |
38 | - if($this->root == null) |
|
38 | + if ($this->root == null) |
|
39 | 39 | { |
40 | 40 | $this->root = $node; |
41 | 41 | return $this; |
42 | 42 | } |
43 | 43 | |
44 | 44 | $n = $this->root; |
45 | - while(1) |
|
45 | + while (1) |
|
46 | 46 | { |
47 | - if($node->value < $n->value) |
|
47 | + if ($node->value < $n->value) |
|
48 | 48 | { |
49 | - if($n->left == null) |
|
49 | + if ($n->left == null) |
|
50 | 50 | { |
51 | 51 | $n->left = $node; |
52 | 52 | return $this; |
@@ -54,9 +54,9 @@ discard block |
||
54 | 54 | |
55 | 55 | $n = $n->left; |
56 | 56 | } |
57 | - else if($node->value > $n->value) |
|
57 | + else if ($node->value > $n->value) |
|
58 | 58 | { |
59 | - if($n->right == null) |
|
59 | + if ($n->right == null) |
|
60 | 60 | { |
61 | 61 | $n->right = $node; |
62 | 62 | return $this; |
@@ -64,7 +64,7 @@ discard block |
||
64 | 64 | |
65 | 65 | $n = $n->right; |
66 | 66 | } |
67 | - else if($node->value == $n->value) //node is already in the tree, we do not create duplicates! |
|
67 | + else if ($node->value == $n->value) //node is already in the tree, we do not create duplicates! |
|
68 | 68 | { |
69 | 69 | return $this; |
70 | 70 | } |
@@ -73,7 +73,7 @@ discard block |
||
73 | 73 | |
74 | 74 | public function search($value) |
75 | 75 | { |
76 | - if(gettype($value) != "integer" && gettype($value) != "int" && gettype($value) != "double") |
|
76 | + if (gettype($value) != "integer" && gettype($value) != "int" && gettype($value) != "double") |
|
77 | 77 | { |
78 | 78 | throw new InvalidArgument("Expected a number, got: " . gettype($value) . " !\n"); |
79 | 79 | } |
@@ -83,22 +83,22 @@ discard block |
||
83 | 83 | |
84 | 84 | private function recursive_search($value, $node) |
85 | 85 | { |
86 | - if($node == null) |
|
86 | + if ($node == null) |
|
87 | 87 | { |
88 | 88 | return null; |
89 | 89 | } |
90 | 90 | |
91 | - $eval = $value - $node->value; |
|
92 | - if($eval == 0) |
|
91 | + $eval = $value-$node->value; |
|
92 | + if ($eval == 0) |
|
93 | 93 | { |
94 | 94 | return $node; |
95 | 95 | } |
96 | 96 | |
97 | - if($eval < 0) |
|
97 | + if ($eval < 0) |
|
98 | 98 | { |
99 | 99 | return $this->recursive_search($value, $node->left); |
100 | 100 | } |
101 | - else if($eval > 0) |
|
101 | + else if ($eval > 0) |
|
102 | 102 | { |
103 | 103 | return $this->recursive_search($value, $node->right); |
104 | 104 | } |
@@ -106,7 +106,7 @@ discard block |
||
106 | 106 | |
107 | 107 | public function get_min($node) |
108 | 108 | { |
109 | - while($node->left != null) //find left most leaf |
|
109 | + while ($node->left != null) //find left most leaf |
|
110 | 110 | { |
111 | 111 | $node = $node->left; |
112 | 112 | } |
@@ -118,14 +118,14 @@ discard block |
||
118 | 118 | $this->validate_parameter($nodeA); |
119 | 119 | $this->validate_parameter($nodeB); |
120 | 120 | |
121 | - $eval = $nodeA->value - $nodeB->value; |
|
121 | + $eval = $nodeA->value-$nodeB->value; |
|
122 | 122 | |
123 | - if($eval < 0) |
|
123 | + if ($eval < 0) |
|
124 | 124 | { |
125 | 125 | return -1; //go left |
126 | 126 | } |
127 | 127 | |
128 | - if($eval > 0) |
|
128 | + if ($eval > 0) |
|
129 | 129 | { |
130 | 130 | return 1; //go right |
131 | 131 | } |
@@ -137,7 +137,7 @@ discard block |
||
137 | 137 | { |
138 | 138 | //check if value is in the tree |
139 | 139 | $node = $this->search($value); //this function also handles type checking |
140 | - if($node != null) |
|
140 | + if ($node != null) |
|
141 | 141 | { |
142 | 142 | $this->delete_recursive($this->root, $node->value); |
143 | 143 | } |
@@ -148,28 +148,28 @@ discard block |
||
148 | 148 | |
149 | 149 | private function delete_recursive(&$parent, $value) |
150 | 150 | { |
151 | - if($parent == null) |
|
151 | + if ($parent == null) |
|
152 | 152 | { |
153 | 153 | return $parent; |
154 | 154 | } |
155 | 155 | |
156 | - if($value < $parent->value) |
|
156 | + if ($value < $parent->value) |
|
157 | 157 | { |
158 | 158 | $parent->left = $this->delete_recursive($parent->left, $value); |
159 | 159 | } |
160 | - else if($value > $parent->value) |
|
160 | + else if ($value > $parent->value) |
|
161 | 161 | { |
162 | 162 | $parent->right = $this->delete_recursive($parent->right, $value); |
163 | 163 | } |
164 | 164 | else |
165 | 165 | { |
166 | - if($parent->left == null) |
|
166 | + if ($parent->left == null) |
|
167 | 167 | { |
168 | 168 | $temp = $parent->right; |
169 | 169 | $parent = null; |
170 | 170 | return $temp; |
171 | 171 | } |
172 | - else if($parent->right == null) |
|
172 | + else if ($parent->right == null) |
|
173 | 173 | { |
174 | 174 | $temp = $parent->left; |
175 | 175 | $parent = null; |
@@ -186,7 +186,7 @@ discard block |
||
186 | 186 | |
187 | 187 | public function size() |
188 | 188 | { |
189 | - return $this->count + 1; |
|
189 | + return $this->count+1; |
|
190 | 190 | } |
191 | 191 | |
192 | 192 | public function get_height() |
@@ -196,7 +196,7 @@ discard block |
||
196 | 196 | |
197 | 197 | private function get_height_recursive($node) |
198 | 198 | { |
199 | - if($node == null) |
|
199 | + if ($node == null) |
|
200 | 200 | { |
201 | 201 | return 0; |
202 | 202 | } |
@@ -204,25 +204,25 @@ discard block |
||
204 | 204 | $lHeight = $this->get_height_recursive($node->left); |
205 | 205 | $rHeight = $this->get_height_recursive($node->right); |
206 | 206 | |
207 | - if($lHeight > $rHeight) |
|
207 | + if ($lHeight > $rHeight) |
|
208 | 208 | { |
209 | - return ($lHeight + 1); |
|
209 | + return ($lHeight+1); |
|
210 | 210 | } |
211 | 211 | else |
212 | 212 | { |
213 | - return ($rHeight + 1); |
|
213 | + return ($rHeight+1); |
|
214 | 214 | } |
215 | 215 | } |
216 | 216 | |
217 | 217 | public function get_level($level) |
218 | 218 | { |
219 | - if(gettype($level) != "integer" && gettype($level) != "int") |
|
219 | + if (gettype($level) != "integer" && gettype($level) != "int") |
|
220 | 220 | { |
221 | 221 | throw new InvalidArgument("Expected an int, got: " . gettype($level) . " !"); |
222 | 222 | } |
223 | 223 | |
224 | 224 | $h = $this->get_height(); |
225 | - if($level > $h) |
|
225 | + if ($level > $h) |
|
226 | 226 | { |
227 | 227 | throw new InvalidArgument("Cannot get the level: " . $level . " as it is greater than the tree level of: " . $h . " !"); |
228 | 228 | } |
@@ -232,18 +232,18 @@ discard block |
||
232 | 232 | return $results; |
233 | 233 | } |
234 | 234 | |
235 | - private function get_level_recursive($level, $node=null, &$results) //it is important that reference to results stays a reference (php pointer) ! |
|
235 | + private function get_level_recursive($level, $node = null, &$results) //it is important that reference to results stays a reference (php pointer) ! |
|
236 | 236 | { |
237 | - if($node == null) |
|
237 | + if ($node == null) |
|
238 | 238 | { |
239 | 239 | return null; |
240 | 240 | } |
241 | 241 | |
242 | - if($level == 1) |
|
242 | + if ($level == 1) |
|
243 | 243 | { |
244 | 244 | array_push($results, $node); |
245 | 245 | } |
246 | - else if($level > 1) |
|
246 | + else if ($level > 1) |
|
247 | 247 | { |
248 | 248 | $this->get_level_recursive($level-1, $node->left, $results); |
249 | 249 | $this->get_level_recursive($level-1, $node->right, $results); |
@@ -252,7 +252,7 @@ discard block |
||
252 | 252 | |
253 | 253 | public function traverse($order) |
254 | 254 | { |
255 | - switch($order) |
|
255 | + switch ($order) |
|
256 | 256 | { |
257 | 257 | case self::IN_ORDER: |
258 | 258 | $results = array(); |
@@ -270,7 +270,7 @@ discard block |
||
270 | 270 | return $results; |
271 | 271 | |
272 | 272 | case self::LEVEL_ORDER: |
273 | - $results =array(); |
|
273 | + $results = array(); |
|
274 | 274 | $this->level_order_traversal($results); |
275 | 275 | return $results; |
276 | 276 | |
@@ -321,7 +321,7 @@ discard block |
||
321 | 321 | |
322 | 322 | private function level_order_traversal(&$results) |
323 | 323 | { |
324 | - for($i = 1; $i <= $this->get_height(); $i++) |
|
324 | + for ($i = 1; $i <= $this->get_height(); $i++) |
|
325 | 325 | { |
326 | 326 | $results[$i] = $this->get_level($i); |
327 | 327 | } |
@@ -329,12 +329,12 @@ discard block |
||
329 | 329 | |
330 | 330 | private function validate_parameter($node) |
331 | 331 | { |
332 | - if(gettype($node) != "object") |
|
332 | + if (gettype($node) != "object") |
|
333 | 333 | { |
334 | 334 | throw new UnexpectedType("Expected a Ptypes\TreeNode, got: " . gettype($node)); |
335 | 335 | } |
336 | 336 | |
337 | - if(get_class($node) != "Ptypes\TreeNode") |
|
337 | + if (get_class($node) != "Ptypes\TreeNode") |
|
338 | 338 | { |
339 | 339 | throw new UnexpectedType("Expected a Ptypes\TreeNode, got: " . get_class($node)); |
340 | 340 | } |
@@ -350,6 +350,6 @@ discard block |
||
350 | 350 | */ |
351 | 351 | public function count() |
352 | 352 | { |
353 | - return $this->count + 1; |
|
353 | + return $this->count+1; |
|
354 | 354 | } |
355 | 355 | } |
356 | 356 | \ No newline at end of file |