From c63ab86cbd89550d69d97adcf6b89303215b7301 Mon Sep 17 00:00:00 2001 From: marizvi <202051121@iiitvadodara.ac.in> Date: Wed, 6 Oct 2021 01:21:57 +0530 Subject: [PATCH] Left_view of Binary Search Tree added --- binary_search_tree/Left_View.java | 96 +++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 binary_search_tree/Left_View.java diff --git a/binary_search_tree/Left_View.java b/binary_search_tree/Left_View.java new file mode 100644 index 00000000..dde4feba --- /dev/null +++ b/binary_search_tree/Left_View.java @@ -0,0 +1,96 @@ +package Binarytree; + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.Queue; +import java.util.Scanner; + +class Node{ + int data; + Node left; + Node right; + Node(int element) + { + this.data = element; + this.left = this.right = null; + } +} + +class BST{ + Node root; + BST() + { + root = null; + } + void addNode(int element) + { + root = add(root,element); + } + Node add(Node node, int element) + { + if(node == null) + return new Node(element); + if(element<=node.data) + node.left = add(node.left,element); + else + if(element>node.data) + node.right = add(node.right,element); + + return node; + } + + void inorder(Node root) + { + if(root == null) + return; + inorder(root.left); + System.out.print(root.data+" "); + inorder(root.right); + } + + ArrayList leftView(Node node) + { + ArrayList al = new ArrayList<>(); + + Queue q = new LinkedList<>(); + q.add(node); + while(!q.isEmpty()) + { + int n = q.size(); + for(int i=1;i<=n;i++) + { + Node temp = q.poll(); + if(i==1) { + al.add(temp.data); + } + if(temp.left!=null) + q.add(temp.left); + if(temp.right!=null) + q.add(temp.right); + + } + } + return al; + } +} + +public class Left_View { + public static void main(String[] args) { + Scanner s = new Scanner(System.in); + BST obj = new BST(); + + obj.addNode(9); + obj.addNode(6); + obj.addNode(5); + obj.addNode(8); + obj.addNode(7); + obj.addNode(13); + obj.addNode(17); + obj.addNode(19); + System.out.println("Inorder of entered Tree is: "); + obj.inorder(obj.root); + ArrayList al = new ArrayList<>(); + al = obj.leftView(obj.root); + System.out.println("\nLeft View of this tree is: "+al); + } +}