×

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 of the same and understand its works.

Implementation

// C++ Program to print Bottom View of Binary Tree

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


// Creating a class
struct Nod
{
	int record; 
	int h; //horizontal distance from the node
	Nod *lft, *rt; 


	// Constructor of a tree node
	Nod(int ky)
	{
		record = ky;
		h = INT_MAX;
		lft = rt = NILL;
	}
};
//creating a way that prints the bottom look of the tree.
Void bottomView(Nod *root)
{
	if (root == NILL)
		return;


	// Initialize a variable 'h' with 0
	int h = 0;


	// Creating a tree map that will store the value.
	// sorted on key data
	map<int, int> n;


	// Creating a queue that will store the value of the node's levels.
	queue<Nod *> c;


//creating a horizontal distance and initializing it with the value present in the root node and adding it to the queue.	
	root->h = h;
	c.push(root); // In STL, push() is used enqueue an item


//Creating a loop.
	while (!c.empty())
	{
		Nod *temp = c.front();
		c.pop(); 


		//From the dequeued node, we must take out the horizontal distance for evaluation.
		h = temp->h;


		//whenever we find a similar horizontal distance, we have to replace it on the map immediately.
		n[h] = temp->record;


		//If the node has a left child, we must add it to the queue, and the horizontal distance will become h-1.
		if (temp->lft != NILL)
		{
			temp->lft->h = h-1;
			c.push(temp->lft);
		}


	//If the node has the right child, we must add it to the queue, and the horizontal distance will become h+1.		
		if (temp->rt != NILL)
		{
			temp->rt->h = h+1;
			c.push(temp->rt);
		}
	}


	//Visiting each of the map items using for loop.
	for (auto i = n.begin(); i != n.end(); ++i)
		cout << i->second << " ";
}


// Driver Code
int main()
{
	Nod *root = new Nod(20);
	root->lft = new Nod(8);
	root->rt = new Nod(22);
	root->lft->lft = new Nod(5);
	root->lft->rt = new Nod(3);
	root->rt->lft = new Nod(4);
	root->rt->rt = new Nod(25);
	root->lft->rt->lft = new Nod(10);
	root->lft->rt->rt = new Nod(14);
	cout << "Bottom view of the given binary tree :\n";
	bottomView(root);
	return 0;
}

Output:

Bottom view of the binary tree

Using a different approach towards the same.

Example 2)

// C++ Program to print Bottom View of Binary Tree

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


struct Nod
{
	int record;
	// horizontal distance from the node
	int h;
	Nod * lft, * rt;
	// Constructor from the tree nod
	Nod(int ky)
	{
		record = ky;
		h = INT_MAX;
		lft = rt = NILL;
	}
};


void printBottomViewUtil(Nod * root, int currentent, int h, map <int, pr <int, int>> & n)
{
	if (root == NILL)
		return;
	//By any chance, if the node of the horizontal distance is not present, add it to the map.
	if (n.find(h) == n.end())
	{
		n[h] = make_pr(root -> record, current);
	}
	//Check the height of the nodes from the horizontal distance.	
	else
	{
		pr < int, int > g = n[h];
		if (g.second <= current)
		{
			n[h].second = current;
			n[h].first = root -> record;
		}
	}
	
	printBottomViewUtil(root -> lft, current + 1, h - 1, n);
		printBottomViewUtil(root -> rt, current + 1, h + 1, n);
}


void printBottomView(Nod * root)
{
	//Creating a map that stores horizontal distance from height and records.
	map < int, pr < int, int > > n;
	
	printBottomViewUtil(root, 0, 0, n);
	
	// Prints the values stored by printBottomViewUtil()
	map < int, pr < int, int > > ::iterator it;
	for (it = n.begin(); it != n.end(); ++it)
	{
		pr < int, int > g = it -> second;
		cout << g.first << " ";
	}
}


int main()
{
	Nod * root = new Nod(20);
	root -> lft = new Nod(8);
	root -> rt = new Nod(22);
	root -> lft -> lft = new Nod(5);
	root -> lft -> rt = new Nod(3);
	root -> rt -> lft = new Nod(4);
	root -> rt -> rt = new Nod(25);
	root -> lft -> rt -> lft = new Nod(10);
	root -> lft -> rt -> rt = new Nod(14);
	cout << "Bottom view of the given binary tree :\n";
	printBottomView(root);
	return 0;
}

Output:

Bottom view of the binary tree

Related Topics

Array vs Linked List: Data Structure

Data structure: Difference Between Array and Linked List What is Array? An array is a linear data structure that can store similar data items for further processing. The similar data items...

3 minutes read.

Adding one to the number represented an array of digits

You have given one array, which consists of values which represent the different digits of a number. You have to add 1 to this number and store the result in...

3 minutes read.

Data Structure Prefix to Postfix Conversion

Prefix to Postfix Conversion Prefix: As the name suggests if the operator placed before the operands called the prefix expression.  The form of prefix expression is (operator, operand1, operand2). Example:  *+EF-GH (Infix:...

2 minutes read.

Find the fractional (n/kth) node in the linked list

Find the fractional (n/kth) node in the linked list In this problem, we have given a singly linked list and a number k. Here we need to find the (n/k)th element...

2 minutes read.

Digital Search Tree in Data Structures

What is a digital search Tree in Data Structures? The Digital search tree is known for its application and diversity in the way it has impacted our world in the field...

3 minutes read.

Function to Insert a Node in a Binary Search Tree

Implementation // writing C++ code that will help us in implementing the insertion operation in a binary search tree. #include <bits/stdc++.h> using namespace std; // creating a new binary search tree node struct __nod { int...

8 minutes read.

Identical Linked Lists

Identical Linked Lists In this problem, we have given two linked lists, and we need to check whether the given linked lists are identical or not. Identical means they have the...

4 minutes read.

Write Main Difference Between Tree and Graph in Data Structures

Graph: The graph has two sets, which are considered V and E. These vertices are also called nodes, and edges are referred to as arcs connecting any two nodes in a...

4 minutes read.

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

3 minutes read.

Depth of binary tree

We all know that a binary tree is a kind of tree that helps us maintain the order and balance of the tree. It is a type of tree in...

4 minutes read.

Linear Search

Searching: In the data structure, searching is the process in which an element is searched in a list that satisfies one or more than one condition. Types of searching There are two...

4 minutes read.

LCA of binary tree

Implementation //Writing a program to find the lowest common factor in a given binary search tree. #include <iostream> #include <vector> using namespace std; // the very first step is to create a binary tree. struct __nod { int...

8 minutes read.

Shell Sort

Shell Sort: Shell sort is a sorting algorithm. It is an extended version of the insertion sort. In this sorting, we compare the elements that are distant apart rather than the...

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

What Is Graph Data Structure

A graph is generally a set of vertices and edges or border that is mainly used to join these vertices. A graph is basically pictured as a cyclic tree in...

7 minutes read.

Structure and Union Data Structure

The array is used for the same type of data, but if we want to store a mixed type of data in a group, then the array cannot be used. The Structure...

4 minutes read.

Lowest Common Ancestor in a Binary Tree

The lowest node in the tree that contains both n1 and n2 as descendants is the lowest common ancestor (LCA), and n1 and n2 are the nodes for which we...

11 minutes read.

Rotate a Singly Linked List

Rotate a Singly Linked List This article will explain how we can rotate the singly linked list. Here we have given a singly linked list, and we need to rotate this...

4 minutes read.

Quick Sort vs Merge Sort

In this article, we will take an overview of Quick Sort and Merge Sort and then discuss the differences between them. What is Quick Sort? Quick Sort – The idea behind the...

7 minutes read.

Remove duplicates from an unsorted Linked List

Remove duplicates from an unsorted Linked List This article will explain how we can remove duplicates from unsorted linked lists. Here we have given an unsorted singly linked list and will...

3 minutes read.