This repository was archived by the owner on Sep 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathBinarySearchTree.php
More file actions
209 lines (193 loc) · 5.32 KB
/
Copy pathBinarySearchTree.php
File metadata and controls
209 lines (193 loc) · 5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
/**
* @author Jan Tabacki
*
* Implementation of Binary Search Tree
* https://en.wikipedia.org/wiki/Binary_search_tree
*/
class BinarySearchTree
{
private $root;
public function __construct()
{
$this->root = null;
}
/**
* Adding new element to BST
* @param {any} Value stored in BST, this must be comparable value because
* this implementation does not assume the use of a key
* @returns {this BST or null} when succesfull add of new element returns this BST otherwise returns null
*/
public function insert($value)
{
$newNode = new Node($value);
if ($this->root == null) {
$this->root = $newNode;
return $this;
}
$current = $this->root;
while (true) {
if ($value == $current->value) {
return null;
}
if ($value < $current->value) {
if ($current->left == null) {
$current->left = $newNode;
return $this;
}
$current = $current->left;
} else {
if ($current->right == null) {
$current->right = $newNode;
return $this;
}
$current = $current->right;
}
}
}
/**
* Deletes given element in BST
* @param {any} Value stored in BST
*/
public function delete($value)
{
if ($this->root == null) {
return;
} else {
$this->findAndDelete($this->root, $value);
}
}
private function minValueNode($node)
{
$current = $node;
while ($current->left != null) {
$current = $current->left;
}
return $current;
}
private function findAndDelete($root, $value)
{
if ($value < $root->value) {
$root->left = $this->findAndDelete($root->left, $value);
} else if ($value > $root->value) {
$root->right = $this->findAndDelete($root->right, $value);
} else {
if ($root->left == null) {
$temp = $root->right;
$root = null;
return $temp;
} else if ($root->right == null) {
$temp = $root->left;
$root = null;
return $temp;
}
$temp = $this->minValueNode($root->right);
$root->value = $temp->value;
$root->right = $this->findAndDelete($root->right, $temp->value);
}
return $root;
}
/**
* Finding element in BST
* @param {any} Value stored in BST
* @returns {returns found node or null} when succesfull match returns found node otherwise returns null
*/
public function find($value)
{
if ($this->root == null) {
return false;
}
$current = $this->root;
$found = false;
while ($current && !$found) {
if ($value < $current->value) {
$current = $current->left;
} else if ($value > $current->value) {
$current = $current->right;
} else {
$found = true;
}
}
if (!$found) {
return null;
}
return $current;
}
/**
* Check if elements exists in BST
* @param {any} Value stored in BST
* @returns {bool} when succesfull match returns true otherwise returns false
*/
public function contains($value)
{
if ($this->root == null) {
return false;
}
$current = $this->root;
$found = false;
while ($current && !$found) {
if ($value < $current->value) {
$current = $current->left;
} else if ($value > $current->value) {
$current = $current->right;
} else {
return true;
}
}
return false;
}
/**
* BreadthFirstSearch
* @returns {array} of BST nodes values
*/
public function BreadthFirstSearch()
{
$node = $this->root;
$data = array();
$queue = array();
array_push($queue, $node);
while (count($queue)) {
$node = array_shift($queue);
array_push($data, $node->value);
if ($node->left != null) {
array_push($queue, $node->left);
}
if ($node->right != null) {
array_push($queue, $node->right);
}
}
return $data;
}
private function traverseInOrder($node, &$data)
{
if ($node->left != null) {
$this->traverseInOrder($node->left, $data);
}
array_push($data, $node->value);
if ($node->right != null) {
$this->traverseInOrder($node->right, $data);
}
}
/**
* DepthFirstSearchInOrder
* @returns {array} of ASC orderred BST elements
*/
public function DepthFirstSearchInOrder()
{
$data = array();
$this->traverseInOrder($this->root, $data);
return $data;
}
}
class Node
{
public $value;
public $left;
public $rght;
public function __construct($value)
{
$this->value = $value;
$this->left = null;
$this->right = null;
}
}