AVL Tree
...

An AVL tree defined as a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees for any node cannot be more than one.

AVL tree.excalidraw.svg

Height of AVL tree
...

M(h) is the minimum number of node in a AVL tree with height h.
the proof is trivial with recursion.

also we have and since the inequality we wrote earlier looks like Fibonacci and the general formula for Fibonacci is we can say that similarly there exist a number where this proves that the height of AVL tree is logarithmic.

Insertion Algorithm
...

To make sure that the given tree remains AVL after every insertion, we must augment the standard BST insert operation to perform some re-balancing.
Following are two basic operations that can be performed to balance a BST without violating the BST property (keys(left) < key(root) < keys(right)).

  • Left Rotation 
  • Right Rotation

Operation Step By Step
...

  1. Perform the normal BST insertion. 
  2. The current node must be one of the ancestors of the newly inserted node. Update the height of the current node. 
  3. Get the balance factor (left subtree height – right subtree height) of the current node. 
  4. If the balance factor is greater than 1, then the current node is unbalanced and we are either in the Left Left case or left Right case. To check whether it is left left case or not, compare the newly inserted key with the key in the left subtree root
  5. If the balance factor is less than -1, then the current node is unbalanced and we are either in the Right Right case or Right-Left case. To check whether it is the Right Right case or not, compare the newly inserted key with the key in the right subtree root.

Height Difference in Black and Red Tree
...

Black and red tree height diff.excalidraw.svg

the difference between sub trees is equal to the difference between the number of red nodes we also know that this number is at most where is the height of the tree, and we have proven that in black and red tree so the difference is .