🗂️
Data Structures
  • Data Structures Manual
  • Arrays
    • Array ADT
    • Linear Search
    • Binary Search
    • Some More Basic Operations
    • Reversing an Array
    • Operations in a Sorted Array
    • Merging Two Arrays
    • Set Operations
    • Finding Missing Elements
    • Duplicates in an Array
    • Getting a Pair whose Sum = K
    • Finding Max & Min in Single Scan
  • Strings
    • Finding the Length of a String
    • Changing Cases in a String
    • Finding Number of Vowels, Consonants & Words
    • Reversing a String
    • Checking for Palindrome
    • Duplicates in a String
    • Checking if Strings are Anagrams
    • Permutations of a String
  • Singly Linked List
    • Displaying the Nodes
    • Counting the Nodes
    • Sum of all Nodes
    • Finding the Maximum Element
    • Searching in a Node
    • Inserting a Node
    • Inserting a Node in Sorted List
    • Deleting a Node
    • Checking if List is Sorted
    • Removing Duplicates from a List
    • Reversing a Linked List
    • Concatenating Two Lists
    • Detecting a Loop in Linked List
    • Merge Two Sorted Lists
    • Finding the Middle Node
  • Cirular Linked List
    • Displaying the Nodes
    • Inserting a Node
    • Deleting a Node
  • Doubly Linked List
    • Inserting a Node
    • Deleting a Node
    • Reversing a Doubly Linked List
    • Circular Doubly Linked List
  • Stack
    • Stack Using Array
    • Stack Using Linked List
    • Balancing Parenthesis
    • Infix to Postfix
    • Evaluation of Postfix Expression
  • Queue
    • Queue using Array
    • Queue using Linked List
    • Double Ended Queue
  • Binary Tree
    • Creating a Binary Tree using Queue
    • Recursive Tree Traversals
    • Iterative Tree Traversals
    • Level Order Traversal
    • Counting Nodes in a Binary Tree
    • Finding the Height of Tree
    • Finding Sum of All Nodes
  • Binary Search Tree
    • Searching in a BST
    • Inserting in a BST
    • Deleting in a BST
  • AVL Tree
    • Inserting in an AVL Tree
    • AVL Tree Rotations
    • Deleting in an AVL Tree
  • Heap
    • Inserting in a Heap
    • Deleting in a Heap
    • Heapify
Powered by GitBook
On this page

Was this helpful?

  1. AVL Tree

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.

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 :

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

PreviousDeleting in a BSTNextAVL Tree Rotations

Last updated 4 years ago

Was this helpful?