getMaxBalance method

int getMaxBalance ()
override

Get the maximum balance of an node in the tree. The balance is the difference in height of the two children of a node.

@return

Implementation

int getMaxBalance() {
  int maxBalance = 0;
  for (int i = 0; i < _nodeCapacity; ++i) {
    if (_height[i] <= 1) {
      continue;
    }

    assert(_child1[i] != NULL_NODE);

    int child1 = _child1[i];
    int child2 = _child2[i];
    int balance = (_height[child2] - _height[child1]).abs();
    maxBalance = Math.max(maxBalance, balance);
  }

  return maxBalance;
}