PHP - Recursive binary tree
I've been having some fun with PHP trying to explore its potentials so I
tried to see if I can implement a binary tree structure. Here is the code:
class Node{
public $leftNode;
public $rightNode;
public $value;
public function Node($value){
$this->value = $value;
}
}
class binTree{
public function inserter(Node $node, $value){
if($value < $node->value){
if($node->leftNode != null){
inserter($node, $value);
}
else{
$node->leftNode = new Node($value);
}
}
else if($value > $node->value){
if($node->rightNode != null){
inserter($node, $value);
}
else{
$node->rightNode = new Node($value);
}
}
}
}
Now for some reason, when I try to call the inserter function within
itself (i.e. inserter ($node, $value), I get this error: Fatal error: Call
to undefined function inserter(). So I tried referencing it via $this and
even binTree:: with no luck. I get Fatal error: Using $this when not in
object context and Fatal error: Allowed memory size of 134217728 bytes
exhausted errors respectively. Can anyone explain what is happening?
No comments:
Post a Comment