Completed
Push — development ( 2d042e...b2c933 )
by Dylan David
01:38
created

BinaryTree::search()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
eloc 0
nc 1
nop 1
1
<?php
2
3
namespace Ptypes;
4
5
use Ptypes\TreeNode;
6
use Ptypes\Exceptions\UnexpectedType;
7
use Ptypes\Exceptions\InvalidArgument;
8
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)
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

62
	public function search(/** @scrutinizer ignore-unused */ $value)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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)
0 ignored issues
show
Unused Code introduced by
The parameter $node is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

88
	private function in_order_traversal(/** @scrutinizer ignore-unused */ $node, $results)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The method in_order_traversal() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
Unused Code introduced by
The parameter $results is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

88
	private function in_order_traversal($node, /** @scrutinizer ignore-unused */ $results)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
89
	{
90
		
91
	}
92
	
93
	private function validate_parameter($node)
94
	{
95
		if(gettype($node) != "object")
96
		{
97
			throw new UnexpectedType("Expected a Ptypes\TreeNode, got: " . gettype($node));
98
		}
99
		
100
		if(get_class($node) != "Ptypes\TreeNode")
101
		{
102
			throw new UnexpectedType("Expected a Ptypes\TreeNode, got: " . get_class($node));
103
		}
104
	}
105
}