Hoa central
Public Member Functions | Private Member Functions | List of all members
Hoa\Tree\Binary Class Reference
Inheritance diagram for Hoa\Tree\Binary:

Public Member Functions

 insert (Generic $child)
 
 insertLeft (self $child)
 
 insertRight (self $child)
 
 delete ($i)
 
 deleteLeft ()
 
 deleteRight ()
 
 isSimpleLeft ()
 
 isSimpleRight ()
 
 isDouble ()
 
 isLeaf ()
 
 isNode ()
 
 getLeft ()
 
 getRight ()
 
 getChild ($nodeId)
 
 childExists ($nodeId)
 
- Public Member Functions inherited from Hoa\Tree\Generic
 __construct ($value=null)
 
 setValue ($value)
 
 getValue ()
 
 current ()
 
 key ()
 
 next ()
 
 rewind ()
 
 valid ()
 
 seek ($position)
 
 count ()
 
 getChild ($nodeId)
 
 getChilds ()
 
 childExists ($nodeId)
 
 insert (Generic $child)
 
 delete ($i)
 
 isLeaf ()
 
 isNode ()
 
 accept (Visitor\Visit $visitor, &$handle=null, $eldnah=null)
 
- Public Member Functions inherited from Hoa\Visitor\Element
 accept (Visit $visitor, &$handle=null, $eldnah=null)
 

Private Member Functions

 _childExists ($nodeId)
 

Additional Inherited Members

- Protected Attributes inherited from Hoa\Tree\Generic
 $_value = null
 
 $_childs = []
 

Detailed Description

Class .

Manipulate a binary tree.

Definition at line 47 of file Binary.php.

Member Function Documentation

Hoa\Tree\Binary::_childExists (   $nodeId)
private

Check if a child exists, and return the child index (0 for left and 1 for right).

Parameters
mixed$nodeIdNode ID.
Returns
mixed

Definition at line 287 of file Binary.php.

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  }

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Tree\Binary::childExists (   $nodeId)

Check if a child exists.

Parameters
mixed$nodeIdNode ID.
Returns
bool

Definition at line 275 of file Binary.php.

276  {
277  return false !== $this->_childExist($nodeId);
278  }
Hoa\Tree\Binary::delete (   $i)

Delete a child.

Parameters
int$iChild index.
Returns

Definition at line 130 of file Binary.php.

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  }
Hoa\Tree\Binary::deleteLeft ( )

Delete the left child.

Returns

Definition at line 148 of file Binary.php.

149  {
150  return $this->delete(0);
151  }
Hoa\Tree\Binary::deleteRight ( )

Delete the right child.

Returns

Definition at line 158 of file Binary.php.

159  {
160  return $this->delete(1);
161  }
Hoa\Tree\Binary::getChild (   $nodeId)

Get a specific child (not the same behavior that other trees).

Parameters
mixed$nodeIdNode ID.
Returns
Exceptions

Definition at line 260 of file Binary.php.

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  }
_childExists($nodeId)
Definition: Binary.php:287

Here is the call graph for this function:

Hoa\Tree\Binary::getLeft ( )

Get the left child.

Returns

Definition at line 230 of file Binary.php.

231  {
232  if (array_key_exists(0, $this->_childs)) {
233  return $this->_childs[0];
234  }
235 
236  return null;
237  }

Here is the caller graph for this function:

Hoa\Tree\Binary::getRight ( )

Get the right child.

Returns

Definition at line 244 of file Binary.php.

245  {
246  if (array_key_exists(1, $this->_childs)) {
247  return $this->_childs[1];
248  }
249 
250  return null;
251  }

Here is the caller graph for this function:

Hoa\Tree\Binary::insert ( Generic  $child)

Insert a child. Fill the child list from left to right.

Parameters
\Hoa\Tree\Generic$childChild to insert.
Returns
Exceptions

Definition at line 57 of file Binary.php.

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  }

Here is the call graph for this function:

Hoa\Tree\Binary::insertLeft ( self  $child)

Insert the left child.

Parameters
\Hoa\Tree\Binary$childChild to insert.
Returns
Exceptions

Definition at line 95 of file Binary.php.

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  }

Here is the call graph for this function:

Hoa\Tree\Binary::insertRight ( self  $child)

Insert the right child.

Parameters
\Hoa\Tree\Binary$childChild to insert.
Returns
Exceptions

Definition at line 113 of file Binary.php.

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  }

Here is the call graph for this function:

Hoa\Tree\Binary::isDouble ( )

Check if the node is double, i.e. if left and right child are set.

Returns
bool

Definition at line 194 of file Binary.php.

195  {
196  return
197  null !== $this->getLeft() &&
198  null !== $this->getRight();
199  }

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Tree\Binary::isLeaf ( )

Check if the node is a leaf.

Returns
bool

Definition at line 206 of file Binary.php.

207  {
208  return
209  null === $this->getLeft() &&
210  null === $this->getRight();
211  }

Here is the call graph for this function:

Hoa\Tree\Binary::isNode ( )

Check if the node is a noe (i.e. not a leaf).

Returns
bool

Definition at line 218 of file Binary.php.

219  {
220  return
221  null !== $this->getLeft() ||
222  null !== $this->getRight();
223  }

Here is the call graph for this function:

Hoa\Tree\Binary::isSimpleLeft ( )

Check if the node is simple left, i.e. if the left child is set and not the right child.

Returns
bool

Definition at line 169 of file Binary.php.

170  {
171  return
172  null !== $this->getLeft() &&
173  null === $this->getRight();
174  }

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Tree\Binary::isSimpleRight ( )

Check if the node is simple right, i.e. if the right child is set and not the left child.

Returns
bool

Definition at line 182 of file Binary.php.

183  {
184  return
185  null === $this->getLeft() &&
186  null !== $this->getRight();
187  }

Here is the call graph for this function:

Here is the caller graph for this function:


The documentation for this class was generated from the following file: