From 49bc4e2406b32d57e425ba35bb174b575209ce6c Mon Sep 17 00:00:00 2001 From: Pramil Kesarwani Date: Tue, 12 Oct 2021 19:36:03 +0530 Subject: [PATCH 1/5] Vertical Order Traversal in a tree added --- .classpath | 19 +++ .project | 17 +++ .../Trees/VerticalOrderTraversal.java | 118 ++++++++++++++++++ 3 files changed, 154 insertions(+) create mode 100644 .classpath create mode 100644 .project create mode 100644 DataStructures/Trees/VerticalOrderTraversal.java diff --git a/.classpath b/.classpath new file mode 100644 index 000000000000..ac56de7916f0 --- /dev/null +++ b/.classpath @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + + + diff --git a/.project b/.project new file mode 100644 index 000000000000..ea54703d8788 --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + Java + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/DataStructures/Trees/VerticalOrderTraversal.java b/DataStructures/Trees/VerticalOrderTraversal.java new file mode 100644 index 000000000000..8f75f89340ee --- /dev/null +++ b/DataStructures/Trees/VerticalOrderTraversal.java @@ -0,0 +1,118 @@ +import java.util.LinkedList; +import java.util.Queue; +import java.util.Map; +import java.util.ArrayList; +import java.util.HashMap; + +/* The following class prints a tree in vertical order from + top to bottom and left to right so for a tree : + 1 + / \ + 2 3 + / \ \ + 4 5 6 + \ / \ + 7 8 10 + \ + 9 + the output will be : + 4 + 2 7 + 1 5 9 + 3 8 + 6 + 10 + */ +public class VerticalOrderTraversal{ + /* Class to represent Tree Node */ + static class Node { + int data; + Node left, right; + public Node(int data) { + this.data = data; + left = null; + right = null; + } + } + + public static void main(String[] args) { + /* Creating a tree */ + Node root = new Node(1); + root.left = new Node(2); + root.right = new Node(3); + root.left.left = new Node(4); + root.left.right = new Node(5); + root.left.left.right = new Node(7); + root.left.left.right.right = new Node(9); + root.right.right = new Node(6); + root.right.right.left = new Node(8); + root.right.right.right = new Node(10); + verticalTraversal(root); + } + + /*Function that receives a root Node and prints the tree + in Vertical Order.*/ + private static void verticalTraversal(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 printed in separate lines.*/ + for(int i =min ; i<= max ; i++) { + for(int j = 0 ; j Date: Thu, 14 Oct 2021 16:30:31 +0530 Subject: [PATCH 2/5] Delete .classpath --- .classpath | 19 ------------------- 1 file changed, 19 deletions(-) delete mode 100644 .classpath diff --git a/.classpath b/.classpath deleted file mode 100644 index ac56de7916f0..000000000000 --- a/.classpath +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - - - - - - - - - - - - - - From b370dba87857a83ee8214722f00c1265df14e8e8 Mon Sep 17 00:00:00 2001 From: Pramil <75042864+Pramil01@users.noreply.github.com> Date: Thu, 14 Oct 2021 16:32:02 +0530 Subject: [PATCH 3/5] Delete .project --- .project | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 .project diff --git a/.project b/.project deleted file mode 100644 index ea54703d8788..000000000000 --- a/.project +++ /dev/null @@ -1,17 +0,0 @@ - - - Java - - - - - - org.eclipse.jdt.core.javabuilder - - - - - - org.eclipse.jdt.core.javanature - - From c9c63a44a2e26c00c706d4f8bb19d8ff4b0b055c Mon Sep 17 00:00:00 2001 From: Pramil <75042864+Pramil01@users.noreply.github.com> Date: Thu, 14 Oct 2021 17:25:55 +0530 Subject: [PATCH 4/5] Update VerticalOrderTraversal.java Removed the Node class and used the existing binary tree class , also instead of printing the values a sequence in arraylist of ans is returned. --- .../Trees/VerticalOrderTraversal.java | 66 ++++++++----------- 1 file changed, 28 insertions(+), 38 deletions(-) diff --git a/DataStructures/Trees/VerticalOrderTraversal.java b/DataStructures/Trees/VerticalOrderTraversal.java index 8f75f89340ee..c7e599c09802 100644 --- a/DataStructures/Trees/VerticalOrderTraversal.java +++ b/DataStructures/Trees/VerticalOrderTraversal.java @@ -1,11 +1,15 @@ -import java.util.LinkedList; -import java.util.Queue; -import java.util.Map; +package DataStructures.Trees; + import java.util.ArrayList; import java.util.HashMap; +import java.util.LinkedList; +import java.util.Map; +import java.util.Queue; + +import BinaryTree.Node; -/* The following class prints a tree in vertical order from - top to bottom and left to right so for a tree : +/* 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 @@ -15,44 +19,29 @@ 7 8 10 \ 9 - the output will be : - 4 - 2 7 - 1 5 9 - 3 8 - 6 - 10 + the sequence will be : + 4 2 7 1 5 9 3 8 6 10 */ public class VerticalOrderTraversal{ - /* Class to represent Tree Node */ - static class Node { - int data; - Node left, right; - public Node(int data) { - this.data = data; - left = null; - right = null; - } - } + public static void main(String[] args) { - /* Creating a tree */ - Node root = new Node(1); - root.left = new Node(2); - root.right = new Node(3); - root.left.left = new Node(4); - root.left.right = new Node(5); - root.left.left.right = new Node(7); - root.left.left.right.right = new Node(9); - root.right.right = new Node(6); - root.right.right.left = new Node(8); - root.right.right.right = new Node(10); - verticalTraversal(root); + BinaryTree tree = new BinaryTree(); + tree.put(5); + tree.put(6); + tree.put(3); + tree.put(1); + tree.put(4); + 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 void verticalTraversal(Node root) { + private static ArrayList verticalTraversal(Node root) { /*Queue to store the Nodes.*/ Queue queue= new LinkedList<>(); @@ -107,12 +96,13 @@ private static void verticalTraversal(Node root) { } /*Finally map data is printed here which has keys from min to max. Each ArrayList represents a - vertical column that is printed in separate lines.*/ + vertical column that is added in ans ArrayList.*/ + ArrayList ans= new ArrayList<>(); for(int i =min ; i<= max ; i++) { for(int j = 0 ; j Date: Thu, 14 Oct 2021 17:35:33 +0530 Subject: [PATCH 5/5] Update VerticalOrderTraversal.java --- DataStructures/Trees/VerticalOrderTraversal.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/DataStructures/Trees/VerticalOrderTraversal.java b/DataStructures/Trees/VerticalOrderTraversal.java index c7e599c09802..bfbd36caabdb 100644 --- a/DataStructures/Trees/VerticalOrderTraversal.java +++ b/DataStructures/Trees/VerticalOrderTraversal.java @@ -6,8 +6,6 @@ import java.util.Map; import java.util.Queue; -import BinaryTree.Node; - /* The following class implements a vertical order traversal in a tree from top to bottom and left to right, so for a tree : 1 @@ -32,7 +30,7 @@ public static void main(String[] args) { tree.put(3); tree.put(1); tree.put(4); - Node root = tree.getRoot(); + BinaryTree.Node root = tree.getRoot(); ArrayList ans = verticalTraversal(root); for(int i : ans) { System.out.print(i+" "); @@ -41,9 +39,9 @@ public static void main(String[] args) { /*Function that receives a root Node and prints the tree in Vertical Order.*/ - private static ArrayList verticalTraversal(Node root) { + private static ArrayList verticalTraversal(BinaryTree.Node root) { /*Queue to store the Nodes.*/ - Queue queue= new LinkedList<>(); + Queue queue= new LinkedList<>(); /*Queue to store the index of particular vertical column of a tree , with root at 0, Nodes on left