Hoa central
Binary.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Tree;
38 
47 class Binary extends Generic
48 {
57  public function insert(Generic $child)
58  {
59  if (!($child instanceof self)) {
60  throw new Exception(
61  'Child must be an instance of Binary; given %s.',
62  0,
63  get_class($child)
64  );
65  }
66 
67  if (true === $this->isDouble()) {
68  throw new Exception(
69  'Cannot insert a new element: left and right child are ' .
70  'already set.',
71  1
72  );
73  }
74 
75  if (false === $this->isSimpleLeft()) {
76  $this->_childs[0] = $child;
77 
78  return $this;
79  }
80 
81  if (true === $this->isSimpleLeft()) {
82  $this->_childs[1] = $child;
83 
84  return $this;
85  }
86  }
87 
95  public function insertLeft(self $child)
96  {
97  if (false === $this->isSimpleLeft()) {
98  throw new Exception('Left child is already set.', 2);
99  }
100 
101  $this->_childs[0] = $child;
102 
103  return $this;
104  }
105 
113  public function insertRight(self $child)
114  {
115  if (true === $this->isSimpleRight()) {
116  throw new Exception('Right child is already set.', 3);
117  }
118 
119  $this->_childs[1] = $child;
120 
121  return $this;
122  }
123 
130  public function delete($i)
131  {
132  if ($i === 0) {
133  $this->_childs[0] = null;
134 
135  return $this;
136  }
137 
138  $this->_childs[1] = null;
139 
140  return $this;
141  }
142 
148  public function deleteLeft()
149  {
150  return $this->delete(0);
151  }
152 
158  public function deleteRight()
159  {
160  return $this->delete(1);
161  }
162 
169  public function isSimpleLeft()
170  {
171  return
172  null !== $this->getLeft() &&
173  null === $this->getRight();
174  }
175 
182  public function isSimpleRight()
183  {
184  return
185  null === $this->getLeft() &&
186  null !== $this->getRight();
187  }
188 
194  public function isDouble()
195  {
196  return
197  null !== $this->getLeft() &&
198  null !== $this->getRight();
199  }
200 
206  public function isLeaf()
207  {
208  return
209  null === $this->getLeft() &&
210  null === $this->getRight();
211  }
212 
218  public function isNode()
219  {
220  return
221  null !== $this->getLeft() ||
222  null !== $this->getRight();
223  }
224 
230  public function getLeft()
231  {
232  if (array_key_exists(0, $this->_childs)) {
233  return $this->_childs[0];
234  }
235 
236  return null;
237  }
238 
244  public function getRight()
245  {
246  if (array_key_exists(1, $this->_childs)) {
247  return $this->_childs[1];
248  }
249 
250  return null;
251  }
252 
260  public function getChild($nodeId)
261  {
262  if (false === $i = $this->_childExists($nodeId)) {
263  throw new Exception('Child %s does not exist.', 0, $nodeId);
264  }
265 
266  return $this->_childs[$i];
267  }
268 
275  public function childExists($nodeId)
276  {
277  return false !== $this->_childExist($nodeId);
278  }
279 
287  private function _childExists($nodeId)
288  {
289  if ((null !== $left = $this->getLeft()) &&
290  $left->getValue()->getId() === $nodeId) {
291  return 0;
292  }
293 
294  if ((null !== $right = $this->getLeft()) &&
295  $right->getValue()->getId() === $nodeId) {
296  return 1;
297  }
298 
299  return false;
300  }
301 }
insertRight(self $child)
Definition: Binary.php:113
_childExists($nodeId)
Definition: Binary.php:287
childExists($nodeId)
Definition: Binary.php:275
insertLeft(self $child)
Definition: Binary.php:95
getChild($nodeId)
Definition: Binary.php:260
insert(Generic $child)
Definition: Binary.php:57