Skip to content

Commit 57e772a

Browse files
Implemented Binary Search Tree as Data Structure. Implemented with Iterator Interface. (#174)
Implemented Binary Search Tree as Data Structure. Implemented with Iterator Interface
1 parent cb2bbf9 commit 57e772a

File tree

6 files changed

+1269
-0
lines changed

6 files changed

+1269
-0
lines changed

DIRECTORY.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
* [Avltree](./DataStructures/AVLTree/AVLTree.php)
2222
* [Avltreenode](./DataStructures/AVLTree/AVLTreeNode.php)
2323
* [Treetraversal](./DataStructures/AVLTree/TreeTraversal.php)
24+
* Binarysearchtree
25+
* [Binarytreetraversal](./DataStructures/BinarySearchTree/BinaryTreeTraversal.php)
26+
* [Bstnode](./DataStructures/BinarySearchTree/BSTNode.php)
27+
* [Bstree](./DataStructures/BinarySearchTree/BSTree.php)
28+
* [Duplicatekeyexception](./DataStructures/BinarySearchTree/DuplicateKeyException.php)
2429
* Disjointsets
2530
* [Disjointset](./DataStructures/DisjointSets/DisjointSet.php)
2631
* [Disjointsetnode](./DataStructures/DisjointSets/DisjointSetNode.php)
@@ -131,6 +136,7 @@
131136
* [Conversionstest](./tests/Conversions/ConversionsTest.php)
132137
* Datastructures
133138
* [Avltreetest](./tests/DataStructures/AVLTreeTest.php)
139+
* [Bstreetest](./tests/DataStructures/BSTreeTest.php)
134140
* [Disjointsettest](./tests/DataStructures/DisjointSetTest.php)
135141
* [Doublylinkedlisttest](./tests/DataStructures/DoublyLinkedListTest.php)
136142
* [Queuetest](./tests/DataStructures/QueueTest.php)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
/*
4+
* Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) in Pull Request: #174
5+
* https://github.com/TheAlgorithms/PHP/pull/174
6+
*
7+
* Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request addressing bugs/corrections to this file.
8+
* Thank you!
9+
*/
10+
11+
namespace DataStructures\BinarySearchTree;
12+
13+
class BSTNode
14+
{
15+
public int $key;
16+
/**
17+
* @var mixed
18+
*/
19+
public $value;
20+
public ?BSTNode $left;
21+
public ?BSTNode $right;
22+
public ?BSTNode $parent;
23+
24+
/**
25+
* @param int $key The key of the node.
26+
* @param mixed $value The associated value.
27+
*/
28+
public function __construct(int $key, $value)
29+
{
30+
$this->key = $key;
31+
$this->value = $value;
32+
$this->left = null;
33+
$this->right = null;
34+
$this->parent = null;
35+
}
36+
37+
public function isRoot(): bool
38+
{
39+
return $this->parent === null;
40+
}
41+
42+
public function isLeaf(): bool
43+
{
44+
return $this->left === null && $this->right === null;
45+
}
46+
47+
public function getChildren(): array
48+
{
49+
if ($this->isLeaf()) {
50+
return [];
51+
}
52+
53+
$children = [];
54+
if ($this->left !== null) {
55+
$children['left'] = $this->left;
56+
}
57+
if ($this->right !== null) {
58+
$children['right'] = $this->right;
59+
}
60+
return $children;
61+
}
62+
public function getChildrenCount(): int
63+
{
64+
return count($this->getChildren());
65+
}
66+
}

0 commit comments

Comments
 (0)