/** * @return int */ public function calculateHeight() { /* * @var AvlTreeNode $root */ $root = $this->root; if ($root === null) { return 0; } $leftTree = new self(); $leftTree->root($root->left()); $rightTree = new self(); $rightTree->root($root->right()); $root->setHeight(max($leftTree->calculateHeight(), $rightTree->calculateHeight()) + 1); return $root->getHeight(); }
/** * @return int */ public function calculateHeight() { $root = $this->getRoot(); if ($root === null) { return 0; } $leftTree = new self(); $leftTree->setRoot($root->getLeft()); $rightTree = new self(); $rightTree->setRoot($root->getRight()); return max($leftTree->calculateHeight(), $rightTree->calculateHeight()) + 1; }