-
-
Notifications
You must be signed in to change notification settings - Fork 480
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 Data Structure with Iterator.
- Loading branch information
1 parent
f546cb3
commit b595461
Showing
5 changed files
with
1,284 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,66 @@ | ||
<?php | ||
|
||
/* | ||
* Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) in Pull Request: #173 | ||
* https://github.com/TheAlgorithms/PHP/pull/173 | ||
* 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.