Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like BinaryTreeAbstract often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BinaryTreeAbstract, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | abstract class BinaryTreeAbstract implements TreeInterface { |
||
| 23 | protected $root; |
||
| 24 | protected $size; |
||
| 25 | |||
| 26 | public abstract function createNode($key, $data, $parent = null, $left = null, $right = null); |
||
| 27 | /** |
||
| 28 | * Checks if the tree is empty. |
||
| 29 | * |
||
| 30 | * @return boolean true if is empty, else false. |
||
| 31 | */ |
||
| 32 | public function empty() { |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Returns the tree size. |
||
| 38 | * |
||
| 39 | * @return int the length |
||
| 40 | */ |
||
| 41 | public function size() { |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Inserts data in the correct position. |
||
| 47 | * |
||
| 48 | * @param integer|string $key the key used to store. |
||
| 49 | * @param mixed $data the data to store. |
||
| 50 | * @param bool $update if false the node isn't updated |
||
| 51 | * else if the key matches is updated. |
||
| 52 | */ |
||
| 53 | public function put($key, $data, $update = false) { |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Creates a new node or updates it if already exists. |
||
| 90 | * |
||
| 91 | * @param int|string $key the key. |
||
| 92 | * @param mixed $data the data to be stored. |
||
| 93 | */ |
||
| 94 | public function putOrUpdate($key, $data) { |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Retrieve the data stored in the tree. |
||
| 100 | * |
||
| 101 | * @param int|string $key the key to identify the data. |
||
| 102 | * @return mixed |
||
| 103 | */ |
||
| 104 | public function get($key){ |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Returns the root node. |
||
| 125 | * |
||
| 126 | * @return DataStructures\Trees\Nodes\BSTNode|null the root node. |
||
| 127 | */ |
||
| 128 | public function getRoot(){ |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Looks for the node with the given key. |
||
| 134 | * |
||
| 135 | * @param int|string $key the key used to look for. |
||
| 136 | * @return bool true if was found. |
||
| 137 | */ |
||
| 138 | public function exists($key){ |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Method that retrieves true if found a node with the specified key. |
||
| 164 | * It's the recursive version of exists method and it's used internally |
||
| 165 | * for traverse through a root node. |
||
| 166 | * |
||
| 167 | * @param DataStructures\Trees\Nodes\BSTNode|null $node the root node. |
||
| 168 | * @param int|string $key the key used to look for. |
||
| 169 | * @return bool true if exists a node with that key. |
||
| 170 | */ |
||
| 171 | private function _exists($node, $key) : bool { |
||
| 184 | |||
| 185 | public function floor($key){} |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Gets the node with the minimum key. The most left and more bottom. |
||
| 190 | * |
||
| 191 | * @return DataStructures\Trees\Nodes\BSTNode|null the minum node or |
||
| 192 | * null if the tree is empty. |
||
| 193 | */ |
||
| 194 | public function min() { |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Gets the node with the maximum key. The most right and more bottom. |
||
| 213 | * |
||
| 214 | * @return DataStructures\Trees\Nodes\BSTNode|null the maximum node or |
||
| 215 | * null if the tree is empty. |
||
| 216 | */ |
||
| 217 | public function max() { |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Returns the minimum node from a given node in position X. |
||
| 236 | * |
||
| 237 | * @param DataStructures\Trees\Nodes\BSTNode $node the start point. |
||
| 238 | * @return DataStructures\Trees\Nodes\BSTNode|null the minimum node. |
||
| 239 | */ |
||
| 240 | View Code Duplication | private function getMinNode(BinaryNodeInterface $node = null) { |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Returns the maximum node from a given node in position X. |
||
| 254 | * |
||
| 255 | * @param DataStructures\Trees\Nodes\BSTNode $node the start point. |
||
| 256 | * @return DataStructures\Trees\Nodes\BSTNode|null the maximum node. |
||
| 257 | */ |
||
| 258 | View Code Duplication | private function getMaxNode(BinaryNodeInterface $node = null) { |
|
| 269 | |||
| 270 | /** |
||
| 271 | * Deletes the node with the minimum key and returns it. The most left and more bottom. |
||
| 272 | * |
||
| 273 | * @param DataStructures\Trees\Nodes\BSTNode|null if null takes the root. |
||
| 274 | * @return DataStructures\Trees\Nodes\BSTNode|null the minimum node or |
||
| 275 | * null if the tree is empty. |
||
| 276 | */ |
||
| 277 | View Code Duplication | public function deleteMin(BinaryNodeInterface $node = null) { |
|
| 285 | |||
| 286 | /** |
||
| 287 | * Deletes the node with the maximum key and returns it. The most right and more bottom. |
||
| 288 | * |
||
| 289 | * @param DataStructures\Trees\Nodes\BSTNode|null if null takes the root. |
||
| 290 | * @return DataStructures\Trees\Nodes\BSTNode|null the maximum node or |
||
| 291 | * null if the tree is empty. |
||
| 292 | */ |
||
| 293 | View Code Duplication | public function deleteMax(BinaryNodeInterface $node = null) { |
|
| 301 | |||
| 302 | /** |
||
| 303 | * Deletes the node with the maximum key and returns it. The most right and more bottom. |
||
| 304 | * |
||
| 305 | * @param DataStructures\Trees\Nodes\BSTNode|null if null takes the root. |
||
| 306 | * @return DataStructures\Trees\Nodes\BSTNode|null the maximum node or |
||
| 307 | * null if the tree is empty. |
||
| 308 | */ |
||
| 309 | public function delete($key) { |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Deletes the selected node if is not null and returns the node |
||
| 321 | * that replaces the deleted node. Also decrease the size of tree. |
||
| 322 | * |
||
| 323 | * @param DataStructures\Trees\Nodes\BSTNode|null The node to be deleted. |
||
| 324 | * @return the node that replaces the deleted. |
||
| 325 | */ |
||
| 326 | protected function _delete(BinaryNodeInterface &$node) { |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Replaces the node n to remove a new one k and links k with the parent |
||
| 356 | * of n. |
||
| 357 | * |
||
| 358 | * @param DataStructures\Trees\Nodes\BSTNode $nodeToReplace the node to remove. |
||
| 359 | * @param DataStructures\Trees\Nodes\BSTNode|null $newNode the newNode |
||
| 360 | * to link with the $nodeToReplace parent. |
||
| 361 | * @return DataStructures\Trees\Nodes\BSTNode the new linked node. |
||
| 362 | */ |
||
| 363 | protected function replace(&$nodeToReplace, &$newNode) { |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Retrieves the node with the specified key. |
||
| 381 | * |
||
| 382 | * @param int|string $key the key used to store. |
||
| 383 | * @return DataStructures\Trees\Nodes\BSTNode|null the node or null. |
||
| 384 | */ |
||
| 385 | public function search($key) { |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Returns true if is leaf the node. |
||
| 410 | * |
||
| 411 | * @param DataStructures\Trees\Nodes\BSTNode|null $node default to null. |
||
| 412 | * @return true if is leaf the node, is not null and their subtrees has no |
||
| 413 | * pointers to successors. |
||
| 414 | */ |
||
| 415 | public function isLeaf($node) { // BinaryTreeNode |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Checks if a node is root. New nodes that does not point to any other node |
||
| 421 | * also are called a root node. |
||
| 422 | * |
||
| 423 | * @param DataStructures\Trees\Nodes\BSTNode|null $node default to null. |
||
| 424 | * @return true if is root the node, is not null and their subtrees has no |
||
| 425 | * pointers to successors. |
||
| 426 | */ |
||
| 427 | public function isRoot($node) { |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Traverse in preorder. This is: first visit the root, then |
||
| 433 | * the left subtree and finally the right subtree. |
||
| 434 | * |
||
| 435 | * @param Callable|null $callback the callback function to apply to each |
||
| 436 | * node. |
||
| 437 | */ |
||
| 438 | public function preorder(Callable $callback = null) { |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Private preorder traverse method that is recursive and is called by |
||
| 444 | * the public preorder method. |
||
| 445 | * |
||
| 446 | * @param DataStructures\Trees\Nodes\BSTNode|null $node. |
||
| 447 | * @param Callable|null $callback the callback function to apply to each |
||
| 448 | * node. |
||
| 449 | */ |
||
| 450 | View Code Duplication | private function _preorder($node, Callable $callback = null) { |
|
| 460 | |||
| 461 | /** |
||
| 462 | * Traverse in inorder. This is: first visit the left subtree, |
||
| 463 | * then the root and finally the right subtree. |
||
| 464 | * |
||
| 465 | * @param Callable|null $callback the callback function to apply to each |
||
| 466 | * node. |
||
| 467 | */ |
||
| 468 | public function inorder(Callable $callback = null) { |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Private inorder traverse method that is recursive and is called by |
||
| 474 | * the public inorder method. |
||
| 475 | * |
||
| 476 | * @param DataStructures\Trees\Nodes\BSTNode|null $node. |
||
| 477 | * @param Callable|null $callback the callback function to apply to each |
||
| 478 | * node. |
||
| 479 | */ |
||
| 480 | View Code Duplication | private function _inorder($node, Callable $callback = null) { |
|
| 491 | |||
| 492 | /** |
||
| 493 | * Traverse in postorder. This is: first visit the left subtree, |
||
| 494 | * then the right subtree and finally the root. |
||
| 495 | * |
||
| 496 | * @param Callable|null $callback the callback function to apply to each |
||
| 497 | * node. |
||
| 498 | */ |
||
| 499 | public function postorder(Callable $callback = null) { |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Private postorder traverse method that is recursive and is called by |
||
| 505 | * the public postorder method. |
||
| 506 | * |
||
| 507 | * @param DataStructures\Trees\Nodes\BSTNode|null $node. |
||
| 508 | * @param Callable|null $callback the callback function to apply to each |
||
| 509 | * node. |
||
| 510 | */ |
||
| 511 | View Code Duplication | private function _postorder($node, Callable $callback = null) { |
|
| 521 | |||
| 522 | /** |
||
| 523 | * Binds to count() method. This is equal to make $this->tree->size(). |
||
| 524 | * |
||
| 525 | * @return integer the tree size. 0 if it is empty. |
||
| 526 | */ |
||
| 527 | public function count() { |
||
| 530 | } |