Javatpoint Logo
Javatpoint Logo

Bottom View of a Binary Tree

Binary Tree:

There can be a maximum of two children for each parent in a binary tree, which is a non-linear data structure of the tree type.

Every node in a binary tree has a left and right reference in addition to the data element. The node at the top of a tree's hierarchy is known as the root node. The nodes that contain other sub-nodes are the parent nodes.

The left and right children are the two children of a parent node. Hashing, data compression, network traffic routing, setting up binary heaps, and building binary search trees are just a few of the uses for a binary tree.

Bottom View:

The lowest nodes that are present at their horizontal distance are referred to as the bottom view of a binary tree.

The horizontal distance between the nodes of a binary tree is described as follows:

  1. Root's horizontal distance is equal to 0.
  2. Left child's horizontal distance is equal to its parent's horizontal distance minus one.
  3. A right child's horizontal distance is equal to its parent's horizontal distance plus one.
  4. The bottom view of a binary tree and its horizontal distances are depicted in the accompanying diagram.

How is the bottom of a binary tree printed?

The bottom view of a binary tree can be printed by hashing and traversing the binary tree in level order.

What is a binary tree's diameter?

The number of nodes on a tree's longest path connecting two leaves is known as its diameter.

We must print the bottom view of the binary tree, or all of the nodes that are located at the bottom of the tree from left to right.

A binary tree's 3D projection will show diverse views from various perspectives. In this instance, we'll publish the binary tree's bottom view.

It's preferable to picture them at a horizontal distance (like an x-axis). The horizontal distance, or hd 0, will be where the root node is situated. Both the child and the left child will have hd+1 of their respective parents. Our output will have the bottom nodes at each horizontal distance.

Algorithm:

Pre-order traversal should be used to determine each node's horizontal distance.

Each node's:

  1. If the node is the first node to have a certain horizontal distance, add it to the result.
  2. Replace a node that already exists in the result for a specific edit distance with the current node only if the level of the present node is higher than the level of the node that was previously inserted.

C++:


Input:
         10
       /    \
      20    30
     /  \
    40   60
	
Output: 
40 20 60 30

Java:


Input:
         10
       /    \
      20    30
     /  \
    40   60
	
Output: 
40 20 60 30

Python:


Input:
         10
       /    \
      20    30
     /  \
    40   60
Output: 
40 20 60 30

Conclusion:

The representation of all of a binary tree's lowest nodes is known as the bottom view.

With the use of horizontal distances (hd) and the height of the successor in a binary tree, we may make understanding of the lowest nodes more straightforward.

There are two approaches to find a binary tree's bottom view:

Recursion, queue & HashMap







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA