I have the following code:
<?php
class Node{
public $left,$right;
public $data;
function __construct($data)
{
$this->left=$this->right=null;
$this->data = $data;
}
}
class Solution{
public function insert($root,$data){
if($root==null){
return new Node($data);
}
else{
if($data<=$root->data){
$cur=$this->insert($root->left,$data);
$root->left=$cur;
}
else{
$cur=$this->insert($root->right,$data);
$root->right=$cur;
}
return $root;
}
}
public function getHeight($root) {
$heightLeft = 0;
$heightRight = 0;
if ($root->left != null) {
$heightLeft = getHeight($root->left) + 1;
}
if ($root->right != null) {
$heightRight = getHeight($root->right) + 1;
}
echo "heightRigh is $heightRight
";
echo "heightLeft is $heightLeft
";
$ans = ($heightLeft > $heightRight ? $heightLeft : $heightRight);
return $ans;
}
}//End of Solution
$myTree=new Solution();
$root=null;
$T=intval(fgets(STDIN));
while($T-->0){
$data=intval(fgets(STDIN));
$root=$myTree->insert($root,$data);
}
$height=$myTree->getHeight($root);
echo $height;
?>
When I run it with the inputs 1 1 it gives the correct results.
But when I run it with the inputs 2 1 2
I get the error:
PHP Fatal error: Call to undefined function getHeight() in C:\git\phpStudy\CallingAFunction.php on line 36
Fatal error: Call to undefined function getHeight() in C:\git\phpStudy\CallingAFunction.php on line 36
I am new to php and can't figure out what I am doing wrong. Thank you.