@@ 380-401 (lines=22) @@ | ||
377 | * @param int|string $key the key used to store. |
|
378 | * @return DataStructures\Trees\Nodes\BSTNode|null the node or null. |
|
379 | */ |
|
380 | public function search($key) { |
|
381 | if($this->root === null) { |
|
382 | return null; |
|
383 | } |
|
384 | ||
385 | if($this->root->key === $key) { |
|
386 | return $this->root; |
|
387 | } else { |
|
388 | $node = $this->root; |
|
389 | while($node !== null) { |
|
390 | if($key < $node->key) { |
|
391 | $node = $node->left; |
|
392 | } else if($key > $node->key) { |
|
393 | $node = $node->right; |
|
394 | } else { |
|
395 | return $node; |
|
396 | } |
|
397 | } |
|
398 | } |
|
399 | ||
400 | return null; |
|
401 | } |
|
402 | ||
403 | /** |
|
404 | * Returns true if is leaf the node. |
@@ 22-43 (lines=22) @@ | ||
19 | public function put($key, $data){} |
|
20 | public function putOrUpdate($key, $data){} |
|
21 | ||
22 | public function get($key){ |
|
23 | if($this->root === null) { |
|
24 | return null; |
|
25 | } |
|
26 | ||
27 | if($this->root->key === $key) { |
|
28 | return $this->root->data; |
|
29 | } else { |
|
30 | $node = $this->root; |
|
31 | while($node !== null) { |
|
32 | if($key < $node->left) { |
|
33 | $node = $node->left; |
|
34 | } else if($key > $node->right) { |
|
35 | $node = $node->right; |
|
36 | } else { |
|
37 | return $node->data; |
|
38 | } |
|
39 | } |
|
40 | } |
|
41 | ||
42 | return null; |
|
43 | } |
|
44 | public function getRoot(){ |
|
45 | return $this->root; |
|
46 | } |
|
@@ 80-101 (lines=22) @@ | ||
77 | public function delete($key){} |
|
78 | public function count() {} |
|
79 | ||
80 | public function search($key) { |
|
81 | if($this->root === null) { |
|
82 | return null; |
|
83 | } |
|
84 | ||
85 | if($this->root->key === $key) { |
|
86 | return $this->root->data; |
|
87 | } else { |
|
88 | $node = $this->root; |
|
89 | while($node !== null) { |
|
90 | if($key < $node->left) { |
|
91 | $node = $node->left; |
|
92 | } else if($key > $node->right) { |
|
93 | $node = $node->right; |
|
94 | } else { |
|
95 | return $node->data; |
|
96 | } |
|
97 | } |
|
98 | } |
|
99 | ||
100 | return null; |
|
101 | } |
|
102 | ||
103 | public function isLeaf($node) { // BinaryTreeNode |
|
104 | return ($node !== null && $node->left === null && $node->right === null); |