@@ 378-399 (lines=22) @@ | ||
375 | * @param int|string $key the key used to store. |
|
376 | * @return DataStructures\Trees\Nodes\BSTNode|null the node or null. |
|
377 | */ |
|
378 | public function search($key) { |
|
379 | if($this->root === null) { |
|
380 | return null; |
|
381 | } |
|
382 | ||
383 | if($this->root->key === $key) { |
|
384 | return $this->root; |
|
385 | } else { |
|
386 | $node = $this->root; |
|
387 | while($node !== null) { |
|
388 | if($key < $node->key) { |
|
389 | $node = $node->left; |
|
390 | } else if($key > $node->key) { |
|
391 | $node = $node->right; |
|
392 | } else { |
|
393 | return $node; |
|
394 | } |
|
395 | } |
|
396 | } |
|
397 | ||
398 | return null; |
|
399 | } |
|
400 | ||
401 | /** |
|
402 | * Returns true if is leaf the node. |
@@ 11-32 (lines=22) @@ | ||
8 | protected $root; |
|
9 | protected $size; |
|
10 | ||
11 | public function search($key) { |
|
12 | if($this->root === null) { |
|
13 | return null; |
|
14 | } |
|
15 | ||
16 | if($this->root->key === $key) { |
|
17 | return $this->root->data; |
|
18 | } else { |
|
19 | $node = $this->root; |
|
20 | while($node !== null) { |
|
21 | if($key < $node->left) { |
|
22 | $node = $node->left; |
|
23 | } else if($key > $node->right) { |
|
24 | $node = $node->right; |
|
25 | } else { |
|
26 | return $node->data; |
|
27 | } |
|
28 | } |
|
29 | } |
|
30 | ||
31 | return null; |
|
32 | } |
|
33 | ||
34 | public function isLeaf(BinaryTreeNode $node) { |
|
35 | return ($node !== null && $node->left === null && $node->right === null); |