Answer the question
In order to leave comments, you need to log in
Does a binary node inherit the interface of a non-binary node?
Wrote the basic node interface.
interface NodeInterface
{
public function key();
public function value();
public function __construct($key, $value);
public function contains(NodeInterface $node): bool;
public function addChild(NodeInterface $node);
public function setParent(NodeInterface $node);
public function parent(): ?NodeInterface;
public function children(): array;
// ...
}
interface BinaryNodeInterface extends NodeInterface
{
public function left(): ?BinaryNodeInterface;
public function right(): ?BinaryNodeInterface;
}
class BinaryNode extends Node implements BinaryNodeInterface
{
public function left(): ?BinaryNodeInterface
{
return $this->children[0] ?? null;
}
public function right(): ?BinaryNodeInterface
{
return $this->children[1] ?? null;
}
public function addChild(NodeInterface $node)
{
if (count($this->children) < 2) {
parent::addChild($node);
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question