# Inserting in an AVL Tree

### Procedure :

* Use the same method used for inserting in a BST.
* Set the height of new nodes to either 1 or 0. (1 used in code below)
* Calculate the height and balance factor of each node.
* If the balance factor is not -1, 0 or 1, the tree is imbalanced.
* Perform required rotation.

```c
struct Node *insert(struct Node *ptr, int data) {

	if (ptr == NULL) {
		// Create a new node and assign required values
		struct Node *newNode;
		newNode = (struct Node *)malloc(sizeof(struct Node));
		newNode -> data = data;
		newNode -> left = newNode -> right = NULL;
		newNode -> height = 1;
		return newNode;
	}

	if (data < ptr -> data) 
		ptr -> left = insert(ptr -> left, data);
	else if (data > ptr -> data)
		ptr -> right = insert(ptr -> right, data);

	// Assign height as the max height of left subtree and right subtree
	ptr -> height = nodeHeight(ptr);

	// Check balance factor and perform rotation
	if (balanceFactor(ptr) == 2 && balanceFactor(ptr -> left) == 1) 
		return LLRotation(ptr);
	else if (balanceFactor(ptr) == 2 && balanceFactor(ptr -> left) == -1)
		return LRRotation(ptr);
	else if (balanceFactor(ptr) == -2 && balanceFactor(ptr -> right) == 1)
		return RLRotation(ptr);
	else if (balanceFactor(ptr) == -2 && balanceFactor(ptr -> right) == -1)
		return RRRotation(ptr);

	return ptr;

}
```

**Utility Functions :**

```c
int nodeHeight(struct Node *ptr) {
	
	int leftHeight, rightHeight;
	leftHeight = ptr && ptr -> left ? ptr -> left -> height : 0;
	rightHeight = ptr && ptr -> right ? ptr -> right -> height : 0;
	return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;

}

int balanceFactor(struct Node *ptr) {

	int leftHeight, rightHeight;
	leftHeight = ptr && ptr -> left ? ptr -> left -> height : 0;
	rightHeight = ptr && ptr -> right ? ptr -> right -> height : 0;
	return leftHeight - rightHeight;

}
```

Contributed by Nitin Ranganath


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://nitinranganath.gitbook.io/data-structures/avl-tree/inserting-in-an-avl-tree.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
