Posted on by Kalkicode
Code Tree

# AVL Tree Insertion

The problem of AVL Tree Insertion involves adding elements to an AVL (Adelson-Velsky and Landis) tree while maintaining the AVL property, which ensures that the tree remains balanced. AVL trees are binary search trees in which the height difference between the left and right subtrees of any node is at most one. When a new element is inserted, the tree is rebalanced using rotation operations.

## Problem Statement

Given a set of elements, we need to insert them into an AVL tree while maintaining the AVL property and then display the tree in preorder, inorder, and postorder traversal.

## Example

Suppose we have the following set of elements to insert into an AVL tree: 4, 7, 5, 19, 17, 13, 11, 3, 2, and -3. After inserting these elements and performing the necessary rotations to maintain balance, the resultant AVL tree would look like this:

``````
7
/ \
4   17
/ \  / \
2   5 13 19
/     \
-3      11``````

## Idea to Solve

To solve this problem, we need to follow these steps:

1. Create a structure for the AVL tree node with data, height, left, and right pointers.
2. Implement functions to calculate height, perform rotations (right and left), and get balance factor.
3. Implement a function to add a node to the AVL tree recursively. During insertion, update the height of nodes and perform rotations when necessary to maintain the AVL property.
4. After inserting all elements, print the tree in preorder, inorder, and postorder traversal.

Here mentioning program which is added nodes using of proper rotation. See Rotion Animation.

## Algorithm

1. Create a structure `Node` with `data`, `height`, `left`, and `right` pointers.
2. Implement `height(node)` to get the height of a node.
3. Implement `max_height(a, b)` to get the maximum of two heights.
4. Implement `new_node(data)` to create a new node and initialize its values.
5. Implement `right_rotate(node)` and `left_rotate(node)` functions to perform right and left rotations.
6. Implement `get_balance_factor(node)` to get the balance factor of a node.
7. Implement `add_node(node, data)` to add a node recursively, performing rotations to maintain balance.
8. Implement `preorder(root)`, `inorder(root)`, and `postorder(root)` functions for traversal.
9. In the `main()` function, create an AVL tree and insert elements one by one using `add_node()`.
10. Print the AVL tree using preorder, inorder, and postorder traversals.

## Time Complexity

The time complexity of AVL tree insertion and rotations is O(log n), where n is the number of nodes in the tree. This is because rotations and height adjustments are performed only on the path from the newly inserted node to the root, and this path length is limited by the height of the tree.

## Resultant Output Explanation

The output displays the AVL tree after inserting the given elements and performing necessary rotations to maintain the balance. The tree is printed in preorder, inorder, and postorder traversal, illustrating the final structure of the AVL tree and confirming that it maintains the AVL property.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post