@@ -17,7 +17,7 @@ discard block |
||
17 | 17 | |
18 | 18 | //TODO IMPLEMENT COUNTER |
19 | 19 | |
20 | - public function __construct(&$root=null) |
|
20 | + public function __construct(&$root = null) |
|
21 | 21 | { |
22 | 22 | $this->root = $root; |
23 | 23 | } |
@@ -26,18 +26,18 @@ discard block |
||
26 | 26 | { |
27 | 27 | $this->validate_parameter($node); |
28 | 28 | |
29 | - if($this->root == null) |
|
29 | + if ($this->root == null) |
|
30 | 30 | { |
31 | 31 | $this->root = $node; |
32 | 32 | return $this; |
33 | 33 | } |
34 | 34 | |
35 | 35 | $n = $this->root; |
36 | - while(1) |
|
36 | + while (1) |
|
37 | 37 | { |
38 | - if($node->value < $n->value) |
|
38 | + if ($node->value < $n->value) |
|
39 | 39 | { |
40 | - if($n->left == null) |
|
40 | + if ($n->left == null) |
|
41 | 41 | { |
42 | 42 | $n->left = $node; |
43 | 43 | return $this; |
@@ -45,9 +45,9 @@ discard block |
||
45 | 45 | |
46 | 46 | $n = $n->left; |
47 | 47 | } |
48 | - else if($node->value > $n->value) |
|
48 | + else if ($node->value > $n->value) |
|
49 | 49 | { |
50 | - if($n->right == null) |
|
50 | + if ($n->right == null) |
|
51 | 51 | { |
52 | 52 | $n->right = $node; |
53 | 53 | return $this; |
@@ -55,7 +55,7 @@ discard block |
||
55 | 55 | |
56 | 56 | $n = $n->right; |
57 | 57 | } |
58 | - else if($node->value == $n->value) //node is already in the tree, we do not create duplicates! |
|
58 | + else if ($node->value == $n->value) //node is already in the tree, we do not create duplicates! |
|
59 | 59 | { |
60 | 60 | return $this; |
61 | 61 | } |
@@ -64,7 +64,7 @@ discard block |
||
64 | 64 | |
65 | 65 | public function search($value) |
66 | 66 | { |
67 | - if(gettype($value) != "integer" && gettype($value) != "int" && gettype($value) != "double") |
|
67 | + if (gettype($value) != "integer" && gettype($value) != "int" && gettype($value) != "double") |
|
68 | 68 | { |
69 | 69 | throw new InvalidArgument("Expected a number, got: " . gettype($value) . " !\n"); |
70 | 70 | } |
@@ -74,22 +74,22 @@ discard block |
||
74 | 74 | |
75 | 75 | private function recursive_search($value, $node) |
76 | 76 | { |
77 | - if($node == null) |
|
77 | + if ($node == null) |
|
78 | 78 | { |
79 | 79 | return null; |
80 | 80 | } |
81 | 81 | |
82 | - $eval = $value - $node->value; |
|
83 | - if($eval == 0) |
|
82 | + $eval = $value-$node->value; |
|
83 | + if ($eval == 0) |
|
84 | 84 | { |
85 | 85 | return $node; |
86 | 86 | } |
87 | 87 | |
88 | - if($eval < 0) |
|
88 | + if ($eval < 0) |
|
89 | 89 | { |
90 | 90 | return $this->recursive_search($value, $node->left); |
91 | 91 | } |
92 | - else if($eval > 0) |
|
92 | + else if ($eval > 0) |
|
93 | 93 | { |
94 | 94 | return $this->recursive_search($value, $node->right); |
95 | 95 | } |
@@ -97,7 +97,7 @@ discard block |
||
97 | 97 | |
98 | 98 | public function get_min($node) |
99 | 99 | { |
100 | - while($node->left != null) //find left most leaf |
|
100 | + while ($node->left != null) //find left most leaf |
|
101 | 101 | { |
102 | 102 | $node = $node->left; |
103 | 103 | } |
@@ -109,14 +109,14 @@ discard block |
||
109 | 109 | $this->validate_parameter($nodeA); |
110 | 110 | $this->validate_parameter($nodeB); |
111 | 111 | |
112 | - $eval = $nodeA->value - $nodeB->value; |
|
112 | + $eval = $nodeA->value-$nodeB->value; |
|
113 | 113 | |
114 | - if($eval < 0) |
|
114 | + if ($eval < 0) |
|
115 | 115 | { |
116 | 116 | return -1; //go left |
117 | 117 | } |
118 | 118 | |
119 | - if($eval > 0) |
|
119 | + if ($eval > 0) |
|
120 | 120 | { |
121 | 121 | return 1; //go right |
122 | 122 | } |
@@ -128,7 +128,7 @@ discard block |
||
128 | 128 | { |
129 | 129 | //check if value is in the tree |
130 | 130 | $node = $this->search($value); //this function also handles type checking |
131 | - if($node != null) |
|
131 | + if ($node != null) |
|
132 | 132 | { |
133 | 133 | $this->delete_recursive($this->root, $node->value); |
134 | 134 | } |
@@ -138,28 +138,28 @@ discard block |
||
138 | 138 | |
139 | 139 | private function delete_recursive(&$parent, $value) |
140 | 140 | { |
141 | - if($parent == null) |
|
141 | + if ($parent == null) |
|
142 | 142 | { |
143 | 143 | return $parent; |
144 | 144 | } |
145 | 145 | |
146 | - if($value < $parent->value) |
|
146 | + if ($value < $parent->value) |
|
147 | 147 | { |
148 | 148 | $parent->left = $this->delete_recursive($parent->left, $value); |
149 | 149 | } |
150 | - else if($value > $parent->value) |
|
150 | + else if ($value > $parent->value) |
|
151 | 151 | { |
152 | 152 | $parent->right = $this->delete_recursive($parent->right, $value); |
153 | 153 | } |
154 | 154 | else |
155 | 155 | { |
156 | - if($parent->left == null) |
|
156 | + if ($parent->left == null) |
|
157 | 157 | { |
158 | 158 | $temp = $parent->right; |
159 | 159 | $parent = null; |
160 | 160 | return $temp; |
161 | 161 | } |
162 | - else if($parent->right == null) |
|
162 | + else if ($parent->right == null) |
|
163 | 163 | { |
164 | 164 | $temp = $parent->left; |
165 | 165 | $parent = null; |
@@ -186,7 +186,7 @@ discard block |
||
186 | 186 | |
187 | 187 | private function get_height_recursive($node) |
188 | 188 | { |
189 | - if($node == null) |
|
189 | + if ($node == null) |
|
190 | 190 | { |
191 | 191 | return 0; |
192 | 192 | } |
@@ -194,25 +194,25 @@ discard block |
||
194 | 194 | $lHeight = $this->get_height_recursive($node->left); |
195 | 195 | $rHeight = $this->get_height_recursive($node->right); |
196 | 196 | |
197 | - if($lHeight > $rHeight) |
|
197 | + if ($lHeight > $rHeight) |
|
198 | 198 | { |
199 | - return ($lHeight + 1); |
|
199 | + return ($lHeight+1); |
|
200 | 200 | } |
201 | 201 | else |
202 | 202 | { |
203 | - return ($rHeight + 1); |
|
203 | + return ($rHeight+1); |
|
204 | 204 | } |
205 | 205 | } |
206 | 206 | |
207 | 207 | public function get_level($level) |
208 | 208 | { |
209 | - if(gettype($level) != "integer" && gettype($level) != "int") |
|
209 | + if (gettype($level) != "integer" && gettype($level) != "int") |
|
210 | 210 | { |
211 | 211 | throw new InvalidArgument("Expected an int, got: " . gettype($level) . " !"); |
212 | 212 | } |
213 | 213 | |
214 | 214 | $h = $this->get_height(); |
215 | - if($level > $h) |
|
215 | + if ($level > $h) |
|
216 | 216 | { |
217 | 217 | throw new InvalidArgument("Cannot get the level: " . $level . " as it is greater than the tree level of: " . $h . " !"); |
218 | 218 | } |
@@ -222,18 +222,18 @@ discard block |
||
222 | 222 | return $results; |
223 | 223 | } |
224 | 224 | |
225 | - private function get_level_recursive($level, $node=null, &$results) //it is important that reference to results stays a reference (php pointer) ! |
|
225 | + private function get_level_recursive($level, $node = null, &$results) //it is important that reference to results stays a reference (php pointer) ! |
|
226 | 226 | { |
227 | - if($node == null) |
|
227 | + if ($node == null) |
|
228 | 228 | { |
229 | 229 | return null; |
230 | 230 | } |
231 | 231 | |
232 | - if($level == 1) |
|
232 | + if ($level == 1) |
|
233 | 233 | { |
234 | 234 | array_push($results, $node); |
235 | 235 | } |
236 | - else if($level > 1) |
|
236 | + else if ($level > 1) |
|
237 | 237 | { |
238 | 238 | $this->get_level_recursive($level-1, $node->left, $results); |
239 | 239 | $this->get_level_recursive($level-1, $node->right, $results); |
@@ -242,7 +242,7 @@ discard block |
||
242 | 242 | |
243 | 243 | public function traverse($order) |
244 | 244 | { |
245 | - switch($order) |
|
245 | + switch ($order) |
|
246 | 246 | { |
247 | 247 | case self::IN_ORDER: |
248 | 248 | $results = array(); |
@@ -260,7 +260,7 @@ discard block |
||
260 | 260 | return $results; |
261 | 261 | |
262 | 262 | case self::LEVEL_ORDER: |
263 | - $results =array(); |
|
263 | + $results = array(); |
|
264 | 264 | $this->level_order_traversal($results); |
265 | 265 | return $results; |
266 | 266 | |
@@ -311,7 +311,7 @@ discard block |
||
311 | 311 | |
312 | 312 | private function level_order_traversal(&$results) |
313 | 313 | { |
314 | - for($i = 1; $i <= $this->get_height(); $i++) |
|
314 | + for ($i = 1; $i <= $this->get_height(); $i++) |
|
315 | 315 | { |
316 | 316 | $results[$i] = $this->get_level($i); |
317 | 317 | } |
@@ -319,12 +319,12 @@ discard block |
||
319 | 319 | |
320 | 320 | private function validate_parameter($node) |
321 | 321 | { |
322 | - if(gettype($node) != "object") |
|
322 | + if (gettype($node) != "object") |
|
323 | 323 | { |
324 | 324 | throw new UnexpectedType("Expected a Ptypes\TreeNode, got: " . gettype($node)); |
325 | 325 | } |
326 | 326 | |
327 | - if(get_class($node) != "Ptypes\TreeNode") |
|
327 | + if (get_class($node) != "Ptypes\TreeNode") |
|
328 | 328 | { |
329 | 329 | throw new UnexpectedType("Expected a Ptypes\TreeNode, got: " . get_class($node)); |
330 | 330 | } |