Skip to content

Commit

Permalink
Implemented Binary Search Tree as Data Structure. Implemented with It…
Browse files Browse the repository at this point in the history
…erator Interface. (#174)

Implemented Binary Search Tree as Data Structure. Implemented with Iterator Interface
  • Loading branch information
Ramy-Badr-Ahmed authored Oct 19, 2024
1 parent cb2bbf9 commit 57e772a
Show file tree
Hide file tree
Showing 6 changed files with 1,269 additions and 0 deletions.
6 changes: 6 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
* [Avltree](./DataStructures/AVLTree/AVLTree.php)
* [Avltreenode](./DataStructures/AVLTree/AVLTreeNode.php)
* [Treetraversal](./DataStructures/AVLTree/TreeTraversal.php)
* Binarysearchtree
* [Binarytreetraversal](./DataStructures/BinarySearchTree/BinaryTreeTraversal.php)
* [Bstnode](./DataStructures/BinarySearchTree/BSTNode.php)
* [Bstree](./DataStructures/BinarySearchTree/BSTree.php)
* [Duplicatekeyexception](./DataStructures/BinarySearchTree/DuplicateKeyException.php)
* Disjointsets
* [Disjointset](./DataStructures/DisjointSets/DisjointSet.php)
* [Disjointsetnode](./DataStructures/DisjointSets/DisjointSetNode.php)
Expand Down Expand Up @@ -131,6 +136,7 @@
* [Conversionstest](./tests/Conversions/ConversionsTest.php)
* Datastructures
* [Avltreetest](./tests/DataStructures/AVLTreeTest.php)
* [Bstreetest](./tests/DataStructures/BSTreeTest.php)
* [Disjointsettest](./tests/DataStructures/DisjointSetTest.php)
* [Doublylinkedlisttest](./tests/DataStructures/DoublyLinkedListTest.php)
* [Queuetest](./tests/DataStructures/QueueTest.php)
Expand Down
66 changes: 66 additions & 0 deletions DataStructures/BinarySearchTree/BSTNode.php
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());
}
}
Loading

0 comments on commit 57e772a

Please sign in to comment.