0% found this document useful (0 votes)
19 views

Introduction to Height Balanced Binary Tree - GeeksforGeeks

A height-balanced binary tree is defined as a binary tree where the height difference between the left and right subtrees of any node is at most one. Examples include AVL trees and red-black trees, which ensure efficient search operations with a time complexity of O(logN). The document also discusses the conditions for height balance, advantages and disadvantages of height-balanced trees, and methods to check if a tree is height-balanced.

Uploaded by

Bhagya Lakshmi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Introduction to Height Balanced Binary Tree - GeeksforGeeks

A height-balanced binary tree is defined as a binary tree where the height difference between the left and right subtrees of any node is at most one. Examples include AVL trees and red-black trees, which ensure efficient search operations with a time complexity of O(logN). The document also discusses the conditions for height balance, advantages and disadvantages of height-balanced trees, and methods to check if a tree is height-balanced.

Uploaded by

Bhagya Lakshmi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

2/14/25, 10:57 AM Introduction to Height Balanced Binary Tree - GeeksforGeeks

Introduction to Height Balanced Binary Tree


Last Updated : 03 Apr, 2023

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.

Height Balanced tree

Conditions for Height-Balanced Binary Tree:


Following are the conditions for a height-balanced binary tree:

The difference between the heights of the left and the right subtree
for any node is not more than one.
The left subtree is balanced.
The right subtree is balanced.

Note: An empty tree is also height-balanced.

What is the Height Balance of a node?

https://www.geeksforgeeks.org/introduction-to-height-balanced-binary-tree/?ref=asr10 1/10
2/14/25, 10:57 AM Introduction to Height Balanced Binary Tree - GeeksforGeeks

To check if the binary tree is height-balanced or not, you have to check


the height balance of each node. For this, you need to calculate the
heights of the two subtrees for each node making this impractical.
Instead, we store the height balance information of every subtree in the
DSAroot
Coursenode
DSAof that
Data Structures Array each
subtree. Thus, String node
Linked Listonly
not Stack Queue its
maintains Tree
dataBinary Tree
and children’s information but also a height balance value.
The height balance of a node is calculated as follows:

height balance of node = height of right subtree – height of left


subtree

The above formula means that:

If the right subtree is taller, the height balance of the node will be
positive.
If the left subtree is taller, the balance of the node will be negative.

Height-balanced and Unbalanced binary tree

Height of a Height-Balanced Binary Tree:


The height of a node in a tree is the length of the longest path from that
node downward to a leaf, counting both the start and end vertices of
the path. The height of a leaf is 1. The height of a nonempty tree is the

https://www.geeksforgeeks.org/introduction-to-height-balanced-binary-tree/?ref=asr10 2/10
2/14/25, 10:57 AM Introduction to Height Balanced Binary Tree - GeeksforGeeks

height of its root. It can be proved that the height of a height-balanced


binary tree with N nodes is O(logN).

Proof:

The minimum number of nodes in a height-balanced binary tree of


height h is greater than 2h/2-1 nodes and let this is denoted by the
function f(h), i.e. f(h) > 2h/2-1

This can be proved using mathematical induction.

A height-balanced binary tree of height 1 has at least 2 node.


So f(1) = 2 > 21/2 – 1 .
A height-balanced binary tree of height 2 has a minimum of 4
nodes i.e., the root node, its two children and at least one node
at depth of 2. So f(2) = 4 > 22/2 – 1.
Now consider the statement is true for some value of height (h-
1) > 2. So we have to prove that it is also valid for height = h.
A tree of height h, has a root and its two subtrees. One of the
subtrees will have height h-2 and the other h-1 (because we
want the minimum number of nodes). So,
f(h) = 1 + f(h-1) + f(h-2)
f(h) > 1 + 2 * f(h-2) because f(h-1) > f(h-2)
f(h) > 2 * f(h-2).
As the statement is true for values less than h,
So f(h) > 2*2(h-2)/2 – 1
i.e., f(h) > 2h/2-1.
So we can see
log( f(h) ) > h/2 – 1 or
h/2 < log( f(h) ) + 1
h < 2*log( f(h) ) + 2
h < 2*log(N) + 2 [say the minimum number of nodes is N. So
f(h) = N]

Therefore it is proved that h = O(logN)

https://www.geeksforgeeks.org/introduction-to-height-balanced-binary-tree/?ref=asr10 3/10
2/14/25, 10:57 AM Introduction to Height Balanced Binary Tree - GeeksforGeeks

Why do we need a Height-Balanced Binary Tree?


Let’s understand the need for a balanced binary tree through an
example.

Binary search tree

The above tree is a binary search tree and also a height-balanced tree.
Suppose we want to find the value 79 in the above tree. First, we
compare the value of the root node. Since the value of 79 is greater than
35, we move to its right child, i.e., 48. Since the value 79 is greater than
48, so we move to the right child of 48. The value of the right child of
node 48 is 79. The number of hops required to search the element 79 is
2.
Similarly, any element can be found with at most 2 jumps because the
height of the tree is 2.

So it can be seen that any value in a balanced binary tree can be


searched in O(logN) time where N is the number of nodes in the tree.
But if the tree is not height-balanced then in the worst case, a search
operation can take O(N) time.

Applications of Height-Balanced Binary Tree:


Balanced trees are mostly used for in-memory sorts of sets and
dictionaries.
Balanced trees are also used extensively in database applications in
which insertions and deletions are fewer but there are frequent
lookups for data required.

https://www.geeksforgeeks.org/introduction-to-height-balanced-binary-tree/?ref=asr10 4/10
2/14/25, 10:57 AM Introduction to Height Balanced Binary Tree - GeeksforGeeks

It is used in applications that require improved searching apart from


database applications.
It has applications in storyline games as well.
It is used mainly in corporate sectors where they have to keep the
information about the employees working there and their change in
shifts.

Advantages of Height-Balanced Binary Tree:


It will improve the worst-case lookup time at the expense of making
a typical case roughly one lookup less.
As a general rule, a height-balanced tree would work better when
the request frequencies across the data set are more evenly spread,
It gives better search time complexity.

Disadvantages of Height-Balanced Binary Tree:


Longer running times for the insert and remove operations.
Must keep balancing info in each node.
To find nodes to balance, must go back up in the tree.

How to check if a given tree is height-balanced:


You can check if a tree is height-balanced using recursion based on the
idea that every subtree of the tree will also be height-balanced. To
check if a tree is height-balanced perform the following operations:

Use recursion and visit the left subtree and right subtree of each
node:
Check the height of the left subtree and right subtree.
If the absolute difference between their heights is at most 1
then that node is height-balanced.
Otherwise, that node and the whole tree is not balanced.

Refer to our article on “How to determine if a binary tree is height-


balanced” for implementation of this.

https://www.geeksforgeeks.org/introduction-to-height-balanced-binary-tree/?ref=asr10 5/10
2/14/25, 10:57 AM Introduction to Height Balanced Binary Tree - GeeksforGeeks

Master Data Structures and Algorithms at your own pace with our
DSA Self-Paced course. In just 90 days, you’ll cover core concepts,
solve real-world problems, and sharpen your problem-solving
skills. Take the Three 90 Challenge: complete 90% of the course in
90 days and get a 90% refund. Stay motivated, track progress, and
achieve DSA mastery. Start today!

Comment More info


Next Article
Placement Training Program Balanced Binary Tree

Similar Reads
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…
4 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…
9 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…
4 min read

Count Balanced Binary Trees of Height h


Given a height h, count and return the maximum number of balanced
binary trees possible with height h. A balanced binary tree is one in whic…
9 min read

Complexity of different operations in Binary tree, Binary Search Tre…


https://www.geeksforgeeks.org/introduction-to-height-balanced-binary-tree/?ref=asr10 6/10
2/14/25, 10:57 AM Introduction to Height Balanced Binary Tree - GeeksforGeeks

In this article, we will discuss the complexity of different operations in


binary trees including BST and AVL trees. Before understanding this…
4 min read

Balanced Binary Tree definition & meaning in DSA


Balanced binary tree is defined as a binary tree data structure where there
is no more than one height difference between the left and right subtree…
2 min read

Create Balanced Binary Tree using its Leaf Nodes without using extr…
Prerequisites: Binary Tree to Doubly Linked ListGiven a Binary Tree, the
task is to create a Balanced Binary Tree from all the leaf nodes of the…
15 min read

Check if the Binary Tree contains a balanced BST of size K


Given a Binary Tree and a positive integer K. The task is to check whether
the Balanced BST of size K exists in a given Binary Tree or not. If it exists…
13 min read

Count balanced nodes present in a binary tree


Given a binary tree, the task is to count the number of balanced nodes in
the given tree. Balanced nodes of a binary tree are defined as the nodes…
7 min read

Sum of specially balanced nodes from a given Binary Tree


Given a Binary Tree, the task is to find the sum of all the specially
balanced nodes in the given Binary Tree. A specially balanced node in a…
13 min read

Corporate & Communications Address:


A-143, 7th Floor, Sovereign Corporate
Tower, Sector- 136, Noida, Uttar Pradesh
https://www.geeksforgeeks.org/introduction-to-height-balanced-binary-tree/?ref=asr10 7/10
2/14/25, 10:57 AM Introduction to Height Balanced Binary Tree - GeeksforGeeks
(201305)

Registered Address:
K 061, Tower K, Gulshan Vivante
Apartment, Sector 137, Noida, Gautam
Buddh Nagar, Uttar Pradesh, 201305

Advertise with us

Company Explore
About Us Job-A-Thon Hiring Challenge
Legal Hack-A-Thon
Privacy Policy GfG Weekly Contest
Careers Offline Classes (Delhi/NCR)
In Media DSA in JAVA/C++
Contact Us Master System Design
GFG Corporate Solution Master CP
Placement Training Program GeeksforGeeks Videos
Geeks Community

Languages DSA
Python Data Structures
Java Algorithms
C++ DSA for Beginners
PHP Basic DSA Problems
GoLang DSA Roadmap
SQL DSA Interview Questions
R Language Competitive Programming
Android Tutorial

Data Science & ML Web Technologies


Data Science With Python HTML
Data Science For Beginner CSS
Machine Learning JavaScript
ML Maths TypeScript
Data Visualisation ReactJS
Pandas NextJS
NumPy NodeJs
NLP Bootstrap
Deep Learning Tailwind CSS

Python Tutorial Computer Science


Python Programming Examples GATE CS Notes

https://www.geeksforgeeks.org/introduction-to-height-balanced-binary-tree/?ref=asr10 8/10
2/14/25, 10:57 AM Introduction to Height Balanced Binary Tree - GeeksforGeeks

Django Tutorial Operating Systems


Python Projects Computer Network
Python Tkinter Database Management System
Web Scraping Software Engineering
OpenCV Tutorial Digital Logic Design
Python Interview Question Engineering Maths

DevOps System Design


Git High Level Design
AWS Low Level Design
Docker UML Diagrams
Kubernetes Interview Guide
Azure Design Patterns
GCP OOAD
DevOps Roadmap System Design Bootcamp
Interview Questions

School Subjects Commerce


Mathematics Accountancy
Physics Business Studies
Chemistry Economics
Biology Management
Social Science HR Management
English Grammar Finance
Income Tax

Databases Preparation Corner


SQL Company-Wise Recruitment Process
MYSQL Resume Templates
PostgreSQL Aptitude Preparation
PL/SQL Puzzles
MongoDB Company-Wise Preparation
Companies
Colleges

Competitive Exams More Tutorials


JEE Advanced Software Development
UGC NET Software Testing
UPSC Product Management
SSC CGL Project Management
SBI PO Linux
SBI Clerk Excel
IBPS PO All Cheat Sheets
IBPS Clerk Recent Articles

Free Online Tools Write & Earn


Typing Test Write an Article
Image Editor Improve an Article
Code Formatters Pick Topics to Write

https://www.geeksforgeeks.org/introduction-to-height-balanced-binary-tree/?ref=asr10 9/10
2/14/25, 10:57 AM Introduction to Height Balanced Binary Tree - GeeksforGeeks

Code Converters Share your Experiences


Currency Converter Internships
Random Number Generator
Random Password Generator

DSA/Placements Development/Testing
DSA - Self Paced Course JavaScript Full Course
DSA in JavaScript - Self Paced Course React JS Course
DSA in Python - Self Paced React Native Course
C Programming Course Online - Learn C with Data Structures Django Web Development Course
Complete Interview Preparation Complete Bootstrap Course
Master Competitive Programming Full Stack Development - [LIVE]
Core CS Subject for Interview Preparation JAVA Backend Development - [LIVE]
Mastering System Design: LLD to HLD Complete Software Testing Course [LIVE]
Tech Interview 101 - From DSA to System Design [LIVE] Android Mastery with Kotlin [LIVE]
DSA to Development [HYBRID]
Placement Preparation Crash Course [LIVE]

Machine Learning/Data Science Programming Languages


Complete Machine Learning & Data Science Program - [LIVE] C Programming with Data Structures
Data Analytics Training using Excel, SQL, Python & PowerBI - C++ Programming Course
[LIVE] Java Programming Course
Data Science Training Program - [LIVE] Python Full Course
Mastering Generative AI and ChatGPT
Data Science Course with IBM Certification

Clouds/Devops GATE
DevOps Engineering GATE CS & IT Test Series - 2025
AWS Solutions Architect Certification GATE DA Test Series 2025
Salesforce Certified Administrator Course GATE CS & IT Course - 2025
GATE DA Course 2025
GATE Rank Predictor

@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved

https://www.geeksforgeeks.org/introduction-to-height-balanced-binary-tree/?ref=asr10 10/10

You might also like