AVL TREE
Rotating the subtrees in an AVL Tree
In rotation operation, the positions of the nodes of a subtree are interchanged.There are two types of rotations:
In left-rotation, the arrangement of the nodes on the right is transformed into the arrangements on the left node.
Algorithm
- Let the initial tree be:
- If y has a left subtree, assign x as the parent of the left subtree of y.
- If the parent of x
p
isNULL
, make y as the root of the tree. - Else if x is the left child of p, make y as the left child of p.
- Else assign y as the right child of p.
- Make y as the parent of x.
Right Rotate
In left-rotation, the arrangement of the nodes on the left is transformed into the arrangements on the right node.- Let the initial tree be:
- If x has a right subtree, assign y as the parent of the right subtree of x.
- If the parent of y is
NULL
, make x as the root of the tree. - Else if y is the right child of its parent p, make x as the right child of p.
- Else assign x as the left child of p.
- Make x as the parent of y.
Left-Right and Right-Left Rotate
In left-right rotation, the arrangements are first shifted to the left and then to the right.- Do left rotation on x-y.
- Do right rotation on y-z.
- Do right rotation on x-y.
- Do left rotation on z-y.
Algorithm to insert a newNode
A newNode is always inserted as a leaf node with balance factor equal to 0.- Let the initial tree be:
Let the node to be inserted be:
- Go to the appropriate leaf node to insert a newNode using the following recursive steps. Compare newKey with rootKey of current tree.
- If newKey < rootKey, call insertion algorithm on left subtree of current node until the leaf node is reached.
- Else if newKey > rootKey, call insertion algorithm on the right subtree of current node until the leaf node is reached.
- Else, return leafNode.
- Compare leafKey obtained from above steps with newKey:
- If newKey < leafKey, make newNode as the leftChild of leafNode.
- Else, make newNode as rightChild of leafNode.
- Update balanceFactor of the nodes.
- If the nodes are unbalanced, then rebalance the node.
- If balanceFactor > 1, it means the height of the
left subtree is greater than that of the right subtree. So, do right
rotation or left-right rotation
- If newNodeKey < leftChildKey do right rotation.
- Else, do left-right rotation.
- If balanceFactor < -1, it means the height of the
right subtree is greater than that of the left subtree. So, do right
rotation or right-left rotation
- If newNodeKey > rightChildKey do left rotation.
- Else, do right-left rotation
- If balanceFactor > 1, it means the height of the
left subtree is greater than that of the right subtree. So, do right
rotation or left-right rotation
- The final tree is:
Algorithm to Delete a node
A node is always deleted as a leaf node. After deleting a node, the balance factors of the nodes get changed. In order to rebalance the balance factor, suitable rotations are performed.- Locate nodeToBeDeleted (recursion is used to find nodeToBeDeleted in the code used below).
- There are three cases for deleting a node:
- If nodeToBeDeleted is the leaf node (ie. does not have any child), then remove nodeToBeDeleted.
- If nodeToBeDeleted has one child, then substitute the contents of nodeToBeDeleted with that of child. Remove the child.
- If nodeToBeDeleted has two children, find the inorder successor w of nodeToBeDeleted (ie. node with minimum value of key in the right subtree).
- Substitute the contents of nodeToBeDeleted with that of w.
- Remove the leaf node w.
- Substitute the contents of nodeToBeDeleted with that of w.
- Update balanceFactor of the nodes.
- Rebalance the tree if balance factor of any of the nodes is not equal to -1, 0 or 1.
- If balanceFactor of currentNode > 1,
- If balanceFactor of leftChild >= 0, do right rotation.
- Else do left-right rotation.
- If balanceFactor of leftChild >= 0, do right rotation.
- If balanceFactor of currentNode < -1,
- If balanceFactor of rightChild <= 0, do left rotation.
- Else do right-left rotation.
- If balanceFactor of currentNode > 1,
- The final tree is:
Comments
Post a Comment