From 99362e2646bbfcd5fb0e45b856c93e2a9105c9a9 Mon Sep 17 00:00:00 2001 From: Pramil <75042864+Pramil01@users.noreply.github.com> Date: Thu, 14 Oct 2021 20:03:51 +0530 Subject: [PATCH] Add Vertical Order Traversal (#2585) --- .../Trees/VerticalOrderTraversal.java | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 DataStructures/Trees/VerticalOrderTraversal.java diff --git a/DataStructures/Trees/VerticalOrderTraversal.java b/DataStructures/Trees/VerticalOrderTraversal.java new file mode 100644 index 00000000..bfbd36ca --- /dev/null +++ b/DataStructures/Trees/VerticalOrderTraversal.java @@ -0,0 +1,106 @@ +package DataStructures.Trees; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.Map; +import java.util.Queue; + +/* The following class implements a vertical order traversal +in a tree from top to bottom and left to right, so for a tree : + 1 + / \ + 2 3 + / \ \ + 4 5 6 + \ / \ + 7 8 10 + \ + 9 + the sequence will be : + 4 2 7 1 5 9 3 8 6 10 + */ +public class VerticalOrderTraversal{ + + + public static void main(String[] args) { + BinaryTree tree = new BinaryTree(); + tree.put(5); + tree.put(6); + tree.put(3); + tree.put(1); + tree.put(4); + BinaryTree.Node root = tree.getRoot(); + ArrayList ans = verticalTraversal(root); + for(int i : ans) { + System.out.print(i+" "); + } + } + + /*Function that receives a root Node and prints the tree + in Vertical Order.*/ + private static ArrayList verticalTraversal(BinaryTree.Node root) { + /*Queue to store the Nodes.*/ + Queue queue= new LinkedList<>(); + + /*Queue to store the index of particular vertical + column of a tree , with root at 0, Nodes on left + with negative index and Nodes on right with positive + index. */ + Queue index = new LinkedList<>(); + + /*Map of Integer and ArrayList to store all the + elements in a particular index in a single arrayList + that will have a key equal to the index itself. */ + Map> map = new HashMap<>(); + + /* min and max stores leftmost and right most index to + later print the tree in vertical fashion.*/ + int max =0, min =0; + queue.offer(root); + index.offer(0); + + while(!queue.isEmpty()) { + + if(queue.peek().left!=null) { + /*Adding the left Node if it is not null + and its index by subtracting 1 from it's + parent's index*/ + queue.offer(queue.peek().left); + index.offer(index.peek()-1); + } + if(queue.peek().right!=null) { + /*Adding the right Node if it is not null + and its index by adding 1 from it's + parent's index*/ + queue.offer(queue.peek().right); + index.offer(index.peek()+1); + } + /*If the map does not contains the index a new + ArrayList is created with the index as key.*/ + if(!map.containsKey(index.peek())) { + ArrayList a = new ArrayList<>(); + map.put(index.peek(), a); + } + /*For a index, corresponding Node data is added + to the respective ArrayList present at that + index. */ + map.get(index.peek()).add(queue.peek().data); + max = (int)Math.max(max,index.peek()); + min = (int)Math.min(min,index.peek()); + /*The Node and its index are removed + from their respective queues.*/ + index.poll();queue.poll(); + } + /*Finally map data is printed here which has keys + from min to max. Each ArrayList represents a + vertical column that is added in ans ArrayList.*/ + ArrayList ans= new ArrayList<>(); + for(int i =min ; i<= max ; i++) { + for(int j = 0 ; j