Skip to content

Commit 4cbf48e

Browse files
committed
exception when no comparision returns true
1 parent d3767d2 commit 4cbf48e

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* MIT License
4+
*
5+
* Copyright (c) 2018 Dogan Ucar
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all
15+
* copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
* SOFTWARE.
24+
*/
25+
26+
namespace doganoo\PHPAlgorithms\Common\Exception;
27+
28+
/**
29+
* Class InvalidSearchOperationException
30+
*
31+
* @package doganoo\PHPAlgorithms\Common\Exception
32+
*/
33+
class InvalidSearchComparisionException extends \Exception {
34+
35+
}

src/Datastructure/Graph/Tree/BinarySearchTree.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
namespace doganoo\PHPAlgorithms\Datastructure\Graph\Tree;
2727

2828

29+
use doganoo\PHPAlgorithms\Common\Exception\InvalidSearchComparisionException;
2930
use doganoo\PHPAlgorithms\Common\Interfaces\IBinaryNode;
3031
use doganoo\PHPAlgorithms\Common\Interfaces\IBinaryTree;
3132
use doganoo\PHPAlgorithms\Common\Util\Comparator;
@@ -188,6 +189,7 @@ private function _height(?IBinaryNode $node): int {
188189
*
189190
* @param $value
190191
* @return BinarySearchNode|null
192+
* @throws InvalidSearchComparisionException
191193
*/
192194
public function search($value): ?BinarySearchNode {
193195
/** @var BinarySearchNode $node */
@@ -199,6 +201,8 @@ public function search($value): ?BinarySearchNode {
199201
$node = $node->getLeft();
200202
} else if (Comparator::greaterThan($value, $node->getValue())) {
201203
$node = $node->getRight();
204+
} else {
205+
throw new InvalidSearchComparisionException("no comparision returned true. Maybe you passed different data types (scalar, object)?");
202206
}
203207
}
204208
return null;

0 commit comments

Comments
 (0)