getMaxBalance method

  1. @override
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.

Implementation

@override
int getMaxBalance() {
  var maxBalance = 0;
  for (var i = 0; i < _nodeCapacity; ++i) {
    final node = _nodes[i];
    if (node.height <= 1) {
      continue;
    }

    assert(node.child1 != null);
    assert(node.child2 != null);
    final balance = (node.child2!.height - node.child1!.height).abs();
    maxBalance = max(maxBalance, balance);
  }
  return maxBalance;
}