-
-
Notifications
You must be signed in to change notification settings - Fork 470
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented Binary Search Tree as Data Structure. Implemented with It…
…erator Interface. (#174) Implemented Binary Search Tree as Data Structure. Implemented with Iterator Interface
- Loading branch information
1 parent
cb2bbf9
commit 57e772a
Showing
6 changed files
with
1,269 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
/* | ||
* Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) in Pull Request: #174 | ||
* https://github.com/TheAlgorithms/PHP/pull/174 | ||
* | ||
* Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request addressing bugs/corrections to this file. | ||
* Thank you! | ||
*/ | ||
|
||
namespace DataStructures\BinarySearchTree; | ||
|
||
class BSTNode | ||
{ | ||
public int $key; | ||
/** | ||
* @var mixed | ||
*/ | ||
public $value; | ||
public ?BSTNode $left; | ||
public ?BSTNode $right; | ||
public ?BSTNode $parent; | ||
|
||
/** | ||
* @param int $key The key of the node. | ||
* @param mixed $value The associated value. | ||
*/ | ||
public function __construct(int $key, $value) | ||
{ | ||
$this->key = $key; | ||
$this->value = $value; | ||
$this->left = null; | ||
$this->right = null; | ||
$this->parent = null; | ||
} | ||
|
||
public function isRoot(): bool | ||
{ | ||
return $this->parent === null; | ||
} | ||
|
||
public function isLeaf(): bool | ||
{ | ||
return $this->left === null && $this->right === null; | ||
} | ||
|
||
public function getChildren(): array | ||
{ | ||
if ($this->isLeaf()) { | ||
return []; | ||
} | ||
|
||
$children = []; | ||
if ($this->left !== null) { | ||
$children['left'] = $this->left; | ||
} | ||
if ($this->right !== null) { | ||
$children['right'] = $this->right; | ||
} | ||
return $children; | ||
} | ||
public function getChildrenCount(): int | ||
{ | ||
return count($this->getChildren()); | ||
} | ||
} |
Oops, something went wrong.