For my AVL Tree implementation, I have a node which has a left, right and parent pointer, and a balance variable. Each time I am inserting a new node and carrying out the required rotation, I am updating the balance by subtracting the right sub-tree from the left sub-tree. This method in question calls itself recursively to calculate the height.
The method in question:
private int getHeight(Node nd){
if (nd != null){
if (nd.getLeft() == null && nd.getRight() == null)
return 0;
else if (nd.getLeft() == null)
return getHeight(nd.getRight()) + 1;
else if (nd.getRight() == null)
return getHeight(nd.getLeft()) + 1;
else
return Math.max(getHeight(nd.getLeft()), getHeight(nd.getRight())) + 1;
}
else return -1;
}
My question is, since an AVL Tree Insert time complexity is O(log n), I think this method affects on this time complexity. What can be done to amend it?