×

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

Object-Oriented Analysis and Design

While designing a system, one should know all the requirements or needs of the plan beforehand, and to do so, we should use a systematic approach to analyze the goal...

3 minutes read.

Burning binary tree

Burn the Binary tree starting from the target node You have given a binary tree and a target node value. Now you have to burn the tree from target node. You...

4 minutes read.

Given a Binary Tree, Check if it's balanced

Implementation /*Creating a C++ program that will help us identify whether the given tree is height-balanced or not.  */ #include <bits/stdc++.h> using namespace std; /* A particular binary tree node consists of data with some...

4 minutes read.

Minimum Spanning Tree

Before getting to know about the minimum spanning tree, we should first discuss about what is a spanning tree. A spanning tree is basically a sub or minimized graph that...

7 minutes read.

Dynamic memory allocation of structure in C

We can normally store elements of the same datatype with the help of an array in C programming. We can store multiple numbers of elements of a character data type...

5 minutes read.

Binary tree deletion

This article will discuss the deletion operation's implementation in the binary tree. The deletion operation helps us eliminate an element from the tree. Implementation #include <bits/stdc++.h> using namespace std; /* A binary tree node...

4 minutes read.

Trim a binary search tree

Implementation //writing a C++ program will help us eliminate the keys that are out of the league.  #include<bits/stdc++.h> using namespace std; //we are now creating a binary search tree node consisting of key left...

8 minutes read.

A Full Binary Tree with n Nodes

Implementation // Writing the implementation of the above approach in C++ #include <bits/stdc++.h> using namespace std; // We are creating a class that will create a node and its left and right children.  struct __nod...

12 minutes read.

Finding the Sum of All Paths in a Binary Tree

Implementation // Writing the C++ program to implement the below approach.  #include <bits/stdc++.h> using namespace std; // creating the new tree node structure. struct Tree__nod { int val; Tree__nod *Lft, *Rt; }; // creating a new function that will...

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

Cocktail Sort

C Program executes cocktail sort. Combo sort is a somewhat straightforward arranging calculation initially planned by Wlodzimierz Dobosiewicz and Artur Borowy in 1980, later rediscovered by Stephen Lacey and Richard Box...

5 minutes read.

Height of a binary tree

The height of a binary tree is generally defined as the height or length of the root _nod in the entire binary tree. In simple words, the height of a...

4 minutes read.

Bubble Sort vs Quick Sort

In this article, we are going to compare two sorting techniques, Bubble sort and Quick Sort. In starting, we will first discuss the idea of sorting an array using bubble...

7 minutes read.

Circular Linked List

Circular Linked List A circular linked list where all nodes are connected to their next node and last node is connected to the starting node or we can say all nodes...

5 minutes read.

Interval Tree

Interval Tree Interval Tree: The concept is to increase a Binary Search Tree self-balancing such as Red Black Tree, and AVL Tree, so that every feature can be completed in time O(Logn). Each Interval...

4 minutes read.

Sum of Nodes in a Binary Tree

In this article, we will see the sample problems that will help us understand the concept and summation of all the nodes in the binary tree. Implementation /* creating a program that...

4 minutes read.

Trie data structure

Trie data structure The term “trie” comes from the word “retrieval” which means getting information. The trie data structure is a sorted extension of tree-based data structure. The trie data structure...

5 minutes read.

Vertical Order Traversal of Binary Tree

Implementation #include <iostream> #include <vector> #include <map> using namespace std; // representing the primary model of a binary tree node. struct _nod { int ky; _nod *Lft, *Rt; }; // establishing a new function representing the new binary tree node. struct _nod*...

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

Construction of B tree in Data Structure

A B-tree is a type of balanced tree data structure that is commonly used in file systems and databases to improve the efficiency of search, insert, and delete operations. The structure...

4 minutes read.