From 49bc4e2406b32d57e425ba35bb174b575209ce6c Mon Sep 17 00:00:00 2001 From: Pramil Kesarwani Date: Tue, 12 Oct 2021 19:36:03 +0530 Subject: [PATCH 1/7] 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/7] 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/7] 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/7] 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/7] 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 From e2fafd74a2b97b9858e612ab3787de29ba714b35 Mon Sep 17 00:00:00 2001 From: Pramil <75042864+Pramil01@users.noreply.github.com> Date: Sat, 16 Oct 2021 18:01:52 +0530 Subject: [PATCH 6/7] Create PowSum.java --- Backtracking/PowSum.java | 52 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 Backtracking/PowSum.java diff --git a/Backtracking/PowSum.java b/Backtracking/PowSum.java new file mode 100644 index 000000000000..9f5159a10878 --- /dev/null +++ b/Backtracking/PowSum.java @@ -0,0 +1,52 @@ +package Backtracking; + +import java.util.Scanner; +/* + * Problem Statement : + * Find the number of ways that a given integer, N , can be expressed as the sum of the Xth powers of unique, natural numbers. + * For example, if N=100 and X=3, we have to find all combinations of unique cubes adding up to 100. The only solution is 1^3+2^3+3^3+4^3. + * Therefore output will be 1. +*/ +public class powerSum { + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("Enter the number and the power"); + int N = sc.nextInt(); + int X = sc.nextInt(); + powerSum ps = new powerSum(); + int count = ps.powSum(N,X); + //printing the answer. + System.out.println("Number of combinations of different natural number's raised to "+X+" having sum "+N+" are : "); + System.out.println(count); + sc.close(); + } + private int count = 0,sum=0; + public int powSum(int N, int X) { + Sum(N,X,1); + return count; + } + //here i is the natural number which will be raised by X and added in sum. + public void Sum(int N, int X,int i) { + //if sum is equal to N that is one of our answer and count is increased. + if(sum == N) { + count++; + return; + } + //we will be adding next natural number raised to X only if on adding it in sum the result is less than N. + else if(sum+power(i,X)<=N) { + sum+=power(i,X); + Sum(N,X,i+1); + //backtracking and removing the number added last since no possible combination is there with it. + sum-=power(i,X); + } + if(power(i,X) Date: Sat, 16 Oct 2021 18:11:41 +0530 Subject: [PATCH 7/7] Update and rename PowSum.java to PowerSum.java --- Backtracking/{PowSum.java => PowerSum.java} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename Backtracking/{PowSum.java => PowerSum.java} (97%) diff --git a/Backtracking/PowSum.java b/Backtracking/PowerSum.java similarity index 97% rename from Backtracking/PowSum.java rename to Backtracking/PowerSum.java index 9f5159a10878..c06fa4439197 100644 --- a/Backtracking/PowSum.java +++ b/Backtracking/PowerSum.java @@ -7,14 +7,14 @@ * For example, if N=100 and X=3, we have to find all combinations of unique cubes adding up to 100. The only solution is 1^3+2^3+3^3+4^3. * Therefore output will be 1. */ -public class powerSum { +public class PowerSum { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter the number and the power"); int N = sc.nextInt(); int X = sc.nextInt(); - powerSum ps = new powerSum(); + PowerSum ps = new PowerSum(); int count = ps.powSum(N,X); //printing the answer. System.out.println("Number of combinations of different natural number's raised to "+X+" having sum "+N+" are : ");