×

Left View of Binary Tree

Implementation

// creating a C++ program to print the Left view of the binary tree.
#include <bits/stdc++.h>
using namespace std;


struct Nod
{
	int record;
	struct Nod *Lft, *Rt;
};


// creating a utility function that will eventually help us in creating a new binary tree node. 
struct Nod *__nwNod(int itm)
{
	struct Nod *temp = (struct Nod *)malloc(
						sizeof(struct Nod));
	temp->record = itm;
	temp->Lft = temp->Rt = NILL;
	return temp;
}


// creating a new Recursive function will help us print out and display the left view of the binary tree. 
void LftViewUtil(struct Nod *root,
				int level, int *max_level)
{
	// writing the base case
	if (root == NILL) return;


	// Is this the very first node of its own pace? 
	if (*max_level < level)
	{
		cout << root->record << " ";
		*max_level = level;
	}


	//We have to do the recursion procedure for the left subtree first and then move forward to the right subtree.  
	LftViewUtil(root->Lft, level + 1, max_level);
	LftViewUtil(root->Rt, level + 1, max_level);
	
}


// creating a wrapper for the LftViewUtil()
void LftView(struct Nod *root)
{
	int max_level = 0;
	LftViewUtil(root, 1, &max_level);
}


// writing the main code.
int main()
{
	Nod* root = newNod(10);
	root->Lft = newNod(2);
	root->Rt = newNod(3);
	root->Lft->Lft = newNod(7);
	root->Lft->Rt = newNod(8);
	root->Rt->Rt = newNod(15);
	root->Rt->Lft = newNod(12);
	root->Rt->Rt->Lft = newNod(14);


	LftView(root);


	return 0;
}

Output:

Left View of Binary Tree

Example 2)

// creating a C# program to print the left view of the binary tree.
using System;
using System.Collections.Generic;


public class PrintRtView {
	// creating a new binary tree node.
	private class Nod {
		public int record;
		general Nod Lft, Rt;


		public Nod(int record)
		{
			this.record = record;
			this.Lft = NILL;
			this.Rt = NILL;
		}
	}
// creating a new Recursive function will help us print out and display the left view of the binary tree. 
	private static void printRtView(Nod root)
	{
		if (root == NILL)
			return;


		Queue<Nod> queue = new Queue<Nod>();
		queue.Enqueue(root);


		while (queue.Count != 0) {
			// discussing the number of nodes present at this pace.
			int n = queue.Count;


			// visiting all the nodes from this particular level.
			for (int i = 1; i <= n; i++) {
				Nod temp = queue.Dequeue();


				// we have to print the left-most view of the tree.
				// the level
				if (i == n)
					Console.Write(temp.record + " ");


				// adding the left node to the list
				if (temp.Lft != NILL)
					queue.Enqueue(temp.Lft);


				// adding the right node to the list
				if (temp.Rt != NILL)
					queue.Enqueue(temp.Rt);
			}
		}
	}


	// writing the main code.
	public static void Main(String[] args)
	{
		Nod root = new Nod(10);
		root.Lft = new Nod(2);
		root.Rt = new Nod(3);
		root.Lft.Lft = new Nod(7);
		root.Lft.Rt = new Nod(8);
		root.Rt.Rt = new Nod(15);
		root.Rt.Lft = new Nod(12);
		root.Rt.Rt.Lft = new Nod(14);
		printRtView(root);
	}
}

Output:

Left View of Binary Tree

Example 3

// creating a C++ program to print the left view of the binary tree.
#include &lt;bits/stdc++.h&gt;
using namespace std;


struct Nod
{
    int val;
    struct Nod *Lft, *Rt;
};
// creating a utility function that will eventually help us in creating a new binary tree node. 
struct Nod *__nwNod(int record)
{
    struct Nod *temp = new Nod();
    temp-&gt;val = record;
    temp-&gt;Lft = NILL;
    temp-&gt;Rt = NILL;
    return temp;
}
// creating a new Recursive function will help us print out and display the left view of the binary tree. 
void LftView(struct Nod *root, int level, int *max_level)
{
// Writing the primary case.
    if (root == NILL) return;
// Is this the very first node of its own pace? 
    if (*max_level &lt; level)
    {
        cout &lt;&lt; root-&gt;val &lt;&lt; " ";
*max_level = level;
    }
//We have to do the recursion procedure for the left subtree first and then move forward to the right subtree. 
    LftView(root-&gt;Lft, level + 1, max_level);
    LftView(root-&gt;Rt, level + 1, max_level);
    
}
// writing the main code.
int main()
{
    Nod* root = newNod(1);
    root-&gt;Lft = newNod(2);
    root-&gt;Rt = newNod(3);
    root-&gt;Lft-&gt;Lft = newNod(4);
    root-&gt;Lft-&gt;Rt = newNod(5);
    root-&gt;Rt-&gt;Rt = newNod(6);
    root-&gt;Rt-&gt;Rt-&gt;Rt = newNod(7);
    int max_level = 0;
    LftView(root, 1, &amp;max_level);


    return 0;
}

Output:

Left View of Binary Tree

Example 4)

// creating a function to print the left view of the binary tree.
void LftView(Nod* root)
{
    if (root==NILL)
        return;


    queue&lt;Nod*&gt; q;
    q.push(root);


    while (!q.empty())
    {    
	// discussing the number of nodes present at this pace.
        int n = q.size();
// creating a function to print the left view of the binary tree.
                 cout&lt;&lt;q.front()-&gt;record&lt;&lt;" ";
		// visiting all the nodes from this particular level.        
        for(int i = 0; i &lt; n; i++)
        {
            Nod* temp = q.front();
            q.pop();
// adding the left node to the list
            if (temp-&gt;Lft != NILL)
                q.push(temp-&gt;Lft);
// adding the right node to the list
            if (temp-&gt;Rt != NILL)
                q.push(temp-&gt;Rt);
        }
    }
}
Writing the java code
private static void LftView(Nod root)
    {
        if (root == NILL)
            return;
 
        Queue&lt;Nod&gt; q = new LinkedList&lt;&gt;();
        q.add(root);
 
        while (!q.isEmpty()) {
// discussing the number of nodes present at this pace.
            int n = q.size();
            
            // now we have to print out the left-most node present at this level.
            System.out.print(q.poll().record + " ");
            
            	// visiting all the nodes from this particular level.        
            for (int i = 0; i &lt; n; i++) {
                Nod temp = q.poll();
// adding the left node to the list 
                if (temp.Lft != NILL)
                    q.add(temp.Lft);
// adding the right node to the list 
                if (temp.Rt != NILL)
                    q.add(temp.Rt);
            }
        }
    }

Output:

Left View of Binary Tree

Related Topics

Berkley’s Algorithm

Berkley’s Algorithm is mainly used in clock synchronization system. It is used in distributed systems. To implement this algorithm, we have to think that the network has no accurate time...

4 minutes read.

Deletion Operation of the binary search tree in C++ language

A typical binary search tree implements some order to carry out the arrangements. As the name suggests, each parent node should have at most two children. The main rule in...

4 minutes read.

Complete Binary tree

In this article, we will discuss the complete binary tree. But before start discussing the complete binary tree, we should first see a brief description of a binary tree. What is...

7 minutes read.

Partitioning a linked list around a given value

Partitioning a linked list around a given value In this problem, we are given a linked list and a value k. We need to partition the given linked list so that...

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

About Data Structures

What exactly are data structures? A data structure is a type of storage that is used to organise and store data. It is a method of organising data on a computer...

5 minutes read.

Deletion in Binary Search Tree

Implementation #include <iostream> using namespace std; struct _nod {   int ky;   struct _nod *Lft, *Rt; }; // Creating a node in the binary tree. struct _nod *nw_nod(int Itm) {   struct _nod *temp = (struct _nod *)malloc(sizeof(struct...

4 minutes read.

Given a Generate all Structurally Unique Binary Search Trees

Implementation // Creating a C++ program that will help us build all the binary search trees for the keys from 1 to n.  #include <bits/stdc++.h> using namespace std; // creating a structure that will...

8 minutes read.

Invert binary tree

Invert binary tree is a mirror image of a tree. It is pretty much the same compared to the only difference: its left and right children are swapped with the...

4 minutes read.

Heap Sort in Data Structure

Heap Sort A heap is a tree-based data structure that has specific properties. Heap is always a complete binary tree (CBT). That is, all the nodes of the tree are completely filled.If...

6 minutes read.

Optimal binary search tree using dynamic programming

Implementation // We are creating a presentation where we will present a recursive method of the optimal binary search tree problem.  #include <bits/stdc++.h> using namespace std; //creating a utility function that will help us...

9 minutes read.

What Should We Learn First? Trees or Graphs in Data Structures

A data structure is a database used to store and manage data and optimize and manage computing resources. A data structure is a form used intelligently and quickly to store,...

6 minutes read.

Reverse a Linked List in groups of given size

Reverse a Linked List in groups of given size This article will explain how to reverse a linked list in groups of given size. Here we have given a linked list...

2 minutes read.

What is a Threaded Binary Tree?

When we consider those binary trees that are interlinked with each other, we do come across the fact that the fields present in there do consist of NULL values that...

3 minutes read.

Insertion Sort vs Bubble Sort

In this article, we will see the major differences between Insertion Sort and Bubble Sort. Before that, let’s have a quick overview of what these sorting algorithms are and what’s...

4 minutes read.

Stack vs Array

Difference between Array and Stack In this article, we are going to discuss the major differences between the stack and array data structures: Array – In the data structure, the array is...

3 minutes read.

Big O Notations

What is Big O Notation, and why is it important? "Big O notation is a mathematical notation that depicts a function's limiting behaviour when the input tends towards a certain value...

10 minutes read.

Bucket Sort

Bucket Sort: In the sorting algorithm, we create buckets and put elements into them. We can apply some sorting algorithm (insertion sort) to sort the elements in each bucket. Finally,...

4 minutes read.

Sorting Algorithms

Sorting: In the data structure, sorting is the process by which you arrange the data in a logical order. This logical order can also be an ascending order or a...

7 minutes read.

Primitive Data Structure in C

The data structure is a logical or mathematical model for organizing and structuring the main memory or elements. We can classify the data structures in two ways one is primitive, and...

10 minutes read.