Skip to content

Commit

Permalink
Implemented Binary Search Tree Data Structure with Iterator.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramy-Badr-Ahmed committed Oct 13, 2024
1 parent f546cb3 commit b595461
Show file tree
Hide file tree
Showing 5 changed files with 1,284 additions and 10 deletions.
58 changes: 55 additions & 3 deletions DataStructures/BinarySearchTree/BSTNode.php
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());
}
}
Loading

0 comments on commit b595461

Please sign in to comment.