Based on“Data Structures and Algorithm Analysis Edition 3.2 (C++ Version)” from C. A. Shaffer
Properties
An AVL tree is a type of self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property.
Lookup, insertion, and deletion all take O(log n) time in both the average and worst cases.
Operations
Rotate
In discrete mathematics, tree rotation is an operation on a binary tree that changes the structure without interfering with the order of the elements. Rotation is the basic operation of AVL Tree. When rotating, the programmer should note that this operation may result in a new root for the entire tree and take care to update pointers accordingly.
Rotation is used for AVL Tree rebalancing. One can strategically apply rotations to nodes whose left child and right child differ in height by more than 1.
Search
AVL Tree searching is just the same as BST searching: if greater, go left; else, go right.
Insert
- The first step to insert is the same as insert a new node into a BST;
- After that, it’s necessary to check each of the node’s ancestors whether it’s balanced. If not, rotation is needed.
Remove
- The first step to remove is the same as remove a node in a BST;
- After that, balance checking is necessary. If not balanced, rotation is needed.
Performance
For search, insert and remove operations, the costs are all O(logn).