Print Bottom-Left View of a Binary Tree

Bottom Left View of a Binary Tree

Here given code implementation process.

/*
  C Program 
+ Print Bottom-Left View of a Binary Tree
*/
#include<stdio.h>
#include<stdlib.h>
//structure of Binary Tree node
struct Node
{
  int data;
  struct Node*left,*right;
};

struct List
{
  int order,data;
  struct List*next;
  
};
//Create a binary tree nodes and node fields (data,pointer) 
//And returning the reference of newly nodes

struct Node* insert(int data)
{
  //create dynamic memory to new binary tree node
  struct Node*new_node=(struct Node*)malloc(sizeof(struct Node));
  if(new_node!=NULL)
  {
    //set data and pointer values
    new_node->data=data;
    new_node->left=NULL; //Initially node left-pointer is NULL
    new_node->right=NULL;//Initially node right-pointer is NULL
  }else
  {
    printf("Memory Overflow\n");
    exit(0); //Terminate program execution
  }
  //return reference
  return new_node;
  
}
struct List* create(int order,int data)
{
  struct List*new_node=(struct List*)malloc(sizeof(struct List));
  if(new_node!=NULL)
  {
    new_node->next=NULL;
    new_node->data=data;
    new_node->order=order;
    return new_node;
  }else
  {
    printf("Memory Overflow\n");
    exit(0);
    
  }
}
//Adding a new element in sorted order when get new left bottom element
void addList(struct List**result,int order,int data)
{
  struct List*new_node=NULL,*back=NULL;
  if(*result!=NULL)
  {

    struct List*temp=*result;

    if(temp->order>order)
    {
      //smallest order element
      new_node=create(order,data);
      new_node->next=*result;
      *result=new_node;
    }else{
      int status=1;
      //Check inserted node are exist or not
      while(temp!=NULL)
      {

        if(temp->order==order)
        {
          //get new node 
          temp->data=data;
          
          status=0;
          break;
        }
        temp=temp->next;
      }
      if(status==1)
      {
          temp=*result;

          while(temp!=NULL)
          {
            back=temp;
            if(temp->order > order)
            {
              break;
            }else
            {
              temp=temp->next;
            }
          }
        //add new bottom node
        new_node=create(order,data);
        new_node->next=back->next;
        back->next=new_node;
      }
    }
  }else{
    //When first node are get
    *result=create(order,data);
  }

}
void removeList(struct List**bucket)
{
  struct List*temp=NULL;
  while(*bucket)
  {
    temp=*bucket;
    *bucket=temp->next;
    free(temp);
    temp=NULL;
  }
}
//Get the information about bottom left view
void get_bottom(struct Node*node,struct List**bucket,int order)
{

  if(node!=NULL)
  {
    addList(bucket,order,node->data);
    get_bottom(node->right,bucket,order+1);
    get_bottom(node->left,bucket,order);
    
  }
}
void left_bottom(struct Node*root)
{
  struct List*bucket=NULL;

  //Get the left bottom view
  get_bottom(root,&bucket,0);

  struct List*temp=bucket;
  //Show Left bottom elements
  while(temp!=NULL)
  {
    printf("%3d",temp->data );
    temp=temp->next;
  }
  //Removing all List element which is 
  //containing the information about left bottom
  removeList(&bucket);
}
int main(){

  struct Node*root=NULL;
  


  /*  Make A Binary Tree
  -----------------------
                10
               /  \
              2    4
             /    / \
            3    6   5
                  \
                   7
                 /  \
                8    11 



     [//]
     view
     position
  */
 
  //Insertion of binary tree nodes
  root               =insert(10);
  root->left         =insert(2);
  root->left->left   =insert(3);

  root->right        =insert(4);
  root->right->right =insert(5);
  root->right->left  =insert(6);

  root->right->left->right  =insert(7);
  root->right->left->right->left  =insert(8);
  root->right->left->right->right  =insert(11);
 
  left_bottom(root);
  

  return 0;
}

Output

3  6  8 11
/*
  C++ Program 
+ Print Bottom-Left View of a Binary Tree
*/

#include <iostream>

using namespace std;

class Node {

  public:
    int data;

  Node *left, *right;

  Node(int value) {
    data = value;
    left = NULL;
    right = NULL;
  }
};

class List {

  public:
    int order, data;

  List *next;

  List(int level, int value) {
    next = NULL;
    order = level;
    data = value;
  }
};
class BinaryTree {

  public:
    Node *root;

  List *result;

  BinaryTree() {
    root = NULL;
    result = NULL;
  }

  void addList(int level, int value) {
    List *new_node = NULL, *back = NULL;

    if (result != NULL) {
      List *temp = result;

      if (temp->order < level) {
        new_node = new List(level, value);
        new_node->next = result;
        result = new_node;
      } else {
        int status = 1;

        while (temp != NULL) {

          if (temp->order == level) {
            temp->data = value;
            status = 0;
            break;
          }
          temp = temp->next;
        }

        if (status == 1) {
          temp = result;

          while (temp != NULL) {
            back = temp;

            if (temp->order < level) {
              break;
            } else {
              temp = temp->next;
            }
          }
          new_node = new List(level, value);
          new_node->next = back->next;
          back->next = new_node;
        }
      }
    } else {
      result = new List(level, value);
    }
  }
  //Remove list elements
  void remove_list() {
    List *temp = NULL;

    while (result != NULL) {
      temp = result;
      result = temp->next;
      delete(temp);
      temp = NULL;
    }
  }
  //Recursive select LEFT bottom view
  void get_bottom(Node *node, int level) {

    if (node != NULL) {
      addList(level, node->data);
      get_bottom(node->right, level - 1);
      get_bottom(node->left, level);



    }
  }
  void left_bottom() {
    List *temp = result;

    while (temp != NULL) {
      cout << " " << temp->data;
      temp = temp->next;
    }
  }
};

int main() {
  BinaryTree obj;
  /*Make A Binary Tree
  -----------------------
        10
       /  \
      2    4
     /    / \
    3    6   5
          \
           7
         /  \
        8    11 


                    
[//]
view
position
  */
  obj.root = new Node(10);
  obj.root->left = new Node(2);
  obj.root->left->left = new Node(3);
  obj.root->right = new Node(4);
  obj.root->right->right = new Node(5);
  obj.root->right->left = new Node(6);
  obj.root->right->left->right = new Node(7);
  obj.root->right->left->right->left = new Node(8);
  obj.root->right->left->right->right = new Node(11);
  obj.get_bottom(obj.root, 0);
  obj.left_bottom();
  obj.remove_list();
  return 0;
}

Output

3 6 8 11
/*
  Java Program 
+ Print Bottom-Left View of a Binary Tree
*/

class Node {

  public int data;

  public Node left, right;

  public Node(int value) {
    data = value;
    left = null;
    right = null;
  }
}

class List {

  public int order, data;

  public List next;

  public List(int level, int value) {
    next = null;
    order = level;
    data = value;
  }
}
class BinaryTree {

  public Node root;

  public List result;

  public BinaryTree() {
    root = null;
    result = null;
  }

  public void addList(int level, int value) {
    List new_node = null, back = null;

    if (result != null) {
      List temp = result;

      if (temp.order < level) {
        new_node = new List(level, value);
        new_node.next = result;
        result = new_node;
      } else {
        int status = 1;

        while (temp != null) {

          if (temp.order == level) {
            temp.data = value;
            status = 0;
            break;
          }
          temp = temp.next;
        }

        if (status == 1) {
          temp = result;

          while (temp != null) {
            back = temp;

            if (temp.order < level) {
              break;
            } else {
              temp = temp.next;
            }
          }
          new_node = new List(level, value);
          new_node.next = back.next;
          back.next = new_node;
        }
      }
    } else {
      result = new List(level, value);
    }
  }
  //Remove list elements
  public void removeList() {
    List temp = null;

    while (result != null) {
      temp = result;
      result = temp.next;
      temp = null;
    }
  }
  //Recursive select right bottom view
  public void get_bottom(Node node, int level) {

    if (node != null) {
      addList(level, node.data);
      get_bottom(node.right, level - 1);
      get_bottom(node.left, level);
    }
  }
  public void left_bottom() {
    List temp = result;

    while (temp != null) {
      System.out.printf("%3d", temp.data);
      temp = temp.next;
    }
  }
  public static void main(String[] args) {
    BinaryTree obj = new BinaryTree();
    /*Make A Binary Tree
    -----------------------
          10
         /  \
        2    4
       /    / \
      3    6   5
            \
             7
           /  \
          8    11 


  
[//]
view
position
    */
    obj.root = new Node(10);
    obj.root.left = new Node(2);
    obj.root.left.left = new Node(3);
    obj.root.right = new Node(4);
    obj.root.right.right = new Node(5);
    obj.root.right.left = new Node(6);
    obj.root.right.left.right = new Node(7);
    obj.root.right.left.right.left = new Node(8);
    obj.root.right.left.right.right = new Node(11);
    obj.get_bottom(obj.root, 0);
    obj.left_bottom();
    obj.removeList();

  }
}

Output

 3  6  8 11
# Python3 Program
# Print Bottom-Left View of a Binary Tree
class Node :

  def __init__(self,value) :
    self.data = value;
    self.left = None;
    self.right = None;
  
class List :

  def __init__(self,level, value) :
    self.next = None;
    self.order = level;
    self.data = value;
  

class BinaryTree :

  def __init__(self) :
    self.root = None;
    self.result = None;
  
  def addList(self,level, value) :
    new_node = None;
    back = None;
    if (self.result != None) :
      temp = self.result;
      if (temp.order < level) :
        new_node = List(level, value);
        new_node.next = self.result;
        self.result = new_node;
      else :
        status = 1;
        while (temp != None) :
          if (temp.order == level) :
            temp.data = value;
            status = 0;
            break;
          
          temp = temp.next;
        
        if (status == 1) :
          temp = self.result;
          while (temp != None) :
            back = temp;
            if (temp.order < level) :
              break;
            else :
              temp = temp.next;
            
          
          new_node = List(level, value);
          new_node.next = back.next;
          back.next = new_node;
        
      
    else :
      self.result = List(level, value);
    
  
  def remove_list(self) :
    temp = None;
    while (self.result != None) :
      temp = self.result;
      self.result = temp.next;
      temp = None;
    
  
  def get_bottom(self,node, level) :
    if (node != None) :
      self.addList(level, node.data);
      self.get_bottom(node.right, level - 1);
      self.get_bottom(node.left, level);
    
  
  def left_bottom(self) :
    temp = self.result;
    while (temp != None) :
      print(temp.data,end=" ");
      temp = temp.next;
    
  
def main() :
  obj = BinaryTree();


  #  Make A Binary Tree
  #        10
  #       /  \
  #      2    4
  #     /    / \
  #    3    6   5
  #          \
  #           7
  #         /  \
  #        8    11
  #
  #
# [//]
# view
# position
#  
  obj.root = Node(10);
  obj.root.left = Node(2);
  obj.root.left.left = Node(3);
  obj.root.right = Node(4);
  obj.root.right.right = Node(5);
  obj.root.right.left = Node(6);
  obj.root.right.left.right = Node(7);
  obj.root.right.left.right.left = Node(8);
  obj.root.right.left.right.right = Node(11);
  obj.get_bottom(obj.root, 0);
  obj.left_bottom();
  obj.remove_list();
  

if __name__ == "__main__":
  main()

Output

3 6 8 11
/*
  C# Program 
+ Print Bottom-Left View of a Binary Tree
*/
using System;
class Node {

  public int data;

  public Node left, right;

  public Node(int value) {
    data = value;
    left = null;
    right = null;
  }
}

class List {

  public int order, data;

  public List next;

  public List(int level, int value) {
    next = null;
    order = level;
    data = value;
  }
}
class BinaryTree {

  public Node root;

  public List result;

  public BinaryTree() {
    root = null;
    result = null;
  }

  public void addList(int level, int value) {
    List new_node = null, back = null;

    if (result != null) {
      List temp = result;

      if (temp.order < level) {
        new_node = new List(level, value);
        new_node.next = result;
        result = new_node;
      } else {
        int status = 1;

        while (temp != null) {

          if (temp.order == level) {
            temp.data = value;
            status = 0;
            break;
          }
          temp = temp.next;
        }

        if (status == 1) {
          temp = result;

          while (temp != null) {
            back = temp;

            if (temp.order < level) {
              break;
            } else {
              temp = temp.next;
            }
          }
          new_node = new List(level, value);
          new_node.next = back.next;
          back.next = new_node;
        }
      }
    } else {
      result = new List(level, value);
    }
  }
  //Remove list elements
  public void removeList() {
    List temp = null;

    while (result != null) {
      temp = result;
      result = temp.next;
      temp = null;
    }
  }
  //Recursive select right bottom view
  public void get_bottom(Node node, int level) {

    if (node != null) {
      addList(level, node.data);
      get_bottom(node.right, level - 1);
      get_bottom(node.left, level);
    }
  }
  public void left_bottom() {
    List temp = result;

    while (temp != null) {
      Console.Write("  {0}", temp.data);
      temp = temp.next;
    }
  }
  public static void Main(String[] args) {
    BinaryTree obj = new BinaryTree();
    /* Make A Binary Tree
    -----------------------
          10
         /  \
        2    4
       /    / \
      3    6   5
            \
             7
           /  \
          8    11 


                      
  [//]
  view
  position
    */
    obj.root = new Node(10);
    obj.root.left = new Node(2);
    obj.root.left.left = new Node(3);
    obj.root.right = new Node(4);
    obj.root.right.right = new Node(5);
    obj.root.right.left = new Node(6);
    obj.root.right.left.right = new Node(7);
    obj.root.right.left.right.left = new Node(8);
    obj.root.right.left.right.right = new Node(11);
    obj.get_bottom(obj.root, 0);
    obj.left_bottom();
    obj.removeList();

  }
}

Output

3 6 8 11
<?php
/*
  Php Program
  Print Bottom-Left View of a Binary Tree
*/
class Node {
  public $data;
  public $left;
  public $right;

  function __construct($value) {
    $this->data = $value;
    $this->left = null;
    $this->right = null;
  }
}
class Lists {
  public $order;
  public $data;
  public $next;

  function __construct($level, $value) {
    $this->next = null;
    $this->order = $level;
    $this->data = $value;
  }
}
class BinaryTree {
  public $root;
  public $result;

  function __construct() {
    $this->root = null;
    $this->result = null;
  }
  public function addLists($level, $value) {
    $new_node = null;
    $back = null;
    if ($this->result != null) {
      $temp = $this->result;
      if ($temp->order < $level) {
        $new_node = new Lists($level, $value);
        $new_node->next = $this->result;
        $this->result = $new_node;
      } else {
        $status = 1;
        while ($temp != null) {
          if ($temp->order == $level) {
            $temp->data = $value;
            $status = 0;
            break;;
          }
          $temp = $temp->next;
        }
        if ($status == 1) {
          $temp = $this->result;
          while ($temp != null) {
            $back = $temp;
            if ($temp->order < $level) {
              break;;
            } else {
              $temp = $temp->next;
            }
          }
          $new_node = new Lists($level, $value);
          $new_node->next = $back->next;
          $back->next = $new_node;
        }
      }
    } else {
      $this->result = new Lists($level, $value);
    }
  }
  public function removeLists() {
    $temp = null;
    while ($this->result != null) {
      $temp = $this->result;
      $this->result = $temp->next;
      $temp = null;
    }
  }
  public function get_bottom($node, $level) {
    if ($node != null) {
      $this->addLists($level, $node->data);
      $this->get_bottom($node->right, $level - 1);
      $this->get_bottom($node->left, $level);
    }
  }
  public function left_bottom() {
    $temp = $this->result;
    while ($temp != null) {
      printf("%3d", $temp->data);
      $temp = $temp->next;
    }
  }
}
function main() {
  $obj = new BinaryTree();

  /* Make A Binary Tree
  -----------------------
        10
       /  \
      2    4
     /    / \
    3    6   5
          \
           7
         /  \
        8    11 


                    
[//]
view
position
  */
  $obj->root = new Node(10);
  $obj->root->left = new Node(2);
  $obj->root->left->left = new Node(3);
  $obj->root->right = new Node(4);
  $obj->root->right->right = new Node(5);
  $obj->root->right->left = new Node(6);
  $obj->root->right->left->right = new Node(7);
  $obj->root->right->left->right->left = new Node(8);
  $obj->root->right->left->right->right = new Node(11);
  $obj->get_bottom($obj->root, 0);
  $obj->left_bottom();
  $obj->removeLists();
}
main();

Output

 3  6  8 11


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved