The Wayback Machine - https://web.archive.org/web/20241120014427/https://www.geeksforgeeks.org/practice-questions-height-balancedavl-tree/
Open In App

Practice questions on Height balanced/AVL Tree

Last Updated : 03 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

AVL tree is binary search tree with additional property that difference between height of left sub-tree and right sub-tree of any node can’t be more than 1. Here are some key points about AVL trees:

  • If there are n nodes in AVL tree, minimum height of AVL tree is floor(log 2 n).
  • If there are n nodes in AVL tree, maximum height can’t exceed 1.44*log 2 n.
  • If height of AVL tree is h, maximum number of nodes can be 2 h+1 – 1.
  • Minimum number of nodes in a tree with height h can be represented as: N(h) = N(h-1) + N(h-2) + 1 for n>2 where N(0) = 1 and N(1) = 2.
  • The complexity of searching, inserting and deletion in AVL tree is O(log n).

AVL trees, a type of height-balanced binary search tree, are critical for ensuring efficient search operations in databases and data structures. For those looking to practice AVL tree problems and improve their understanding, the GATE CS and IT – 2025 course offers a wide range of practice questions and detailed explanations that can help strengthen your problem-solving skills

We have discussed types of questions based on AVL trees. 

Type 1: Relationship between number of nodes and height of AVL tree – 

Given number of nodes, the question can be asked to find minimum and maximum height of AVL tree. Also, given the height, maximum or minimum number of nodes can be asked. 

Que – 1. What is the maximum height of any AVL-tree with 7 nodes? Assume that the height of a tree with a single node is 0. 

  • (A) 2
  • (B) 3
  • (C) 4
  • (D) 5

Solution: For finding maximum height, the nodes should be minimum at each level. Assuming height as 2, minimum number of nodes required: N(h) = N(h-1) + N(h-2) + 1 N(2) = N(1) + N(0) + 1 = 2 + 1 + 1 = 4. It means, height 2 is achieved using minimum 4 nodes. Assuming height as 3, minimum number of nodes required: N(h) = N(h-1) + N(h-2) + 1 N(3) = N(2) + N(1) + 1 = 4 + 2 + 1 = 7. It means, height 3 is achieved using minimum 7 nodes. Therefore, using 7 nodes, we can achieve maximum height as 3. Following is the AVL tree with 7 nodes and height 3. 

1

 Que – 2. What is the worst case possible height of AVL tree? 

  • (A) 2*logn
  • (B) 1.44*log n
  • (C) Depends upon implementation
  • (D) θ(n)

Solution: The worst case possible height of AVL tree with n nodes is 1.44*logn. This can be verified using AVL tree having 7 nodes and maximum height. 

1

Checking for option (A), 2*log7 = 5.6, however height of tree is 3. 

Checking for option (B), 1.44*log7 = 4, which is near to 3. 

Checking for option (D), n = 7, however height of tree is 3. 

Out of these, option (B) is the best possible answer. 

Type 2: Based on complexity of insertion, deletion and searching in AVL tree – 

Que – 3. Which of the following is TRUE? 

  • (A) The cost of searching an AVL tree is θ(log n) but that of a binary search tree is O(n)
  • (B) The cost of searching an AVL tree is θ(log n) but that of a complete binary tree is θ(n log n)
  • (C) The cost of searching a binary search tree is O(log n ) but that of an AVL tree is θ(n)
  • (D) The cost of searching an AVL tree is θ(n log n) but that of a binary search tree is O(n)

Solution: AVL tree’s time complexity of searching, insertion and deletion = O(logn). But a binary search tree, may be skewed tree, so in worst case BST searching, insertion and deletion complexity = O(n). 

Que – 4. The worst case running time to search for an element in a balanced in a binary search tree with n*2^n elements is 

3

 Solution: Time taken to search an element is Θ(logn) where n is number of elements in AVL tree. As number of elements given is n*2^n, the searching complexity will be Θ(log(n*2^n)) which can be written as:

= Θ(log(n*2^n))
= Θ(log(n)) + Θ(log(2^n))
= Θ(log(n)) + Θ(nlog(2))
= Θ(log(n)) + Θ(n)

As logn is asymptotically smaller than n, Θ(log(n)) + Θ(n) can be written as Θ(n) which matches option C. 

Type 3: Insertion and Deletion in AVL tree – The question can be asked on the resultant tree when keys are inserted or deleted from AVL tree. Appropriate rotations need to be made if balance factor is disturbed. 

Que – 5. Consider the following AVL tree. 

2

 Which of the following is updated AVL tree after insertion of 70? 

  • (A)

3

  • (B)

4

  • (C)

5

  • (D) None

Solution: The element is first inserted in the same way as BST. Therefore after insertion of 70, BST can be shown as: 

6

 However, balance factor is disturbed requiring RL rotation. To remove RL rotation, it is first converted into RR rotation as: 

7

 After removal of RR rotation, AVL tree generated is same as option (C).


Previous Article
Next Article

Similar Reads

Maximum height of an elevation possible such that adjacent matrix cells have a difference of at most height 1
Given a matrix mat][][] of size M x N which represents the topographic map of a region, and 0 denotes land and 1 denotes elevation, the task is to maximize the height in the matrix by assigning each cell a non-negative height such that the height of a land cell is 0 and two adjacent cells must have an absolute height difference of at most 1. Exampl
12 min read
Check if a given Binary Tree is height balanced like a Red-Black Tree
In a Red-Black Tree, the maximum height of a node is at most twice the minimum height (The four Red-Black tree properties make sure this is always followed). Given a Binary Search Tree, we need to check for following property. For every node, length of the longest leaf to node path has not more than twice the nodes on shortest path from node to lea
9 min read
Comparison between Height Balanced Tree and Weight Balanced Tree
What is Height Balanced Tree? Self-Balancing binary search trees are the height-balanced binary tree is one for which at every node, the absolute value of the difference in heights of the left and right subtree is no larger than one. An empty tree is height-balanced. A non-empty binary tree T is balanced if: The left subtree of T is balanced.The ri
4 min read
Iterative Method to find Height of Binary Tree
There are two conventions to define the height of a Binary Tree Number of nodes on the longest path from the root to the deepest node. Number of edges on the longest path from the root to the deepest node. In this post, the first convention is followed. For example, the height of the below tree is 3. The recursive method to find the height of the B
8 min read
Possible edges of a tree for given diameter, height and vertices
Find a tree with the given values and print the edges of the tree. Print "-1", if the tree is not possible. Given three integers n, d and h. n -> Number of vertices. [1, n] d -> Diameter of the tree (largest distance between two vertices). h -> Height of the tree (longest distance between vertex 1 and another vertex) Examples : Input : n =
8 min read
Height of binary tree considering even level leaves only
Find the height of the binary tree given that only the nodes on the even levels are considered valid leaf nodes. The height of a binary tree is the number of edges between the tree's root and its furthest leaf. But what if we bring a twist and change the definition of a leaf node? Let us define a valid leaf node as the node that has no children and
8 min read
Maximum height of Tree when any Node can be considered as Root
Given a tree with N nodes and N-1 edges, find out the maximum height of the tree when any node in the tree is considered as the root of the tree. The above diagram represents a tree with 11 nodes and 10 edges and the path that gives us the maximum height when node 1 is considered as a root. The maximum height is 3. In the above diagram, when 2 is c
15 min read
Traversal of tree with k jumps allowed between nodes of same height
There is a tree with N nodes and node 1 is the root node. Each node of the tree can contain fruit or not. Initially, you are at the root node and start climbing on the tree. You can jump from a node to any node at the same level(i.e. the height of nodes from the root are same), During climbing from root node you can only make maximum K jumps. (K
12 min read
Minimum number of nodes in an AVL Tree with given height
Given the height of an AVL tree 'h', the task is to find the minimum number of nodes the tree can have. Examples : Input : H = 0 Output : N = 1 Only '1' node is possible if the height of the tree is '0' which is the root node. Input : H = 3 Output : N = 7 Recursive Approach : In an AVL tree, we have to maintain the height balance property, i.e. dif
7 min read
Print the nodes of the Binary Tree whose height is a Prime number
Given a binary tree, our task is to print the nodes whose height is a prime number starting from the root node. Examples: Input: 1 / \ 2 3 / \ 4 5 Output: 4 5 Explanation: For this tree: Height of Node 1 - 0, Height of Node 2 - 1, Height of Node 3 - 1, Height of Node 4 - 2, Height of Node 5 - 2. Hence, the nodes whose height is a prime number are 4
8 min read
Calculate height of Binary Tree using Inorder and Level Order Traversal
Given inorder traversal and Level Order traversal of a Binary Tree. The task is to calculate the height of the tree without constructing it. Example: Input : Input: Two arrays that represent Inorder and level order traversals of a Binary Tree in[] = {4, 8, 10, 12, 14, 20, 22}; level[] = {20, 8, 22, 4, 12, 10, 14}; Output : 4 The binary tree that ca
12 min read
Maximum height of the binary search tree created from the given array
Given an array arr[] of N integers, the task is to make two binary search trees. One while traversing from the left side of the array and another while traversing from the right and find which tree has a greater height.Examples: Input: arr[] = {2, 1, 3, 4} Output: 0 BST starting from first index BST starting from last index 2 4 / \ / 1 3 3 \ / 4 1
10 min read
Height of Factor Tree for a given number
Given a positive integer N, the task is to find the height of the Factor Tree of the given integer N. Examples: Input: N = 20Output: 3Explanation: The height of the Factor Tree of 20 shown in the image below is 3. Input: N = 48Output: 5 Approach: The given problem can be solved by using the steps discussed below: Initialize a variable, say height a
5 min read
Number of leaf nodes in a perfect N-ary tree of height K
Find the number of leaf nodes in a perfect N-ary tree of height K. Note: As the answer can be very large, return the answer modulo 109+7. Examples: Input: N = 2, K = 2Output: 4Explanation: A perfect Binary tree of height 2 has 4 leaf nodes. Input: N = 2, K = 1Output: 2Explanation: A perfect Binary tree of height 1 has 2 leaf nodes. Approach: This p
4 min read
Introduction to Height Balanced Binary Tree
A height-balanced binary tree is defined as a binary tree in which the height of the left and the right subtree of any node differ by not more than 1. AVL tree, red-black tree are examples of height-balanced trees. Conditions for Height-Balanced Binary Tree: Following are the conditions for a height-balanced binary tree: The difference between the
5 min read
Construct a Perfect Binary Tree with given Height
Given an integer N, the task is to generate a perfect binary tree with height N such that each node has a value that is the same as its depth. Return the inorder traversal of the generated binary tree. A Perfect binary tree is a type of binary tree where every internal node has exactly two child nodes and all leaf nodes are at same level. Examples:
9 min read
Find Height of Binary Tree represented by Parent array
A given array represents a tree in such a way that the array value gives the parent node of that particular index. The value of the root node index would always be -1. Find the height of the tree. The height of a Binary Tree is the number of nodes on the path from the root to the deepest leaf node, and the number includes both root and leaf. Input:
13 min read
Height of a complete binary tree (or Heap) with N nodes
Consider a Binary Heap of size N. We need to find the height of it. Examples: Input : N = 6 Output : 2 () / \ () () / \ / () () () Input : N = 9 Output : 3 () / \ () () / \ / \ () () () () / \ () ()Recommended PracticeHeight of HeapTry It! Let the size of the heap be N and the height be h. If we take a few examples, we can notice that the value of
3 min read
Find height of a special binary tree whose leaf nodes are connected
Given a special binary tree whose leaf nodes are connected to form a circular doubly linked list, find its height. For example, 1 / \ 2 3 / \ 4 5 / 6 In the above binary tree, 6, 5 and 3 are leaf nodes and they form a circular doubly linked list. Here, the left pointer of leaf node will act as a previous pointer of circular doubly linked list and i
9 min read
Roots of a tree which give minimum height
Given an undirected graph, which has tree characteristics. It is possible to choose any node as root, the task is to find those nodes only which minimize the height of tree. Example: In below diagram all node are made as root one by one, we can see that when 3 and 4 are root, height of tree is minimum(2) so {3, 4} is our answer. We can solve this p
10 min read
Sqrt (or Square Root) Decomposition | Set 2 (LCA of Tree in O(sqrt(height)) time)
Prerequisite: Introduction and DFSThe task is to find LCA of two given nodes in a tree (not necessarily a Binary Tree). In previous posts, we have seen how to calculate LCA using the Sparse Matrix DP approach. In this post, we will see an optimization done on the Naive method by sqrt decomposition technique that works well over the Naive Approach.
15+ min read
Print middle level of perfect binary tree without finding height
Given a perfect binary tree, print nodes of middle level without computing its height. A perfect binary tree is a binary tree in which all interior nodes have two children and all leaves have the same depth or same level. Output : 4 5 6 7 The idea is similar to method 2 of finding middle of singly linked list.Use fast and slow (or tortoise) pointer
7 min read
Relationship between number of nodes and height of binary tree
Prerequisite - Binary Tree Data Structure In this article, we will discuss various cases for relationship between number of nodes and height of binary tree. Before understanding this article, you should have basic idea about binary trees and their properties. The height of the binary tree is the longest path from root node to any leaf node in the t
3 min read
Height of n-ary tree if parent array is given
Given a parent array P, where P[i] indicates the parent of the ith node in the tree(assume parent of root node id indicated with -1). Find the height of the tree. Examples: Input : array[] = [-1 0 1 6 6 0 0 2 7]Output : height = 5Tree formed is: 0 / | \ 5 1 6 / | \ 2 4 3 / 7 / 8 Start at each node and keep going to its parent until we reach -1. Als
10 min read
Height and Depth of a node in a Binary Tree
Given a Binary Tree consisting of N nodes and a integer K, the task is to find the depth and height of the node with value K in the Binary Tree. The depth of a node is the number of edges present in path from the root node of a tree to that node.The height of a node is the number of edges present in the longest path connecting that node to a leaf n
15+ min read
Maximum Depth or Height of Binary Tree
Given a binary tree, the task is to find the maximum depth or height of the tree. The height of the tree is the number of vertices in the tree from the root to the deepest node. Examples: Example 1: The height of the below binary tree is 3. Example 2: The height of the below binary tree is 4 Table of Content [Expected Approach - 1] Using Recursion
15+ min read
Height of a generic tree from parent array
Given a tree of size n as array parent[0..n-1] where every index i in the parent[] represents a node and the value at i represents the immediate parent of that node. For root, the node value will be -1. Find the height of the generic tree given the parent links. Examples: Input: parent[] = [-1, 0, 0, 0, 3, 1, 1, 2]Output : 2 Input: parent[] = [-1,
13 min read
Practice questions for Linked List and Recursion
Assume the structure of a Linked List node is as follows. C/C++ Code struct Node { int data; struct Node *next; }; // This code is contributed by SHUBHAMSINGH10 C/C++ Code struct Node { int data; struct Node *next; }; Java Code static class Node { int data; Node next; }; // This code is contributed by shubhamsingh10 C/C++ Code class Node: def __ini
11 min read
Practice Questions for Recursion | Set 1
Explain the functionality of the following functions. Question 1 C/C++ Code int fun1(int x, int y) { if (x == 0) return y; else return fun1(x - 1, x + y); } C/C++ Code int fun1(int x, int y) { if (x == 0) return y; else return fun1(x - 1, x + y); } Java Code static int fun1(int x, int y) { if (x == 0) return y; else return fun1(x - 1, x + y); } C/C
5 min read
Practice Questions for Recursion | Set 2
Explain the functionality of the following functions. Question 1 C/C++ Code /* Assume that n is greater than or equal to 1 */ int fun1(int n) { if (n == 1) return 0; else return 1 + fun1(n / 2); } Java Code /* Assume that n is greater than or equal to 1 */ static int fun1(int n) { if (n == 1) return 0; else return 1 + fun1(n / 2); } C/C++ Code # As
3 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg