| Total Complexity | 18 |
| Total Lines | 94 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 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() |
||
| 18 | { |
||
| 19 | $this->root = null; |
||
| 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 | |||
| 65 | } |
||
| 66 | |||
| 67 | public function traverse($order) |
||
| 68 | { |
||
| 69 | switch($order) |
||
| 70 | { |
||
| 71 | case self::IN_ORDER: |
||
| 72 | //TODO |
||
| 73 | break; |
||
| 74 | |||
| 75 | case self::PRE_ORDER: |
||
| 76 | //TODO |
||
| 77 | break; |
||
| 78 | |||
| 79 | case self::POST_ORDER: |
||
| 80 | //TODO |
||
| 81 | break; |
||
| 82 | |||
| 83 | default: |
||
| 84 | 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"); |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | private function in_order_traversal($node, $results) |
||
| 89 | { |
||
| 90 | |||
| 91 | } |
||
| 92 | |||
| 93 | private function validate_parameter($node) |
||
| 103 | } |
||
| 104 | } |
||
| 105 | } |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.