Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA
  • Interview Problems on Tree
  • Practice Tree
  • MCQs on Tree
  • Tutorial on Tree
  • Types of Trees
  • Basic operations
  • Tree Traversal
  • Binary Tree
  • Complete Binary Tree
  • Ternary Tree
  • Binary Search Tree
  • Red-Black Tree
  • AVL Tree
  • Full Binary Tree
  • B-Tree
  • Advantages & Disadvantages
Open In App
Next Article:
AVL with duplicate keys
Next article icon

Practice questions on Height balanced/AVL Tree

Last Updated : 28 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

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).


Next Article
AVL with duplicate keys
https://media.geeksforgeeks.org/auth/avatar.png
GeeksforGeeks
Improve
Article Tags :
  • Misc
  • Tree
  • GATE CS
  • DSA
  • MCQ
Practice Tags :
  • Misc
  • Tree

Similar Reads

    AVL Tree Data Structure
    An AVL tree defined as a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees for any node cannot be more than one. Balance Factor = left subtree height - right subtree heightFor a Balanced Tree(for every node): -1 ≤ Balance Factor ≤ 1Example of an
    5 min read
    What is AVL Tree | AVL Tree meaning
    An AVL is a self-balancing Binary Search Tree (BST) where the difference between the heights of left and right subtrees of any node cannot be more than one. KEY POINTSIt is height balanced treeIt is a binary search treeIt is a binary tree in which the height difference between the left subtree and r
    2 min read
    Insertion in an AVL Tree
    AVL tree is a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees cannot be more than one for all nodes. Insertion in an AVL Tree follows the same basic rules as in a Binary Search Tree (BST):A new key is placed in its correct position based on BST
    15+ min read
    Insertion, Searching and Deletion in AVL trees containing a parent node pointer
    AVL tree is a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees cannot be more than one for all nodes. The insertion and deletion in AVL trees have been discussed in the previous article. In this article, insert, search, and delete operations are
    15+ min read
    Deletion in an AVL Tree
    We have discussed Insertion of AVL Tree. In this post, we will follow a similar approach for deletion.Steps to follow for deletion. To make sure that the given tree remains AVL after every deletion, we must augment the standard BST delete operation to perform some re-balancing. Following are two bas
    15+ min read
    How is an AVL tree different from a B-tree?
    AVL Trees: AVL tree is a self-balancing binary search tree in which each node maintain an extra factor which is called balance factor whose value is either -1, 0 or 1. B-Tree: A B-tree is a self - balancing tree data structure that keeps data sorted and allows searches, insertions, and deletions in
    1 min read
    Practice questions on Height balanced/AVL Tree
    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 i
    4 min read
    AVL with duplicate keys
    Please refer below post before reading about AVL tree handling of duplicates. How to handle duplicates in Binary Search Tree?This is to augment AVL tree node to store count together with regular fields like key, left and right pointers. Insertion of keys 12, 10, 20, 9, 11, 10, 12, 12 in an empty Bin
    15+ min read
    Count greater nodes in AVL tree
    In this article we will see that how to calculate number of elements which are greater than given value in AVL tree. Examples: Input : x = 5 Root of below AVL tree 9 / \ 1 10 / \ \ 0 5 11 / / \ -1 2 6 Output : 4 Explanation: there are 4 values which are greater than 5 in AVL tree which are 6, 9, 10
    15+ min read
    Difference between Binary Search Tree and AVL Tree
    Binary Search Tree:A binary Search Tree is a node-based binary tree data structure that has the following properties: The left subtree of a node contains only nodes with keys lesser than the node’s key.The right subtree of a node contains only nodes with keys greater than the node’s key.The left and
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences