File tree Expand file tree Collapse file tree 6 files changed +1269
-0
lines changed
DataStructures/BinarySearchTree Expand file tree Collapse file tree 6 files changed +1269
-0
lines changed Original file line number Diff line number Diff line change 21
21
* [ Avltree] ( ./DataStructures/AVLTree/AVLTree.php )
22
22
* [ Avltreenode] ( ./DataStructures/AVLTree/AVLTreeNode.php )
23
23
* [ 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 )
24
29
* Disjointsets
25
30
* [ Disjointset] ( ./DataStructures/DisjointSets/DisjointSet.php )
26
31
* [ Disjointsetnode] ( ./DataStructures/DisjointSets/DisjointSetNode.php )
131
136
* [ Conversionstest] ( ./tests/Conversions/ConversionsTest.php )
132
137
* Datastructures
133
138
* [ Avltreetest] ( ./tests/DataStructures/AVLTreeTest.php )
139
+ * [ Bstreetest] ( ./tests/DataStructures/BSTreeTest.php )
134
140
* [ Disjointsettest] ( ./tests/DataStructures/DisjointSetTest.php )
135
141
* [ Doublylinkedlisttest] ( ./tests/DataStructures/DoublyLinkedListTest.php )
136
142
* [ Queuetest] ( ./tests/DataStructures/QueueTest.php )
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments