Print Bottom-Right View of a Binary Tree

Bottom right View of a Binary Tree

Here given code implementation process.

/*
  C Program 
+ Print Bottom-Right 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 right 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)
    {
      //lagest 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)
        {
          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;
  }
}

void get_bottom(struct Node*node,struct List**bucket,int order)
{

  if(node!=NULL)
  {
    addList(bucket,order,node->data);
    get_bottom(node->left,bucket,order-1);
    get_bottom(node->right,bucket,order);
  }
}
void right_bottom(struct List*bucket)
{
  struct List*temp=bucket;
  while(temp!=NULL)
  {
    printf("%3d",temp->data );
    temp=temp->next;
  }

}
int main(){

  struct Node*root=NULL;
  struct List*bucket=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);
 
  

  get_bottom(root,&bucket,0);

  right_bottom(bucket);
  removeList(&bucket);

  return 0;
}

Output

 5 11 8
/*
  C++ Program 
+ Print Bottom-Right 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 removeList() {
    List *temp = NULL;

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

    if (node != NULL) {
      addList(level, node->data);
      get_bottom(node->left, level - 1);
      get_bottom(node->right, level);
    }
  }
  void right_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.right_bottom();
  obj.removeList();
  return 0;
}

Output

 5 11 8
/*
  Java Program 
+ Print Bottom-Right 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.left, level - 1);
      get_bottom(node.right, level);
    }
  }
  public void right_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.right_bottom();
    obj.removeList();

  }
}

Output

5 11  8
# Python3 Program
# Print Bottom-Right 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 removeList(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.left, level - 1);
      self.get_bottom(node.right, level);
    
  
  def right_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.right_bottom();
  obj.removeList();
  

if __name__ == "__main__":
  main()

Output

5 11 8
/*
  C# Program 
+ Print Bottom-Right 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.left, level - 1);
      get_bottom(node.right, level);
    }
  }
  public void right_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.right_bottom();
    obj.removeList();

  }
}

Output

 5  11  8
<?php
/*
  Php Program
  Print Bottom-Right 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->left, $level - 1);
      $this->get_bottom($node->right, $level);
    }
  }
  public function right_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->right_bottom();
  $obj->removeLists();
}
main();

Output

  5 11  8
#Ruby
#Print Bottom-Right View of a Binary Tree
class Node 
  attr_reader :data, :left, :right
  attr_accessor :data, :left, :right  
  def initialize(value) 
    @data = value
    @left = nil
    @right = nil
  end
end

class List 
  attr_reader :order, :data, :next
  attr_accessor :order, :data, :next  
  def initialize(level, value) 
    @next = nil
    @order = level
    @data = value
  end
end

class BinaryTree 
  attr_reader :root, :result
  attr_accessor :root, :result  
  def initialize() 
    @root = nil
    @result = nil
  end
  def addList(level, value) 
    new_node = nil
    back = nil
    if (@result != nil) 
      temp = @result
      if (temp.order < level) 
        new_node = List.new(level, value)
        new_node.next = @result
        @result = new_node
      else 
        status = 1
        while (temp != nil) 
          if (temp.order == level) 
            temp.data = value
            status = 0
            break
          end
          temp = temp.next
        end
        if (status == 1) 
          temp = @result
          while (temp != nil) 
            back = temp
            if (temp.order < level) 
              break
            else 
              temp = temp.next
            end
          end
          new_node = List.new(level, value)
          new_node.next = back.next
          back.next = new_node
        end
      end
    else 
      @result = List.new(level, value)
    end
  end
  def removeList() 
    temp = nil
    while (@result != nil) 
      temp = @result
      @result = temp.next
      temp = nil
    end
  end
  def get_bottom(node, level) 
    if (node != nil) 
      self.addList(level, node.data)
      self.get_bottom(node.left, level - 1)
      self.get_bottom(node.right, level)
    end
  end
  def right_bottom() 
    temp = @result
    while (temp != nil) 
      print(" ", temp.data)
      temp = temp.next
    end
  end

end
def main() 
  obj = BinaryTree.new()
  #  Make A Binary Tree
  #        10
  #       /  \
  #      2    4
  #     /    / \
  #    3    6   5
  #          \
  #           7
  #         /  \
  #        8    11
  #                  [\\]
  #                  view
  #                  position
  #   
  obj.root = Node.new(10)
  obj.root.left = Node.new(2)
  obj.root.left.left = Node.new(3)
  obj.root.right = Node.new(4)
  obj.root.right.right = Node.new(5)
  obj.root.right.left = Node.new(6)
  obj.root.right.left.right = Node.new(7)
  obj.root.right.left.right.left = Node.new(8)
  obj.root.right.left.right.right = Node.new(11)
  obj.get_bottom(obj.root, 0)
  obj.right_bottom()
  obj.removeList()
end
main()

Output

 5 11 8
/*
  Swift 4
  Print Bottom-Right View of a Binary Tree
*/
class Node {
  var data: Int;
  var left: Node? ;
  var right: Node? ;
  init(_ value: Int) {
    self.data = value;
    self.left = nil;
    self.right = nil;
  }
}
class List {
  var order: Int;
  var data: Int;
  var next: List? ;
  init(_ level: Int, _ value: Int) {
    self.next = nil;
    self.order = level;
    self.data = value;
  }
}
class BinaryTree {
  var root: Node? ;
  var result: List? ;
  init() {
    self.root = nil;
    self.result = nil;
  }
  func addList(_ level: Int, _ value: Int) {
    var new_node: List? = nil;
    var back: List? = nil;
    if (self.result != nil) {
      var temp: List? = self.result;
      if (temp!.order < level) {
        new_node = List(level, value);
        new_node!.next = self.result;
        self.result = new_node;
      } else {
        var status: Int = 1;
        while (temp != nil) {
          if (temp!.order == level) {
            temp!.data = value;
            status = 0;
            break;
          }
          temp = temp!.next;
        }
        if (status == 1) {
          temp = self.result;
          while (temp != nil) {
            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);
    }
  }
  func removeList() {
    var temp: List? = nil;
    while (self.result != nil) {
      temp = self.result;
      self.result = temp!.next;
      temp = nil;
    }
  }
  func get_bottom(_ node: Node? , _ level : Int) {
    if (node != nil) {
      self.addList(level, node!.data);
      self.get_bottom(node!.left, level - 1);
      self.get_bottom(node!.right, level);
    }
  }
  func right_bottom() {
    var temp: List? = self.result;
    while (temp != nil) {
      print(temp!.data, terminator : " ");
      temp = temp!.next;
    }
  }
}
func main() {
  let obj: BinaryTree? = BinaryTree();
  obj!.root = Node(10);
  /*
    Binary Tree
    ------------
      10
     /  \
    2    4
   /    / \
  3    6   5
        \
         7
       /  \
      8    11 


                  
                [\\]
                view
                position
  */
  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!.right_bottom();
  obj!.removeList();
}
main();

Output

5 11 8
/*
  Node Js
  Print Bottom-Right View of a Binary Tree
*/
class Node {

  constructor(value) {
    this.data = value;
    this.left = null;
    this.right = null;
  }
}
class List {

  constructor(level, value) {
    this.next = null;
    this.order = level;
    this.data = value;
  }
}
class BinaryTree {

  constructor() {
    this.root = null;
    this.result = null;
  }
  addList(level, value) {
    var new_node = null;
    var back = null;
    if (this.result != null) {
      var temp = this.result;
      if (temp.order < level) {
        new_node = new List(level, value);
        new_node.next = this.result;
        this.result = new_node;
      } else {
        var 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 List(level, value);
          new_node.next = back.next;
          back.next = new_node;
        }
      }
    } else {
      this.result = new List(level, value);
    }
  }
  removeList() {
    var temp = null;
    while (this.result != null) {
      temp = this.result;
      this.result = temp.next;
      temp = null;
    }
  }
  get_bottom(node, level) {
    if (node != null) {
      this.addList(level, node.data);
      this.get_bottom(node.left, level - 1);
      this.get_bottom(node.right, level);
    }
  }
  right_bottom() {
    var temp = this.result;
    while (temp != null) {
      process.stdout.write("  "+ temp.data);
      temp = temp.next;
    }
  }
}

function main() {
  var obj = new BinaryTree();

  /*  Make 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.right_bottom();
  obj.removeList();
}
main();

Output

 5  11  8


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