Abstract
- A type of Self-Balancing Binary Search Tree (平衡二叉搜索树). Basiclly, an optimised Binary Search Tree (二叉搜索树) that is either empty OR with all nodes that are Height-Balanced
Self-Balancing
When we insert or delete a node and the binary tree becomes unbalanced, AVL tree will use AVL Tree Rotation to auto-adjust the structure of the binary tree, ensuring that the tree remains height-balanced.
Always efficient searching, deletion and insertion operations
The self-balancing property guarantees that the Tree Height of an AVL tree with nodes is always around . This means operations like searching, insertion, and deletion take a maximum of time, making them very efficient.
- Below is an implementation of AVL tree using Java, read AVL Tree Node, AVL Tree Rotation, AVL Tree Insertion and AVL Tree Deletion for a code breakdown and explanation
AVL Tree Node
- The node should have a piece of information that describes its current Node Height, so its parent node is able to calculate the AVL Tree Balance Factor to see if re-balancing is needed, saving the computation to re-calculate the node height when parent node needs the information
- Below shows the codes for AVL tree node definition
AVL Tree Node Height
- Null Node has a node height of and Leaf Node has a node height of
- Below shows functions to retrieve and update the height information of a node
AVL Tree Balance Factor
- Maintaining the Balance Factor of each node prevents Degenerate Binary Tree. The codes to calculate the balance factor is given below
Practice questions
AVL Tree Rotation
- These rotations are performed to restore the balance whenever the Balance Factor of a node becomes greater than or less than
AVL Tree Right Rotation
- When the Balance Factor of a node(parent node) is bigger than - skewed to the left. We right rotate the parent node to its left child node. So the left child node becomes the parent node and the parent node becomes the child node
What if the left child node has a node as its right node?
In this case, the right node of the left child node is a grandchild node to the parent node. We simply place the grandchild node as the left child of the parent node, as shown in the diagram below.
Code
AVL Tree Left Rotation
- When the Balance Factor of a node(parent node) is smaller than - skewed to the right. We left rotate the parent node to its right child node. So the right child node becomes the parent node and the parent node becomes the child node
What if the right child node has a node as its left node?
In this case, the left node of the right child node is a grandchild node to the parent node. We simply place the grandchild node as the right child of the parent node, as shown in the diagram below.
Code
AVL Tree Left-Right Rotation
- The AVL Tree is skewed to the left side, so we should perform AVL Tree Right Rotation on the parent node. However, the child node has a Balance Factor that is . This means the child node’s right side is one level deeper(there is no way to be deeper because in that case, we will perform self-balancing on the child node already). If we perform a right rotation, the parent node will be added to the child node as the right node, the to the parent’s left hand side is offset by addition of parent node to the child node. Thus, the AVL Tree remains NOT Height-Balanced
- The way to handle this is to perform AVL Tree Left Rotation on the child node first, so the left hand side of the child node is one level deeper than the right side. Now if we perform right rotation on the parent node, the child node will become the new parent node, and its left hand side and right side , so from to
Code
AVL Tree Right-left Rotation
- The AVL Tree is skewed to the right side, so we should perform AVL Tree Left Rotation on the parent node. However, the child node has a Balance Factor that is . This means the child node’s left side is one level deeper(there is no way to be deeper because in that case, we will perform self-balancing on the child node already). If we perform a left rotation, the parent node will be added to the child node as the left node, the to the parent’s right hand side is offset by addition of parent node to the child node. Thus, the AVL Tree remains NOT Height-Balanced
- The way to handle this is to perform AVL Tree Right Rotation on the child node first, so the right hand side of the child node is one level deeper than the left side. Now if we perform left rotation on the parent node, the child node will become the new parent node, and its right hand side and left side , so from to
Code
AVL Tree Rotation Function
- We can encapsulate the logic of AVL Tree Left Rotation, AVL Tree Right Rotation, AVL Tree Left-Right Rotation and AVL Tree Right-left Rotation into a single function
rotate(TreeNode node)
as shown below
AVL Tree Insertion
- Node insertion for AVL Tree is similar to BST Node Insertion. The only difference is that after the node insertion, we need to update the Node Height when recurse back to the Root Node, and perform AVL Tree Rotation to ensure the AVL Tree remains Height-Balanced
AVL Tree Deletion
- Similar to BST Node Deletion, but we need to perform self-balancing after the deletion which can be conveniently performed using Recursion. We are using recursion to find the deleted node, so we don’t need to keep track of the previous node manually
Red-black Tree
- Like AVL Tree, red-black tree is a type of self-balancing binary tree, but with more relaxing balancing requirement, this means less AVL Tree Rotation needed, thus better efficiency for node insertion and deletion