/** * Compares this binary tree with the specified comparable object. * The specified comparable object is assumed to be a BinaryTree instance. * * @param object IComparable $obj * The comparable object with which to compare this * binary tree. * @return integer A number less than zero if this binary tree is less * than the specified binary tree; * greater than zero if this binary tree is greater * than the specified binary tree; * zero if the two trees are identical. */ protected function compareTo(IComparable $obj) { if ($this->isEmpty()) { return $obj->isEmpty() ? 0 : -1; } elseif ($obj->isEmpty()) { return 1; } else { $result = $this->getKey()->compare($obj->getKey()); if ($result == 0) { $result = $this->getLeft()->compare($obj->getLeft()); } if ($result == 0) { $result = $this->getRight()->compare($obj->getRight()); } return $result; } }