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