×

AVL tree in data structure c++

AVL tree is generally known as the self-sustained and most balanced tree in the field of a binary search tree. It was also widely known as the height-balanced binary tree. It was mostly invented by Gm Adelson in the year 1992, as you can very well see that the tree is named after its inventors as an act of honor. The AVL tree is said to be well-coordinated or well-balanced if both factors of the tree lie between 1 and -1.

Why do we use AVL trees?

We are quite familiar with the complexities of the binary search tree, So in a binary search tree, nearly all the operations such as searching, traversing, deletion, and several others have O(h) complexity where we all know that O is known as the big O, and h stands for the height of the binary search tree. Sometimes, the price of the operations may become O(n) for any tree, especially for a skewed binary tree, and so to overcome that, we have to keep in mind that the height of the tree we are using remains the O (log n) whenever we perform one of the operations such as insertion or deletion.

In that case, we can definitely switch for an upper bound that will have the complexity of O(logn) for several other operations as well. An important key note to keep in mind is that the height of an AVL tree is always O (logn), where we know that O is the big O and n stands for the total number of nodes present in that tree.

Balance Factor of an AVL tree

While studying the AVL tree, we often encounter the term balance factor. We will now understand its basic meaning. Well, the balance factor is nothing but the calculated difference between the height of the left sub-tree and the right sub-tree of a node.

The formula for the balance factor can be written as: -

Balance factor = (height of the left subtree – height of the right subtree).

The self-sustained and well-organized or balancing quality that we discussed previously on the AVL tree is usually maintained by the balance factor, which implies that the value should always lie between – 1 to + 1.

Rotations

We know that sometimes while performing some operations in the tree, it becomes unorganized or imbalanced. So, in order to fix that, we have to perform rotations to transfer and return the balance of the tree. There are majorly four kinds of rotation. They are: -

  • RR rotation
    When we insert a node in the right sub-tree and that too as the right child, then it is known as RR rotation.
  • LL rotation
    When we insert a node in the left sub-tree and that too as the left child, then it is known as LL rotation.
  • RL rotation
    When we insert a node in the left sub-tree and that too as the right child, then it is known as RL rotation.
  • LR rotation
    When we insert a node in the right sub-tree and that too as the left child, then it is known as LR rotation.

Implementation

// AVL tree implementation in C++

In this article, we are mainly going to see the implementation of the AVL tree and its operations in the C++ language and observe the output as well.

#include <iostream>
using namespace std;


class node {
   public:
  int key;
  Node *left;
  Node *right;
  int height;
};


int maximum(int x, int y);


// we will calculate the height now: -
int height(node *N) {
  if (N == NULL)
    return 0;
  return N->height;
}


int maximum(int x, int y) {
  return (x > y) ? x : y;
}


// We will create a new node now: -
Node *newnode(int key) {
  node *node = new node();
  (*node).key = key;
  (*node).left = NULL;
  (*node).right = NULL;
  (*node).height = 1;
  return (node);
}


// we will now rotate in the right position
node *rightRotate(node *y) {
  node *x = y->left;
  node *T2 = x->right;
  (*x).right = y;
  (*y).left = T2;
  (*y).height = maximum(height(y->left),
          height(y->right)) +
        1;
  x->height = maximum(height(x->left),
          height(x->right)) +
        1;
  return x;
}


// we will now rotate in the left position
node *leftRotate(node *x) {
  node *y = (*x).right;
  node *T2 = (*y).left;
  (*y).left = x;
  (*x).right = T2;
  (*x).height = maximum(height(x->left),
          height(x->right)) +
        1;
  (*y).height = maximum(height(y->left),
          height(y->right)) +
        1;
  return y;
}


// Next we will be calculating the balance factor of each node
int getBalanceFactor(node *N) {
  if (N == NULL)
    return 0;
  return height(N->left) -
       height(N->right);
}


// We will be inserting a node
node *insertnode(node *node, int key) {
  // Now we have to search for the correct postion and insert the node
  if (node == NULL)
    return (newnode(key));
  if (key < node->key)
    (*node).left = insertnode(*node).left, key);
  else if (key > node->key)
    (*node).right = insertnode(*node).right, key);
  else
    return node;


  // We have to change the balance factor of each node and
  (*node).height = 1 + maximum(height(*node).left),
               height(*node).right));
  int balanceFactor = getBalanceFactor(node);
  if (balanceFactor > 1) {
    if (key < node->left->key) {
      return rightRotate(node);
    } else if (key > node->left->key) {
      node->left = leftRotate(node->left);
      return rightRotate(node);
    }
  }
  if (balanceFactor < -1) {
    if (key > node->right->key) {
      return leftRotate(node);
    } else if (key < node->right->key) {
      node->right = rightRotate(node->right);
      return leftRotate(node);
    }
  }
  return node;
}


// examining the node with the lesser value
node *nodeWithMinimumValue(Node *node) {
  node *curr = node;
  while (*curr).left != NULL)
    current = current->left;
  return current;
}


// removing a specific node
node *deletenode(node *root, int key) {
  if (root == NULL)
    return root;
  if (key < root->key)
    root->left = deleteNode(root->left, key);
  else if (key > root->key)
    root->right = deleteNode(root->right, key);
  else {
    if ((root->left == NULL) ||
      (root->right == NULL)) {
      Node *temp = (*root).left ? root->left : (*root).right;
      if (temp == NULL) {
        temp = root;
        root = NULL;
      } else
        *root = *temp;
      free(temp);
    } else {
      node *temp = nodeWithMimumValue(root->right);
      root->key = temp->key;
      root->right = deletenode(root->right,
                   temp->key);
    }
  }


  if (root == NULL)
    return root;


  //Change the balance factor of each given node


  (*Root).height = 1 + maximum(height(root->left),
               height(*root).right));
  int balanceFactor = getBalanceFactor(root);
  if (balanceFactor > 1) {
    if (getBalanceFactor(root->left) >= 0) {
      return rightRotate(root);
    } else {
      root->left = leftRotate(root->left);
      return rightRotate(root);
    }
  }
  if (balanceFactor < -1) {
    if (getBalanceFactor(root->right) <= 0) {
      return leftRotate(root);
    } else {
      root->right = rightRotate(root->right);
      return leftRotate(root);
    }
  }
  return root;
}


// Final step is to print the constructed tree
void printTree(node *root, bool last) {
  if (root != nullp) {
    cout << indent;
    if (last) {
      cout << "R----";
      indent += "   ";
    } else {
      cout << "L----";
      indent += "|  ";
    }
    cout << root->key << endl;
    printTree(root->left, false);
    printTree(root->right, true);
  }
}


int main() {
*rt = root;
  node *rt = NULL;
  root = insertnode(33);
  root = insertnode(13);
  root = insertnode(53);
  root = insertnode(9);
  root = insertnode(21);
  root = insertnode(61);
  root = insertnode(8);
  root = insertnode(11);
  printTree(root, "", true);
  root = deleteNode(root, 13);
  cout << "After deleting " << endl;
  printTree(root, T);
}

Output:

Difference Between Stack And Queue

Complexity

In this part of the discussion, we are going to witness the complexity of the various operations in the AVL tree. Let us see in the table given below: -

CaseTime
Best caseO(log n)
Average caseO(log n)
Worst caseO(log n)

1. BEST CASE

In the AVL tree, the best scenario happens when we don’t require any sort of balancing or rotation. It means that we can traverse or insert the node which we want to without having to make any sort of amendments in the model of the AVL tree. The complexity of the AVL tree in the best scenario is O (log n).

2. AVERAGE CASE

The average case usually occurs when we have to simply take out the mean of all the possible cases based on the number of elements present in the tree. Suppose, if there are n number of elements present in the tree, then the complexity, in this case, is O (log n).

3.WORST CASE

The worst-case complexity usually occurs when we want to insert or traverse to a new node and when we do that, the tree becomes unorganized and imbalanced. Then to fix that, we have to perform some rotations, which will ultimately cost us a lot of time because of the traversal. So, the time complexity of the AVL tree in the worst scenario turns out to be O(log n).

SPACE COMPLEXITY

Space complexity is generally described as the amount of memory a program takes at the time of execution, So the space complexity of the AVL tree is O(n).


Related Topics

Sorting Algorithms

Sorting: In the data structure, sorting is the process by which you arrange the data in a logical order. This logical order can also be an ascending order or a...

7 minutes read.

Binary Search Tree

Binary Search Tree: A binary search tree is a type of tree in which every node is organized in the sorted order. It is also called an ordered binary tree. Properties...

4 minutes read.

Deque in Data Structure

Deque A deque referred as “Double-Ended Queue”, is a linear collection of data items same like queue data structure. deque has two ends, front end and rear end, deque is the...

27 minutes read.

Reverse a Linked List in groups of given size

Reverse a Linked List in groups of given size This article will explain how to reverse a linked list in groups of given size. Here we have given a linked list...

2 minutes read.

Optimal binary search tree in DSA

Implementation // A simple way of the recursive implementation of the optimal search that we will perform on the binary tree.   #include <bits/stdc++.h> using namespace std; // we have to create a basic utility...

8 minutes read.

Operations on Queue in Data Structures

A queue is a linear structure where operations are done in a specific sequence. Queues are abstract data structures that are comparable to Stacks. A queue, unlike a stack, is...

8 minutes read.

Box Stacking Problem

Stacking of boxes depending on their base You have been given n different boxes. These boxes will have different heights, widths, and depths. You have to stack all these boxes in...

4 minutes read.

Insertion Sort in Data Structures

Insertion Sort in C++ Insertion sort is a sorting algorithm that, in each iteration, installs an unsorted element in its proper position Insertion sort operates in a similar way to how we...

3 minutes read.

Rearrange a linked list into alternate fashion first and the last element

Rearrange a linked list into alternate fashion first and the last element This article will explain how to rearrange the linked list into alternate fashion first and the last element. Here,...

3 minutes read.

Identical Linked Lists

Identical Linked Lists In this problem, we have given two linked lists, and we need to check whether the given linked lists are identical or not. Identical means they have the...

4 minutes read.

Collision Resolution Techniques

Collision Resolution Techniques Collision in hashing In this, the hash function is used to compute the index of the array.The hash value is used to store the key in the hash table,...

2 minutes read.

Queue Data Structure

Queue in DS: The queue is a non-primitive and linear data structure. It works on the principle of FIFO (First In First Out). That is, the element that is added...

4 minutes read.

Priority Queue in Data Structure

Priority Queue A priority queue is a special kind of queue, in priority queue we give some priority to an element and according to this priority an element can be served...

3 minutes read.

Given a Binary Tree Print the Shortest Path

Implementation // Writing a program in C++ to find the shortest between the nodes i and j.  #include <bits/stdc++.h> using namespace std; // the given function will print the path between nodes i and...

7 minutes read.

FLEX (Fast Lexical Analyzer Generator)

FLEX stands for Fast Lexical Analyzer Generator. Around 1987, Vern Paxson created Flex in C with a great deal of input and inspiration from Van Jacobson. Van Jacobson's approach is...

3 minutes read.

Bubble Sort in Data Structures

Bubble Sort in C++ The bubble sort algorithm analyses two adjacent elements and swaps them until they are no longer in the desired order. Each iteration moves each member of the array...

4 minutes read.

B Tree vs B + Tree: Data Structure

Difference Between B Tree and B+ Tree What is B Tree? B-Tree is a self-balancing or special type of m-way tree. B-Trees are used mainly in disc access. If we want...

3 minutes read.

Heap Sort vs Merge Sort

In this article, we are going to discuss the Heap Sort, Merge sort and the difference between them. What is Heap Sort? Heap – A heap is an abstract data type categorised...

7 minutes read.

Hashing

Hashing: Hashing is a process in which a large amount of data is mapped to a small table with the help of hashing function. It is a searching technique. Hash table We...

4 minutes read.

Operations of B Tree in C++ Language

B tree tends to be a self-aligning and balancing tree that helps us organise our data and document safely. We know that every data or information in the B tree...

9 minutes read.