forked from TheAlgorithms/PHP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryTreeTraversal.php
207 lines (183 loc) · 5.43 KB
/
BinaryTreeTraversal.php
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
<?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;
use Iterator;
/**
* Provides tree nodes in several traversal algorithms.
*
* Implements the Iterator Interface. Allows tree traversal using loops in tree in-order manner.
*/
class BinaryTreeTraversal implements Iterator
{
private array $iterationNodes = []; // To store nodes for iteration
protected const IN_ORDER = 'inOrder';
private const PRE_ORDER = 'preOrder';
private const POST_ORDER = 'postOrder';
protected string $traversalType;
private int $currentPosition = 0;
/**
* Helps switch traversal type of the binary tree for the Iterator interface
*/
public function setTraversalType(string $traversalType): void
{
$this->traversalType = $traversalType;
// Reset iterator with new traversal
$this->rewind();
}
/**
* Helper to populate iteration nodes per traversal ype
*/
private function loadTraversedNodes(): void
{
switch ($this->traversalType) {
case self::PRE_ORDER:
$this->preOrderIterator(static::getRoot());
break;
case self::POST_ORDER:
$this->postOrderIterator(static::getRoot());
break;
case self::IN_ORDER:
default:
$this->inOrderIterator(static::getRoot());
}
}
/**
* Returns a flat associative array structure for inOrder Traversal
*/
protected function inOrder(?BSTNode $node): array
{
$result = [];
if ($node !== null) {
$result += $this->inOrder($node->left);
$result[$node->key] = $node->value;
$result += $this->inOrder($node->right);
}
return $result;
}
/**
* Returns a flat associative array structure for preOrder Traversal
*/
protected function preOrder(?BSTNode $node): array
{
$result = [];
if ($node !== null) {
$result[$node->key] = $node->value;
$result += $this->preOrder($node->left);
$result += $this->preOrder($node->right);
}
return $result;
}
/**
* Returns a flat associative array structure for postOrder Traversal
*/
protected function postOrder(?BSTNode $node): array
{
$result = [];
if ($node !== null) {
$result += $this->postOrder($node->left);
$result += $this->postOrder($node->right);
$result[$node->key] = $node->value;
}
return $result;
}
/**
* Returns a flat associative array structure for BFT Traversal
*/
protected function breadthFirst(?BSTNode $node): array
{
$result = [];
if (!isset($node)) {
return $result;
}
$queue = [];
$queue[] = $node;
while (!empty($queue)) {
$currentNode = array_shift($queue);
$result[$currentNode->key] = $currentNode->value;
if ($currentNode->left !== null) {
$queue[] = $currentNode->left;
}
if ($currentNode->right !== null) {
$queue[] = $currentNode->right;
}
}
return $result;
}
/**
* Rewind the iterator to the first element.
*/
public function rewind(): void
{
$this->iterationNodes = [];
$this->currentPosition = 0;
// Load nodes based on traversal type
$this->loadTraversedNodes();
}
/**
* Return the current element if exists
*/
public function current(): ?BSTNode
{
return $this->iterationNodes[$this->currentPosition] ?? null;
}
/**
* Return the key of the current element.
*/
public function key(): int
{
return $this->currentPosition;
}
/**
* Move forward to the next element.
*/
public function next(): void
{
$this->currentPosition++;
}
/**
* Check if the current position is valid.
*/
public function valid(): bool
{
return isset($this->iterationNodes[$this->currentPosition]);
}
/**
* Helper function to traverse the tree in-order and fill the $inOrderNodes array.
*/
private function inOrderIterator(?BSTNode $node): void
{
if ($node !== null) {
$this->inOrderIterator($node->left);
$this->iterationNodes[] = $node;
$this->inOrderIterator($node->right);
}
}
/**
* Helper function to traverse the tree in-order and fill the $preOrderNodes array.
*/
private function preOrderIterator(?BSTNode $node): void
{
if ($node !== null) {
$this->iterationNodes[] = $node;
$this->preOrderIterator($node->left);
$this->preOrderIterator($node->right);
}
}
/**
* Helper function to traverse the tree in-order and fill the $postOrderNodes array.
*/
private function postOrderIterator(?BSTNode $node): void
{
if ($node !== null) {
$this->postOrderIterator($node->left);
$this->postOrderIterator($node->right);
$this->iterationNodes[] = $node;
}
}
}