×

Balanced Binary Tree

A balanced binary tree is just a random nod-based tree with a rule of keeping its height minimum in size to maintain various operations such as insertions, deletions and several others. A balanced binary tree turns out to be one in which all the leaf nodes are at a considerable distance from the base or root nod compared to any other node in the tree. A significant amount of work is supposed to be done to keep them balanced and maintained. Balanced binary trees are also technically called height-balanced binary trees. They are generally denoted by the symbol HB(k), where we know that k stands for the difference between both the left and rt subtrees. Suppose we have a tree that contains the value of k as 0; then, the tree would be considered a fully balanced binary tree

 in that situation. 

To check whether the tree's height is balanced, we have first to get the difference between the two left and rt subtrees, and if the difference between the two is not more than one, then it will return the value true otherwise, false.

The time complexity of the balanced binary tree is known to be O(n2), and the auxiliary complexity of the same is known to be O(n) since it has recursions.

Conditions

There are some well-defined conditions which we have to follow to keep the tree well-maintained. They are as follows: -

  • The subtree on the rt is supposed to be balanced.
  • The subtree on the left is supposed to be balanced.
  • The difference between both the subtrees is not more than one.

Implementation

This section of the article will give examples of the implementation and working of the balanced binary tree to help you understand the concept better.

// Checking if a binary tree is a height-balanced in C++

Example 1)

#include 
using namespace std;


#define bool int


Class N {
   public:
  int item;
  nod *lft;
  nod *rt;
};


// Create anew nod
nod *newNod(int item) {
  nod *Nod = new nod();
  Nod->item = item;
  Nod->lft = NILL;
  Nod->rt = NILL;


  return (Nod);
}


// Check height balance
bool checkHeightBalance(nod *root, int *height) {
  // Check for emptiness
  int lftHeight = 0, rtHeight = 0;


  int l = 0, r = 0;


  if (root == NILL) {
    *height = 0;
    return 1;
  }


  l = checkHeightBalance(root->lft, &lftHeight);
  r = checkHeightBalance(root->rt, &rtHeight);


  *height = (lftHeight > rtHeight ? lftHeight : rtHeight) + 1;


  if (std::abs(lftHeight - rtHeight >= 2))
    return 0;


  else
    return l && r;
}


int main() {
  int height = 0;


  nod *root = newNod(1);
  root->lft = newNod(2);
  root->rt = newNod(3);
  root->lft->lft = newNod(4);
  root->lft->rt = newNod(5);


  if (checkHeightBalance(root, &height))
    cout << "The tree is balanced";
  else
    cout << "The tree is not balanced";
}

Output:

BALANCED BINARY TREE

Example 2)

#include <bits/stdc++.h>
using namespace std;


/* A binary tree nod has info,
pointer to lft child and
a pointer to the rt nod */
class N {
public:
	int info;
	Nod* lft;
	Nod* rt;
	Nod(int d)
	{
		int info = d;
		lft = rt = NILL;
	}
};


// Function to calculate the height of a tree
int height(Nod* nod)
{
	// base case tree is empty
	if (nod == NILL)
		return 0;


	// If a tree is not empty, then
	// height = 1 + max of lft height
	// and rt heights
	return 1 + max(height(nod->lft), height(nod->rt));
}


// Returns true if binary tree
// with root as root is height-balanced
bool isBalanced(Nod* root)
{
	int lh; // for the height of lft subtree
	int rh; // for the height of rt subtree


	// If a tree is empty, then return true
	if (root == NILL)
		return 1;


	// Get the height of lft and rt subtrees
	lh = height(root->lft);
	rh = height(root->rt);


	if (abs(lh - rh) <= 1 && isBalanced(root->lft)
		&& isBalanced(root->rt))
		return 1;


	// If we reach here, then the tree is not height-balanced
	return 0;
}


// Driver code
int main()
{
	Nod* root = new Nod(1);
	root->lft = new Nod(2);
	root->rt = new Nod(3);
	root->lft->lft = new Nod(4);
	root->lft->rt = new Nod(5);
	root->lft->lft->lft = new Nod(8);


	if (isBalanced(root))
		cout << "Tree is balanced";
	else
		cout << "Tree is not balanced";
	return 0;
}

Output:

BALANCED BINARY TREE

Related Topics

DFS (Depth-first search) Algorithm: Data Structure

What is DFS (Depth-first search)? The depth first search is a graph traversal algorithm. The idea behind this algorithm is backtracking and it is a kind of recursive algorithm. In the...

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

Threaded Binary Tree

The linked form of binary trees wastes storage capacity because more than half of the connection variables have a Missing value. A binary tree has several nodes. Hence n+1 link fields...

8 minutes read.

Find the nth node from the end of a Linked List

Find the nth node from the end of a Linked List In this problem, we have given a singly linked list and a number 'n,' and we need to find the...

3 minutes read.

Bottom view of the binary tree

The bottom of the binary tree is generally defined as the number of nods present in the bottom-most part of the tree. In this article, we will see the implementation...

3 minutes read.

Find out the area between two concentric circles

You have given two values of the radius of two circles. You have to find out the area between these two circles. Let's take an example - For the above diagram,...

3 minutes read.

What is a Height-Balanced Tree in Data Structure

A height-balanced tree is a type of binary tree. If the absolute difference between the heights of the left and right subtree is less than or equal to 1, then...

6 minutes read.

What is the B+ Tree in Data Structures?

We all know that the B+ tree in data structures is nothing but just an extended version of the B tree. It allows the smooth working of all the operations...

7 minutes read.

Flattening a Linked List

In this article, we are going to study about the logic behind the flattening of linked list and we also going to build a code in the C++ to flatten...

3 minutes read.

AVL Tree

AVL Tree AVL Tree is referred to as self-balanced or height-balanced binary search tree where the difference between heights of its left subtree and right subtree (Balance Factor) can't more than...

25 minutes read.

What Is Dfs Algorithm in Data Structures

DFS stands for Depth First Search. Generally, it is a repetitive or decidable type of algorithm which is basically used in identifying all the vertices or nodes of a graph...

5 minutes read.

Detect Loop in Linked List: Data Structure

Detect the Loop in Linked List: In this problem, we will be seeing some technique through which we can detect the loop in linked list. We will discuss each technique...

3 minutes read.

Asynchronous advantage actor-critic (A3C) Algorithm

The Asynchronous advantage actor-critic (A3C) Algorithm is one of the latest algorithms developed by the Artificial Intelligence division, Deep Mind at Google. It is used for the Deep Reinforcement Learning...

3 minutes read.

Difference between Structured and Object-Oriented Analysis

Analysis means observing and collecting relevant information about the structure of something or the basic details of a system's requirements. Structured and Object Oriented Analysis are both widely used in...

2 minutes read.

Introduction to 1D-Arrays

One Dimensional Array Technical Definitions The simplest version of an Array is a One-Dimensional Array, in which the items are stored linearly and may be accessed individually by supplying the index value...

6 minutes read.

Polish Notation in Data Structures

Arithmetic Expression: An arithmetic expression is defined as several operands or data items combined using several operators. For example; a+b*(c-d) is an expression. Operands: Operands represent the data in an expression...

2 minutes read.

Binary Tree in Data Structures

What is a Binary Tree in Data Structures? The term binary itself means bi, which implies two of anything. So very clearly, we know we present the trees in the form...

6 minutes read.

Insertion in B+ Tree

We will learn how to insert a node in the B+ tree and what are the different properties we are going to follow. Except for the root node, every node should...

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

Merge two sorted linked lists

Merge two sorted linked lists In this article, we are going to learn how to merge two linked lists. Here we have given two linked lists that are sorted in increasing...

7 minutes read.