The Wayback Machine - https://web.archive.org/web/20240626213902/https://www.geeksforgeeks.org/balanced-binary-tree/
Open In App

Balanced Binary Tree

Last Updated : 21 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

A binary tree is balanced if the height of the tree is O(Log n) where n is the number of nodes. For Example, the AVL tree maintains O(Log n) height by making sure that the difference between the heights of the left and right subtrees is at most 1. Red-Black trees maintain O(Log n) height by making sure that the number of Black nodes on every root-to-leaf path is the same and that there are no adjacent red nodes. Balanced Binary Search trees are performance-wise good as they provide O(log n) time for search, insert and delete. 

A balanced binary tree is a binary tree that follows the 3 conditions:

  • The height of the left and right tree for any node does not differ by more than 1.
  • The left subtree of that node is also balanced.
  • The right subtree of that node is also balanced.

A single node is always balanced. It is also referred to as a height-balanced binary tree.

Example:

Balanced and Unbalanced Binary Tree

Balanced and Unbalanced Binary Tree

It is a type of binary tree in which the difference between the height of the left and the right subtree for each node is either 0 or 1. In the figure above, the root node having a value 0 is unbalanced with a depth of 2 units.

Application of Balanced Binary Tree:

Advantages of Balanced Binary Tree:

  • Non Destructive updates are supported by a Balanced Binary Tree with the same asymptotic effectiveness.
  • Range queries and iteration in the right sequence are made feasible by the balanced binary tree.

Previous Article
Next Article

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 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
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
Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
In this article, we will discuss the complexity of different operations in binary trees including BST and AVL trees. Before understanding this article, you should have a basic idea about Binary Tree, Binary Search Tree, and AVL Tree. The main operations in a binary tree are: search, insert and delete. We will see the worst-case time complexity of t
4 min read
Convert a Binary Tree into its Mirror Tree (Invert Binary Tree)
Given a binary tree, the task is to convert the binary tree into its Mirror tree. Mirror of a Binary Tree T is another Binary Tree M(T) with left and right children of all non-leaf nodes interchanged. Recommended PracticeMirror TreeTry It!The idea is to traverse recursively and swap the right and left subtrees after traversing the subtrees. Follow
15+ 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 subtrees of any given node. Mathematically Height of left subtree - Height of right subtree â‰Ī1 Properties of Balanced Binary Tree: A balanced binary tree has a height that is logarithmic in the number of nodes
2 min read
Create Balanced Binary Tree using its Leaf Nodes without using extra space
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 given Binary Tree. Examples: Input: Output: 7 8 5 9 10 Explanation: Required balanced binary tree will be: Input: Output: 13 21 29 7 15 Explanation: Required balanced binary tree is: 29 / \ 21 7 / \ 13 15
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 then print “Yes” else print “No”. Examples: Input: K = 4, Below is the given Tree: 15 / \ 10 26 / \ / \ 5 12 25 40 / / \ 20 35 50 \ 60 Output: Yes Explanation: Subtree of the given tree with size k
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 which contains both left and right subtrees with their respective sum of node values equal. Examples: Input: 9 / \ 2 4 / \ \ -1 3 0 Output: 1Explanation:Only node 9 contains the left subtree sum = right
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 Binary Tree contains the sum of nodes of one subtree(either left or right) as even and the sum of the other subtree as odd.The nodes having only one child or no child can never be a balanced node. Examples: I
13 min read
Number of levels having balanced parentheses in a Binary Tree
Given a Binary Tree consisting only '(' and ')' a level is considered to be balanced if the nodes of the level having parenthesis are balanced from left to right. The task is to count the total number of balanced levels in a binary tree. Examples: Input: ( / \ ( ) / \ \ ( ) ( / \ \ \ ( ( ) ) Output: 2Explanation:In Level 2 and 4, the parenthesis ar
10 min read
Practice Tags :
three90RightbarBannerImg