diff --git a/.gitignore b/.gitignore
index 4b8ab55..1d8c9c3 100644
--- a/.gitignore
+++ b/.gitignore
@@ -200,4 +200,17 @@
/DynamicProgramming/LongestStringChain/nbproject/private/
/DynamicProgramming/LongestStringChain/build/
/DynamicProgramming/SquareOfZeros/nbproject/private/
-/DynamicProgramming/SquareOfZeros/build/
\ No newline at end of file
+/DynamicProgramming/SquareOfZeros/build/
+/TreesAndGraphs/FindClosestValueInBST/nbproject/private/
+/TreesAndGraphs/FindClosestValueInBST/build/
+/TreesAndGraphs/FindKthLargestValueInBST/nbproject/private/
+/TreesAndGraphs/FindKthLargestValueInBST/build/
+/TreesAndGraphs/ReconstructBST/nbproject/private/
+/TreesAndGraphs/ReconstructBST/build/
+/TreesAndGraphs/ValidateThreeNodes/nbproject/private/
+/TreesAndGraphs/MinimumPassesOfMatrix/nbproject/private/
+/TreesAndGraphs/MinimumPassesOfMatrix/build/
+/TreesAndGraphs/DetectArbitrage/nbproject/private/
+/TreesAndGraphs/DetectArbitrage/build/
+/TreesAndGraphs/TwoEdgeConnectedGraph/nbproject/private/
+/TreesAndGraphs/TwoEdgeConnectedGraph/build/
\ No newline at end of file
diff --git a/TreesAndGraphs/BST/src/bst/BST.java b/TreesAndGraphs/BST/src/bst/BST.java
index c2d2e68..d18cec0 100644
--- a/TreesAndGraphs/BST/src/bst/BST.java
+++ b/TreesAndGraphs/BST/src/bst/BST.java
@@ -85,7 +85,7 @@ private void remove(int value, BST parentNode) {
// target position
currentNode.right.remove(currentNode.value, currentNode);
} else if (parentNode == null) {
- // Parentnode may be null which means it is root node and as
+ // Parentnode is null which means it is root node and as
// first condition failed that means it has only one child.
// either left or right child.
if (currentNode.left != null) {
@@ -94,8 +94,8 @@ private void remove(int value, BST parentNode) {
// node of the root node and right node (if available) will
// be the right node of the root node
currentNode.value = currentNode.left.value;
- currentNode.left = currentNode.left.left;
currentNode.right = currentNode.left.right;
+ currentNode.left = currentNode.left.left;
} else if (currentNode.right != null) {
// Same like above. As it has only right child so we will
// set immediate right childe value as root valuee and
diff --git a/TreesAndGraphs/BST/src/bst/BSTOperation.java b/TreesAndGraphs/BST/src/bst/BSTOperation.java
index 1fe05cb..09f844e 100644
--- a/TreesAndGraphs/BST/src/bst/BSTOperation.java
+++ b/TreesAndGraphs/BST/src/bst/BSTOperation.java
@@ -18,7 +18,7 @@ public static void main(String[] args) {
///nt [] values = {5, 15};
//int [] values = {5, 9, 7 , 4, 3, 11, 12, 6};
//int [] values = {5, 15, 2 , 5, 13, 22, 1, 14, 12};
- int [] values = {5, 15, 2 , 5, 13, 22, 1, 14, 12};
+ /*int [] values = {5, 15, 2 , 5, 13, 22, 1, 14, 12};
BST bst = new BST(10);
@@ -33,7 +33,11 @@ public static void main(String[] args) {
System.out.println("Contains (" + values[i] + ") : " + bst.contains(values[i]));
}
- testValidBST();
+ testValidBST();*/
+
+ BST bst = new BST(5);
+ bst.remove(10);
+ System.out.println("Contains : " + bst.contains(15));
}
public static void testValidBST() {
diff --git a/TreesAndGraphs/DetectArbitrage/build.xml b/TreesAndGraphs/DetectArbitrage/build.xml
new file mode 100644
index 0000000..76ce5a0
--- /dev/null
+++ b/TreesAndGraphs/DetectArbitrage/build.xml
@@ -0,0 +1,73 @@
+
+
+
+
+
+
+
+
+
+
+ Builds, tests, and runs the project DetectArbitrage.
+
+
+
diff --git a/TreesAndGraphs/DetectArbitrage/manifest.mf b/TreesAndGraphs/DetectArbitrage/manifest.mf
new file mode 100644
index 0000000..328e8e5
--- /dev/null
+++ b/TreesAndGraphs/DetectArbitrage/manifest.mf
@@ -0,0 +1,3 @@
+Manifest-Version: 1.0
+X-COMMENT: Main-Class will be added automatically by build
+
diff --git a/TreesAndGraphs/DetectArbitrage/nbproject/build-impl.xml b/TreesAndGraphs/DetectArbitrage/nbproject/build-impl.xml
new file mode 100644
index 0000000..ed9a2f3
--- /dev/null
+++ b/TreesAndGraphs/DetectArbitrage/nbproject/build-impl.xml
@@ -0,0 +1,1420 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must set src.dir
+ Must set test.src.dir
+ Must set build.dir
+ Must set dist.dir
+ Must set build.classes.dir
+ Must set dist.javadoc.dir
+ Must set build.test.classes.dir
+ Must set build.test.results.dir
+ Must set build.classes.excludes
+ Must set dist.jar
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must set javac.includes
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ No tests executed.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must set JVM to use for profiling in profiler.info.jvm
+ Must set profiler agent JVM arguments in profiler.info.jvmargs.agent
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must select some files in the IDE or set javac.includes
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ To run this application from the command line without Ant, try:
+
+ java -jar "${dist.jar.resolved}"
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must select one file in the IDE or set run.class
+
+
+
+ Must select one file in the IDE or set run.class
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must select one file in the IDE or set debug.class
+
+
+
+
+ Must select one file in the IDE or set debug.class
+
+
+
+
+ Must set fix.includes
+
+
+
+
+
+
+
+
+
+ This target only works when run from inside the NetBeans IDE.
+
+
+
+
+
+
+
+
+ Must select one file in the IDE or set profile.class
+ This target only works when run from inside the NetBeans IDE.
+
+
+
+
+
+
+
+
+ This target only works when run from inside the NetBeans IDE.
+
+
+
+
+
+
+
+
+
+
+
+
+ This target only works when run from inside the NetBeans IDE.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must select one file in the IDE or set run.class
+
+
+
+
+
+ Must select some files in the IDE or set test.includes
+
+
+
+
+ Must select one file in the IDE or set run.class
+
+
+
+
+ Must select one file in the IDE or set applet.url
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must select some files in the IDE or set javac.includes
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Some tests failed; see details above.
+
+
+
+
+
+
+
+
+ Must select some files in the IDE or set test.includes
+
+
+
+ Some tests failed; see details above.
+
+
+
+ Must select some files in the IDE or set test.class
+ Must select some method in the IDE or set test.method
+
+
+
+ Some tests failed; see details above.
+
+
+
+
+ Must select one file in the IDE or set test.class
+
+
+
+ Must select one file in the IDE or set test.class
+ Must select some method in the IDE or set test.method
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must select one file in the IDE or set applet.url
+
+
+
+
+
+
+
+
+ Must select one file in the IDE or set applet.url
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/TreesAndGraphs/DetectArbitrage/nbproject/genfiles.properties b/TreesAndGraphs/DetectArbitrage/nbproject/genfiles.properties
new file mode 100644
index 0000000..ec6376d
--- /dev/null
+++ b/TreesAndGraphs/DetectArbitrage/nbproject/genfiles.properties
@@ -0,0 +1,8 @@
+build.xml.data.CRC32=5c9b0e83
+build.xml.script.CRC32=ed63f06d
+build.xml.stylesheet.CRC32=8064a381@1.80.1.48
+# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
+# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
+nbproject/build-impl.xml.data.CRC32=5c9b0e83
+nbproject/build-impl.xml.script.CRC32=479e2f38
+nbproject/build-impl.xml.stylesheet.CRC32=830a3534@1.80.1.48
diff --git a/TreesAndGraphs/DetectArbitrage/nbproject/project.properties b/TreesAndGraphs/DetectArbitrage/nbproject/project.properties
new file mode 100644
index 0000000..a29faa6
--- /dev/null
+++ b/TreesAndGraphs/DetectArbitrage/nbproject/project.properties
@@ -0,0 +1,74 @@
+annotation.processing.enabled=true
+annotation.processing.enabled.in.editor=false
+annotation.processing.processor.options=
+annotation.processing.processors.list=
+annotation.processing.run.all.processors=true
+annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output
+build.classes.dir=${build.dir}/classes
+build.classes.excludes=**/*.java,**/*.form
+# This directory is removed when the project is cleaned:
+build.dir=build
+build.generated.dir=${build.dir}/generated
+build.generated.sources.dir=${build.dir}/generated-sources
+# Only compile against the classpath explicitly listed here:
+build.sysclasspath=ignore
+build.test.classes.dir=${build.dir}/test/classes
+build.test.results.dir=${build.dir}/test/results
+# Uncomment to specify the preferred debugger connection transport:
+#debug.transport=dt_socket
+debug.classpath=\
+ ${run.classpath}
+debug.test.classpath=\
+ ${run.test.classpath}
+# Files in build.classes.dir which should be excluded from distribution jar
+dist.archive.excludes=
+# This directory is removed when the project is cleaned:
+dist.dir=dist
+dist.jar=${dist.dir}/DetectArbitrage.jar
+dist.javadoc.dir=${dist.dir}/javadoc
+excludes=
+includes=**
+jar.compress=false
+javac.classpath=
+# Space-separated list of extra javac options
+javac.compilerargs=
+javac.deprecation=false
+javac.external.vm=true
+javac.processorpath=\
+ ${javac.classpath}
+javac.source=1.8
+javac.target=1.8
+javac.test.classpath=\
+ ${javac.classpath}:\
+ ${build.classes.dir}
+javac.test.processorpath=\
+ ${javac.test.classpath}
+javadoc.additionalparam=
+javadoc.author=false
+javadoc.encoding=${source.encoding}
+javadoc.noindex=false
+javadoc.nonavbar=false
+javadoc.notree=false
+javadoc.private=false
+javadoc.splitindex=true
+javadoc.use=true
+javadoc.version=false
+javadoc.windowtitle=
+main.class=detectarbitrage.DetectArbitrage
+manifest.file=manifest.mf
+meta.inf.dir=${src.dir}/META-INF
+mkdist.disabled=false
+platform.active=default_platform
+run.classpath=\
+ ${javac.classpath}:\
+ ${build.classes.dir}
+# Space-separated list of JVM arguments used when running the project.
+# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value.
+# To set system properties for unit tests define test-sys-prop.name=value:
+run.jvmargs=
+run.test.classpath=\
+ ${javac.test.classpath}:\
+ ${build.test.classes.dir}
+source.encoding=UTF-8
+src.dir=src
+test.src.dir=test
diff --git a/TreesAndGraphs/DetectArbitrage/nbproject/project.xml b/TreesAndGraphs/DetectArbitrage/nbproject/project.xml
new file mode 100644
index 0000000..409a0ef
--- /dev/null
+++ b/TreesAndGraphs/DetectArbitrage/nbproject/project.xml
@@ -0,0 +1,15 @@
+
+
+ org.netbeans.modules.java.j2seproject
+
+
+ DetectArbitrage
+
+
+
+
+
+
+
+
+
diff --git a/TreesAndGraphs/DetectArbitrage/src/detectarbitrage/DetectArbitrage.java b/TreesAndGraphs/DetectArbitrage/src/detectarbitrage/DetectArbitrage.java
new file mode 100644
index 0000000..d100d2d
--- /dev/null
+++ b/TreesAndGraphs/DetectArbitrage/src/detectarbitrage/DetectArbitrage.java
@@ -0,0 +1,91 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package detectarbitrage;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+
+/**
+ * Time Complexity: O(n^3) because n^2 edges and n loop for bellman ford algo
+ * Space Complexity: O(n^2) because of new input array. If we are allow to
+ * modify the input array then space complexity will be O(n) because of
+ * distance array
+ * @author souravpalit
+ */
+public class DetectArbitrage {
+
+ /**
+ * @param args the command line arguments
+ */
+ public static void main(String[] args) {
+ ArrayList> input = new ArrayList>();
+ input.add(new ArrayList(Arrays.asList(1.0, 0.8631, 0.5903)));
+ input.add(new ArrayList(Arrays.asList(1.1586, 1.0, 0.6849)));
+ input.add(new ArrayList(Arrays.asList(1.6939, 1.46, 1.0)));
+
+ System.out.println("Has Arbitrag : " + detectArbitrage(input));
+ }
+
+ public static boolean detectArbitrage(ArrayList> exchangeRates) {
+ ArrayList> logExchangeRates =
+ convertToLogExchangeRates(exchangeRates);
+ return hasNegativeWeightCycle(logExchangeRates);
+ }
+
+ public static boolean hasNegativeWeightCycle(ArrayList> graph) {
+ double [] distancesFromStart = new double [graph.size()];
+ Arrays.fill(distancesFromStart, Double.MAX_VALUE);
+ distancesFromStart[0] = 0; // As starting node to starting node 0 distance
+
+ for (int i = 0; i < graph.size(); i++) {
+ if (!relaxAndUpdateDistance(graph, distancesFromStart)) {
+ // No changes that means no negative cycle and we could stop
+ // as no negative cycle means no aribrage possiblee
+ return false;
+ }
+ }
+
+ return relaxAndUpdateDistance(graph, distancesFromStart);
+ }
+
+ public static ArrayList> convertToLogExchangeRates(
+ ArrayList> exchangeRates
+ ) {
+ ArrayList> logExchangeRates =
+ new ArrayList>();
+
+ for (int i = 0; i < exchangeRates.size(); i++) {
+ ArrayList rates = new ArrayList();
+ for (int j = 0; j < exchangeRates.get(i).size(); j++) {
+ rates.add(-Math.log10(exchangeRates.get(i).get(j)));
+ }
+ logExchangeRates.add(rates);
+ }
+
+ return logExchangeRates;
+ }
+
+ public static boolean relaxAndUpdateDistance(
+ ArrayList> graph,
+ double [] distancesFromStart
+ ) {
+ boolean isUpdated = false;
+
+ for (int sourceIdx = 0; sourceIdx < graph.size(); sourceIdx++) {
+ ArrayList edges = graph.get(sourceIdx);
+ for (int destinationIdx = 0; destinationIdx < edges.size(); destinationIdx++) {
+ double newDistance = distancesFromStart[sourceIdx] + edges.get(destinationIdx);
+ if (newDistance < distancesFromStart[destinationIdx]) {
+ isUpdated = true;
+ distancesFromStart[destinationIdx] = newDistance;
+ }
+ }
+ }
+
+ return isUpdated;
+ }
+
+}
diff --git a/TreesAndGraphs/FindClosestValueInBST/build.xml b/TreesAndGraphs/FindClosestValueInBST/build.xml
new file mode 100644
index 0000000..d3fc9ef
--- /dev/null
+++ b/TreesAndGraphs/FindClosestValueInBST/build.xml
@@ -0,0 +1,73 @@
+
+
+
+
+
+
+
+
+
+
+ Builds, tests, and runs the project FindClosestValueInBST.
+
+
+
diff --git a/TreesAndGraphs/FindClosestValueInBST/manifest.mf b/TreesAndGraphs/FindClosestValueInBST/manifest.mf
new file mode 100644
index 0000000..328e8e5
--- /dev/null
+++ b/TreesAndGraphs/FindClosestValueInBST/manifest.mf
@@ -0,0 +1,3 @@
+Manifest-Version: 1.0
+X-COMMENT: Main-Class will be added automatically by build
+
diff --git a/TreesAndGraphs/FindClosestValueInBST/nbproject/build-impl.xml b/TreesAndGraphs/FindClosestValueInBST/nbproject/build-impl.xml
new file mode 100644
index 0000000..7937117
--- /dev/null
+++ b/TreesAndGraphs/FindClosestValueInBST/nbproject/build-impl.xml
@@ -0,0 +1,1420 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must set src.dir
+ Must set test.src.dir
+ Must set build.dir
+ Must set dist.dir
+ Must set build.classes.dir
+ Must set dist.javadoc.dir
+ Must set build.test.classes.dir
+ Must set build.test.results.dir
+ Must set build.classes.excludes
+ Must set dist.jar
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must set javac.includes
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ No tests executed.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must set JVM to use for profiling in profiler.info.jvm
+ Must set profiler agent JVM arguments in profiler.info.jvmargs.agent
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must select some files in the IDE or set javac.includes
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ To run this application from the command line without Ant, try:
+
+ java -jar "${dist.jar.resolved}"
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must select one file in the IDE or set run.class
+
+
+
+ Must select one file in the IDE or set run.class
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must select one file in the IDE or set debug.class
+
+
+
+
+ Must select one file in the IDE or set debug.class
+
+
+
+
+ Must set fix.includes
+
+
+
+
+
+
+
+
+
+ This target only works when run from inside the NetBeans IDE.
+
+
+
+
+
+
+
+
+ Must select one file in the IDE or set profile.class
+ This target only works when run from inside the NetBeans IDE.
+
+
+
+
+
+
+
+
+ This target only works when run from inside the NetBeans IDE.
+
+
+
+
+
+
+
+
+
+
+
+
+ This target only works when run from inside the NetBeans IDE.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must select one file in the IDE or set run.class
+
+
+
+
+
+ Must select some files in the IDE or set test.includes
+
+
+
+
+ Must select one file in the IDE or set run.class
+
+
+
+
+ Must select one file in the IDE or set applet.url
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must select some files in the IDE or set javac.includes
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Some tests failed; see details above.
+
+
+
+
+
+
+
+
+ Must select some files in the IDE or set test.includes
+
+
+
+ Some tests failed; see details above.
+
+
+
+ Must select some files in the IDE or set test.class
+ Must select some method in the IDE or set test.method
+
+
+
+ Some tests failed; see details above.
+
+
+
+
+ Must select one file in the IDE or set test.class
+
+
+
+ Must select one file in the IDE or set test.class
+ Must select some method in the IDE or set test.method
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must select one file in the IDE or set applet.url
+
+
+
+
+
+
+
+
+ Must select one file in the IDE or set applet.url
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/TreesAndGraphs/FindClosestValueInBST/nbproject/genfiles.properties b/TreesAndGraphs/FindClosestValueInBST/nbproject/genfiles.properties
new file mode 100644
index 0000000..e0fda5a
--- /dev/null
+++ b/TreesAndGraphs/FindClosestValueInBST/nbproject/genfiles.properties
@@ -0,0 +1,8 @@
+build.xml.data.CRC32=1176ff1d
+build.xml.script.CRC32=2930e3f1
+build.xml.stylesheet.CRC32=8064a381@1.80.1.48
+# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
+# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
+nbproject/build-impl.xml.data.CRC32=1176ff1d
+nbproject/build-impl.xml.script.CRC32=7d667b30
+nbproject/build-impl.xml.stylesheet.CRC32=830a3534@1.80.1.48
diff --git a/TreesAndGraphs/FindClosestValueInBST/nbproject/project.properties b/TreesAndGraphs/FindClosestValueInBST/nbproject/project.properties
new file mode 100644
index 0000000..85f41d0
--- /dev/null
+++ b/TreesAndGraphs/FindClosestValueInBST/nbproject/project.properties
@@ -0,0 +1,74 @@
+annotation.processing.enabled=true
+annotation.processing.enabled.in.editor=false
+annotation.processing.processor.options=
+annotation.processing.processors.list=
+annotation.processing.run.all.processors=true
+annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output
+build.classes.dir=${build.dir}/classes
+build.classes.excludes=**/*.java,**/*.form
+# This directory is removed when the project is cleaned:
+build.dir=build
+build.generated.dir=${build.dir}/generated
+build.generated.sources.dir=${build.dir}/generated-sources
+# Only compile against the classpath explicitly listed here:
+build.sysclasspath=ignore
+build.test.classes.dir=${build.dir}/test/classes
+build.test.results.dir=${build.dir}/test/results
+# Uncomment to specify the preferred debugger connection transport:
+#debug.transport=dt_socket
+debug.classpath=\
+ ${run.classpath}
+debug.test.classpath=\
+ ${run.test.classpath}
+# Files in build.classes.dir which should be excluded from distribution jar
+dist.archive.excludes=
+# This directory is removed when the project is cleaned:
+dist.dir=dist
+dist.jar=${dist.dir}/FindClosestValueInBST.jar
+dist.javadoc.dir=${dist.dir}/javadoc
+excludes=
+includes=**
+jar.compress=false
+javac.classpath=
+# Space-separated list of extra javac options
+javac.compilerargs=
+javac.deprecation=false
+javac.external.vm=true
+javac.processorpath=\
+ ${javac.classpath}
+javac.source=1.8
+javac.target=1.8
+javac.test.classpath=\
+ ${javac.classpath}:\
+ ${build.classes.dir}
+javac.test.processorpath=\
+ ${javac.test.classpath}
+javadoc.additionalparam=
+javadoc.author=false
+javadoc.encoding=${source.encoding}
+javadoc.noindex=false
+javadoc.nonavbar=false
+javadoc.notree=false
+javadoc.private=false
+javadoc.splitindex=true
+javadoc.use=true
+javadoc.version=false
+javadoc.windowtitle=
+main.class=findclosestvalueinbst.FindClosestValueInBST
+manifest.file=manifest.mf
+meta.inf.dir=${src.dir}/META-INF
+mkdist.disabled=false
+platform.active=default_platform
+run.classpath=\
+ ${javac.classpath}:\
+ ${build.classes.dir}
+# Space-separated list of JVM arguments used when running the project.
+# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value.
+# To set system properties for unit tests define test-sys-prop.name=value:
+run.jvmargs=
+run.test.classpath=\
+ ${javac.test.classpath}:\
+ ${build.test.classes.dir}
+source.encoding=UTF-8
+src.dir=src
+test.src.dir=test
diff --git a/TreesAndGraphs/FindClosestValueInBST/nbproject/project.xml b/TreesAndGraphs/FindClosestValueInBST/nbproject/project.xml
new file mode 100644
index 0000000..1b7a69f
--- /dev/null
+++ b/TreesAndGraphs/FindClosestValueInBST/nbproject/project.xml
@@ -0,0 +1,15 @@
+
+
+ org.netbeans.modules.java.j2seproject
+
+
+ FindClosestValueInBST
+
+
+
+
+
+
+
+
+
diff --git a/TreesAndGraphs/FindClosestValueInBST/src/findclosestvalueinbst/BST.java b/TreesAndGraphs/FindClosestValueInBST/src/findclosestvalueinbst/BST.java
new file mode 100644
index 0000000..e7778cf
--- /dev/null
+++ b/TreesAndGraphs/FindClosestValueInBST/src/findclosestvalueinbst/BST.java
@@ -0,0 +1,20 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package findclosestvalueinbst;
+
+/**
+ *
+ * @author souravpalit
+ */
+public class BST {
+ int value;
+ BST left;
+ BST right;
+
+ public BST(int value) {
+ this.value = value;
+ }
+}
diff --git a/TreesAndGraphs/FindClosestValueInBST/src/findclosestvalueinbst/FindClosestValueInBST.java b/TreesAndGraphs/FindClosestValueInBST/src/findclosestvalueinbst/FindClosestValueInBST.java
new file mode 100644
index 0000000..e0bfacf
--- /dev/null
+++ b/TreesAndGraphs/FindClosestValueInBST/src/findclosestvalueinbst/FindClosestValueInBST.java
@@ -0,0 +1,58 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package findclosestvalueinbst;
+
+/**
+ * Time Complexity: Average Case O(logn) Space Complexity: O(logn)
+ * Worst Case O(n) Space Complexity: O(n) When tree is only left aligned or
+ * only right aligned
+ * @author souravpalit
+ */
+public class FindClosestValueInBST {
+
+ /**
+ * @param args the command line arguments
+ */
+ public static void main(String[] args) {
+ BST tree = generateSampleBST();
+ int target = 12;
+
+ System.out.println(findClosestValueInBst(tree, target));
+ }
+
+ public static int findClosestValueInBst(BST tree, int target) {
+ // Sending root as closest at first
+ return findClosestValueInBst(tree, target, tree.value);
+ }
+
+ public static int findClosestValueInBst(BST tree, int target, int closest) {
+ if (Math.abs(tree.value - target) < Math.abs(closest - target)) {
+ closest = tree.value;
+ }
+
+ if (tree.value > target && tree.left != null) {
+ closest = findClosestValueInBst(tree.left, target, closest);
+ } else if (tree.value < target && tree.right != null){
+ closest = findClosestValueInBst(tree.right, target, closest);
+ }
+
+ return closest;
+ }
+
+ public static BST generateSampleBST() {
+ BST root = new BST(10);
+ root.left = new BST(5);
+ root.left.left = new BST(2);
+ root.left.left.left = new BST(1);
+ root.left.right = new BST(5);
+ root.right = new BST(15);
+ root.right.left = new BST(13);
+ root.right.left.right = new BST(14);
+ root.right.right = new BST(22);
+
+ return root;
+ }
+}
diff --git a/TreesAndGraphs/FindClosestValueInBST/src/findclosestvalueinbst/FindClosestValueInBSTIteration.java b/TreesAndGraphs/FindClosestValueInBST/src/findclosestvalueinbst/FindClosestValueInBSTIteration.java
new file mode 100644
index 0000000..bb9b8bd
--- /dev/null
+++ b/TreesAndGraphs/FindClosestValueInBST/src/findclosestvalueinbst/FindClosestValueInBSTIteration.java
@@ -0,0 +1,73 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package findclosestvalueinbst;
+
+/**
+ * Time Complexity: Average Case O(logn) Space Complexity: O(1)
+ * Worst Case O(n) Space Complexity: O(1) When tree is only left aligned or
+ * only right aligned
+ * @author souravpalit
+ */
+public class FindClosestValueInBSTIteration {
+
+ /**
+ * @param args the command line arguments
+ */
+ public static void main(String[] args) {
+ BST tree = generateSampleBST();
+ int target = 12;
+
+ System.out.println(findClosestValueInBst(tree, target));
+ }
+
+ public static int findClosestValueInBst(BST tree, int target) {
+ int closest = tree.value;
+ while (tree != null) {
+ if (Math.abs(tree.value - target) < Math.abs(closest - target)) {
+ closest = tree.value;
+ }
+
+ if (tree.value > target) {
+ tree = tree.left;
+ } else if (tree.value < target) {
+ tree = tree.right;
+ } else {
+ // In case same value
+ break;
+ }
+ }
+
+ return closest;
+ }
+
+ public static int findClosestValueInBst(BST tree, int target, int closest) {
+ if (Math.abs(tree.value - target) < Math.abs(closest - target)) {
+ closest = tree.value;
+ }
+
+ if (tree.value > target && tree.left != null) {
+ closest = findClosestValueInBst(tree.left, target, closest);
+ } else if (tree.value < target && tree.right != null){
+ closest = findClosestValueInBst(tree.right, target, closest);
+ }
+
+ return closest;
+ }
+
+ public static BST generateSampleBST() {
+ BST root = new BST(10);
+ root.left = new BST(5);
+ root.left.left = new BST(2);
+ root.left.left.left = new BST(1);
+ root.left.right = new BST(5);
+ root.right = new BST(15);
+ root.right.left = new BST(13);
+ root.right.left.right = new BST(14);
+ root.right.right = new BST(22);
+
+ return root;
+ }
+}
diff --git a/TreesAndGraphs/FindKthLargestValueInBST/build.xml b/TreesAndGraphs/FindKthLargestValueInBST/build.xml
new file mode 100644
index 0000000..390630f
--- /dev/null
+++ b/TreesAndGraphs/FindKthLargestValueInBST/build.xml
@@ -0,0 +1,73 @@
+
+
+
+
+
+
+
+
+
+
+ Builds, tests, and runs the project FindKthLargestValueInBST.
+
+
+
diff --git a/TreesAndGraphs/FindKthLargestValueInBST/manifest.mf b/TreesAndGraphs/FindKthLargestValueInBST/manifest.mf
new file mode 100644
index 0000000..328e8e5
--- /dev/null
+++ b/TreesAndGraphs/FindKthLargestValueInBST/manifest.mf
@@ -0,0 +1,3 @@
+Manifest-Version: 1.0
+X-COMMENT: Main-Class will be added automatically by build
+
diff --git a/TreesAndGraphs/FindKthLargestValueInBST/nbproject/build-impl.xml b/TreesAndGraphs/FindKthLargestValueInBST/nbproject/build-impl.xml
new file mode 100644
index 0000000..ea3b008
--- /dev/null
+++ b/TreesAndGraphs/FindKthLargestValueInBST/nbproject/build-impl.xml
@@ -0,0 +1,1420 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must set src.dir
+ Must set test.src.dir
+ Must set build.dir
+ Must set dist.dir
+ Must set build.classes.dir
+ Must set dist.javadoc.dir
+ Must set build.test.classes.dir
+ Must set build.test.results.dir
+ Must set build.classes.excludes
+ Must set dist.jar
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must set javac.includes
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ No tests executed.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must set JVM to use for profiling in profiler.info.jvm
+ Must set profiler agent JVM arguments in profiler.info.jvmargs.agent
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must select some files in the IDE or set javac.includes
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ To run this application from the command line without Ant, try:
+
+ java -jar "${dist.jar.resolved}"
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must select one file in the IDE or set run.class
+
+
+
+ Must select one file in the IDE or set run.class
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must select one file in the IDE or set debug.class
+
+
+
+
+ Must select one file in the IDE or set debug.class
+
+
+
+
+ Must set fix.includes
+
+
+
+
+
+
+
+
+
+ This target only works when run from inside the NetBeans IDE.
+
+
+
+
+
+
+
+
+ Must select one file in the IDE or set profile.class
+ This target only works when run from inside the NetBeans IDE.
+
+
+
+
+
+
+
+
+ This target only works when run from inside the NetBeans IDE.
+
+
+
+
+
+
+
+
+
+
+
+
+ This target only works when run from inside the NetBeans IDE.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must select one file in the IDE or set run.class
+
+
+
+
+
+ Must select some files in the IDE or set test.includes
+
+
+
+
+ Must select one file in the IDE or set run.class
+
+
+
+
+ Must select one file in the IDE or set applet.url
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must select some files in the IDE or set javac.includes
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Some tests failed; see details above.
+
+
+
+
+
+
+
+
+ Must select some files in the IDE or set test.includes
+
+
+
+ Some tests failed; see details above.
+
+
+
+ Must select some files in the IDE or set test.class
+ Must select some method in the IDE or set test.method
+
+
+
+ Some tests failed; see details above.
+
+
+
+
+ Must select one file in the IDE or set test.class
+
+
+
+ Must select one file in the IDE or set test.class
+ Must select some method in the IDE or set test.method
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must select one file in the IDE or set applet.url
+
+
+
+
+
+
+
+
+ Must select one file in the IDE or set applet.url
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/TreesAndGraphs/FindKthLargestValueInBST/nbproject/genfiles.properties b/TreesAndGraphs/FindKthLargestValueInBST/nbproject/genfiles.properties
new file mode 100644
index 0000000..d92f691
--- /dev/null
+++ b/TreesAndGraphs/FindKthLargestValueInBST/nbproject/genfiles.properties
@@ -0,0 +1,8 @@
+build.xml.data.CRC32=18e40d36
+build.xml.script.CRC32=66655559
+build.xml.stylesheet.CRC32=8064a381@1.80.1.48
+# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
+# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
+nbproject/build-impl.xml.data.CRC32=18e40d36
+nbproject/build-impl.xml.script.CRC32=04f358c0
+nbproject/build-impl.xml.stylesheet.CRC32=830a3534@1.80.1.48
diff --git a/TreesAndGraphs/FindKthLargestValueInBST/nbproject/project.properties b/TreesAndGraphs/FindKthLargestValueInBST/nbproject/project.properties
new file mode 100644
index 0000000..0ca52f5
--- /dev/null
+++ b/TreesAndGraphs/FindKthLargestValueInBST/nbproject/project.properties
@@ -0,0 +1,74 @@
+annotation.processing.enabled=true
+annotation.processing.enabled.in.editor=false
+annotation.processing.processor.options=
+annotation.processing.processors.list=
+annotation.processing.run.all.processors=true
+annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output
+build.classes.dir=${build.dir}/classes
+build.classes.excludes=**/*.java,**/*.form
+# This directory is removed when the project is cleaned:
+build.dir=build
+build.generated.dir=${build.dir}/generated
+build.generated.sources.dir=${build.dir}/generated-sources
+# Only compile against the classpath explicitly listed here:
+build.sysclasspath=ignore
+build.test.classes.dir=${build.dir}/test/classes
+build.test.results.dir=${build.dir}/test/results
+# Uncomment to specify the preferred debugger connection transport:
+#debug.transport=dt_socket
+debug.classpath=\
+ ${run.classpath}
+debug.test.classpath=\
+ ${run.test.classpath}
+# Files in build.classes.dir which should be excluded from distribution jar
+dist.archive.excludes=
+# This directory is removed when the project is cleaned:
+dist.dir=dist
+dist.jar=${dist.dir}/FindKthLargestValueInBST.jar
+dist.javadoc.dir=${dist.dir}/javadoc
+excludes=
+includes=**
+jar.compress=false
+javac.classpath=
+# Space-separated list of extra javac options
+javac.compilerargs=
+javac.deprecation=false
+javac.external.vm=true
+javac.processorpath=\
+ ${javac.classpath}
+javac.source=1.8
+javac.target=1.8
+javac.test.classpath=\
+ ${javac.classpath}:\
+ ${build.classes.dir}
+javac.test.processorpath=\
+ ${javac.test.classpath}
+javadoc.additionalparam=
+javadoc.author=false
+javadoc.encoding=${source.encoding}
+javadoc.noindex=false
+javadoc.nonavbar=false
+javadoc.notree=false
+javadoc.private=false
+javadoc.splitindex=true
+javadoc.use=true
+javadoc.version=false
+javadoc.windowtitle=
+main.class=findkthlargestvalueinbst.FindKthLargestValueInBST
+manifest.file=manifest.mf
+meta.inf.dir=${src.dir}/META-INF
+mkdist.disabled=false
+platform.active=default_platform
+run.classpath=\
+ ${javac.classpath}:\
+ ${build.classes.dir}
+# Space-separated list of JVM arguments used when running the project.
+# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value.
+# To set system properties for unit tests define test-sys-prop.name=value:
+run.jvmargs=
+run.test.classpath=\
+ ${javac.test.classpath}:\
+ ${build.test.classes.dir}
+source.encoding=UTF-8
+src.dir=src
+test.src.dir=test
diff --git a/TreesAndGraphs/FindKthLargestValueInBST/nbproject/project.xml b/TreesAndGraphs/FindKthLargestValueInBST/nbproject/project.xml
new file mode 100644
index 0000000..668b5dc
--- /dev/null
+++ b/TreesAndGraphs/FindKthLargestValueInBST/nbproject/project.xml
@@ -0,0 +1,15 @@
+
+
+ org.netbeans.modules.java.j2seproject
+
+
+ FindKthLargestValueInBST
+
+
+
+
+
+
+
+
+
diff --git a/TreesAndGraphs/FindKthLargestValueInBST/src/findkthlargestvalueinbst/BST.java b/TreesAndGraphs/FindKthLargestValueInBST/src/findkthlargestvalueinbst/BST.java
new file mode 100644
index 0000000..772c344
--- /dev/null
+++ b/TreesAndGraphs/FindKthLargestValueInBST/src/findkthlargestvalueinbst/BST.java
@@ -0,0 +1,20 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package findkthlargestvalueinbst;
+
+/**
+ *
+ * @author souravpalit
+ */
+public class BST {
+ int value;
+ BST left;
+ BST right;
+
+ public BST(int value) {
+ this.value = value;
+ }
+}
diff --git a/TreesAndGraphs/FindKthLargestValueInBST/src/findkthlargestvalueinbst/FindKthLargestValueInBST.java b/TreesAndGraphs/FindKthLargestValueInBST/src/findkthlargestvalueinbst/FindKthLargestValueInBST.java
new file mode 100644
index 0000000..0407d27
--- /dev/null
+++ b/TreesAndGraphs/FindKthLargestValueInBST/src/findkthlargestvalueinbst/FindKthLargestValueInBST.java
@@ -0,0 +1,51 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package findkthlargestvalueinbst;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Time Complexity: O(n) as we need to traverse all node
+ * Space Complexity: O(n) because of storing all the sorted value
+ * @author souravpalit
+ */
+public class FindKthLargestValueInBST {
+
+ /**
+ * @param args the command line arguments
+ */
+ public static void main(String[] args) {
+ BST root = new BST(10);
+ root.left = new BST(5);
+ root.left.left = new BST(2);
+ root.left.left.left = new BST(1);
+ root.left.right = new BST(5);
+ root.right = new BST(15);
+ root.right.left = new BST(13);
+ root.right.left.right = new BST(14);
+ root.right.right = new BST(22);
+
+ int k = 3;
+
+ System.out.println("Thee Kth Largest value is : " + findKthLargestValueInBst(root, k));
+ }
+
+ public static int findKthLargestValueInBst(BST tree, int k) {
+ List sortedList = new ArrayList();
+ inOrderTraverse(tree, sortedList);
+ return sortedList.get(sortedList.size() - k);
+ }
+
+ public static void inOrderTraverse(BST tree, List list) {
+ if (tree == null) return;
+
+ inOrderTraverse(tree.left, list);
+ list.add(tree.value);
+ inOrderTraverse(tree.right, list);
+ }
+
+}
diff --git a/TreesAndGraphs/FindKthLargestValueInBST/src/findkthlargestvalueinbst/FindKthLargestValueInBSTOptimized.java b/TreesAndGraphs/FindKthLargestValueInBST/src/findkthlargestvalueinbst/FindKthLargestValueInBSTOptimized.java
new file mode 100644
index 0000000..bd3954e
--- /dev/null
+++ b/TreesAndGraphs/FindKthLargestValueInBST/src/findkthlargestvalueinbst/FindKthLargestValueInBSTOptimized.java
@@ -0,0 +1,64 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package findkthlargestvalueinbst;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Time Complexity: O(h + k)
+ * Space Complexity: O(h)
+ * @author souravpalit
+ */
+public class FindKthLargestValueInBSTOptimized {
+
+ /**
+ * @param args the command line arguments
+ */
+ public static void main(String[] args) {
+ BST root = new BST(10);
+ root.left = new BST(5);
+ root.left.left = new BST(2);
+ root.left.left.left = new BST(1);
+ root.left.right = new BST(5);
+ root.right = new BST(15);
+ root.right.left = new BST(13);
+ root.right.left.right = new BST(14);
+ root.right.right = new BST(22);
+
+ int k = 3;
+
+ System.out.println("Thee Kth Largest value is : " + findKthLargestValueInBst(root, k));
+ }
+
+ public static int findKthLargestValueInBst(BST tree, int k) {
+ TreeInfo treeInfo = new TreeInfo(0, -1);
+ reverseInOrderTraverse(tree, k, treeInfo);
+ return treeInfo.lastVisitedNodeValue;
+ }
+
+ public static void reverseInOrderTraverse(BST tree, int k, TreeInfo treeInfo) {
+ if (tree == null || treeInfo.visitedNodeCount >= k) return;
+
+ reverseInOrderTraverse(tree.right, k, treeInfo);
+ if (treeInfo.visitedNodeCount < k) {
+ treeInfo.lastVisitedNodeValue = tree.value;
+ treeInfo.visitedNodeCount++;
+ reverseInOrderTraverse(tree.left, k, treeInfo);
+ }
+ }
+
+ public static class TreeInfo {
+ int visitedNodeCount;
+ int lastVisitedNodeValue;
+
+ public TreeInfo(int visitedNodeCount, int lastVisitedNodeValue) {
+ this.visitedNodeCount = visitedNodeCount;
+ this.lastVisitedNodeValue = lastVisitedNodeValue;
+ }
+ }
+
+}
diff --git a/TreesAndGraphs/MinimumPassesOfMatrix/build.xml b/TreesAndGraphs/MinimumPassesOfMatrix/build.xml
new file mode 100644
index 0000000..1791688
--- /dev/null
+++ b/TreesAndGraphs/MinimumPassesOfMatrix/build.xml
@@ -0,0 +1,73 @@
+
+
+
+
+
+
+
+
+
+
+ Builds, tests, and runs the project MinimumPassesOfMatrix.
+
+
+
diff --git a/TreesAndGraphs/MinimumPassesOfMatrix/manifest.mf b/TreesAndGraphs/MinimumPassesOfMatrix/manifest.mf
new file mode 100644
index 0000000..328e8e5
--- /dev/null
+++ b/TreesAndGraphs/MinimumPassesOfMatrix/manifest.mf
@@ -0,0 +1,3 @@
+Manifest-Version: 1.0
+X-COMMENT: Main-Class will be added automatically by build
+
diff --git a/TreesAndGraphs/MinimumPassesOfMatrix/nbproject/build-impl.xml b/TreesAndGraphs/MinimumPassesOfMatrix/nbproject/build-impl.xml
new file mode 100644
index 0000000..088866e
--- /dev/null
+++ b/TreesAndGraphs/MinimumPassesOfMatrix/nbproject/build-impl.xml
@@ -0,0 +1,1420 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must set src.dir
+ Must set test.src.dir
+ Must set build.dir
+ Must set dist.dir
+ Must set build.classes.dir
+ Must set dist.javadoc.dir
+ Must set build.test.classes.dir
+ Must set build.test.results.dir
+ Must set build.classes.excludes
+ Must set dist.jar
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must set javac.includes
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ No tests executed.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must set JVM to use for profiling in profiler.info.jvm
+ Must set profiler agent JVM arguments in profiler.info.jvmargs.agent
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must select some files in the IDE or set javac.includes
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ To run this application from the command line without Ant, try:
+
+ java -jar "${dist.jar.resolved}"
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must select one file in the IDE or set run.class
+
+
+
+ Must select one file in the IDE or set run.class
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must select one file in the IDE or set debug.class
+
+
+
+
+ Must select one file in the IDE or set debug.class
+
+
+
+
+ Must set fix.includes
+
+
+
+
+
+
+
+
+
+ This target only works when run from inside the NetBeans IDE.
+
+
+
+
+
+
+
+
+ Must select one file in the IDE or set profile.class
+ This target only works when run from inside the NetBeans IDE.
+
+
+
+
+
+
+
+
+ This target only works when run from inside the NetBeans IDE.
+
+
+
+
+
+
+
+
+
+
+
+
+ This target only works when run from inside the NetBeans IDE.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must select one file in the IDE or set run.class
+
+
+
+
+
+ Must select some files in the IDE or set test.includes
+
+
+
+
+ Must select one file in the IDE or set run.class
+
+
+
+
+ Must select one file in the IDE or set applet.url
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must select some files in the IDE or set javac.includes
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Some tests failed; see details above.
+
+
+
+
+
+
+
+
+ Must select some files in the IDE or set test.includes
+
+
+
+ Some tests failed; see details above.
+
+
+
+ Must select some files in the IDE or set test.class
+ Must select some method in the IDE or set test.method
+
+
+
+ Some tests failed; see details above.
+
+
+
+
+ Must select one file in the IDE or set test.class
+
+
+
+ Must select one file in the IDE or set test.class
+ Must select some method in the IDE or set test.method
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must select one file in the IDE or set applet.url
+
+
+
+
+
+
+
+
+ Must select one file in the IDE or set applet.url
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/TreesAndGraphs/MinimumPassesOfMatrix/nbproject/genfiles.properties b/TreesAndGraphs/MinimumPassesOfMatrix/nbproject/genfiles.properties
new file mode 100644
index 0000000..bd8db44
--- /dev/null
+++ b/TreesAndGraphs/MinimumPassesOfMatrix/nbproject/genfiles.properties
@@ -0,0 +1,8 @@
+build.xml.data.CRC32=f495dfce
+build.xml.script.CRC32=b6e92989
+build.xml.stylesheet.CRC32=8064a381@1.80.1.48
+# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
+# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
+nbproject/build-impl.xml.data.CRC32=f495dfce
+nbproject/build-impl.xml.script.CRC32=589e4dfd
+nbproject/build-impl.xml.stylesheet.CRC32=830a3534@1.80.1.48
diff --git a/TreesAndGraphs/MinimumPassesOfMatrix/nbproject/project.properties b/TreesAndGraphs/MinimumPassesOfMatrix/nbproject/project.properties
new file mode 100644
index 0000000..e229012
--- /dev/null
+++ b/TreesAndGraphs/MinimumPassesOfMatrix/nbproject/project.properties
@@ -0,0 +1,74 @@
+annotation.processing.enabled=true
+annotation.processing.enabled.in.editor=false
+annotation.processing.processor.options=
+annotation.processing.processors.list=
+annotation.processing.run.all.processors=true
+annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output
+build.classes.dir=${build.dir}/classes
+build.classes.excludes=**/*.java,**/*.form
+# This directory is removed when the project is cleaned:
+build.dir=build
+build.generated.dir=${build.dir}/generated
+build.generated.sources.dir=${build.dir}/generated-sources
+# Only compile against the classpath explicitly listed here:
+build.sysclasspath=ignore
+build.test.classes.dir=${build.dir}/test/classes
+build.test.results.dir=${build.dir}/test/results
+# Uncomment to specify the preferred debugger connection transport:
+#debug.transport=dt_socket
+debug.classpath=\
+ ${run.classpath}
+debug.test.classpath=\
+ ${run.test.classpath}
+# Files in build.classes.dir which should be excluded from distribution jar
+dist.archive.excludes=
+# This directory is removed when the project is cleaned:
+dist.dir=dist
+dist.jar=${dist.dir}/MinimumPassesOfMatrix.jar
+dist.javadoc.dir=${dist.dir}/javadoc
+excludes=
+includes=**
+jar.compress=false
+javac.classpath=
+# Space-separated list of extra javac options
+javac.compilerargs=
+javac.deprecation=false
+javac.external.vm=true
+javac.processorpath=\
+ ${javac.classpath}
+javac.source=1.8
+javac.target=1.8
+javac.test.classpath=\
+ ${javac.classpath}:\
+ ${build.classes.dir}
+javac.test.processorpath=\
+ ${javac.test.classpath}
+javadoc.additionalparam=
+javadoc.author=false
+javadoc.encoding=${source.encoding}
+javadoc.noindex=false
+javadoc.nonavbar=false
+javadoc.notree=false
+javadoc.private=false
+javadoc.splitindex=true
+javadoc.use=true
+javadoc.version=false
+javadoc.windowtitle=
+main.class=minimumpassesofmatrix.MinimumPassesOfMatrix
+manifest.file=manifest.mf
+meta.inf.dir=${src.dir}/META-INF
+mkdist.disabled=false
+platform.active=default_platform
+run.classpath=\
+ ${javac.classpath}:\
+ ${build.classes.dir}
+# Space-separated list of JVM arguments used when running the project.
+# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value.
+# To set system properties for unit tests define test-sys-prop.name=value:
+run.jvmargs=
+run.test.classpath=\
+ ${javac.test.classpath}:\
+ ${build.test.classes.dir}
+source.encoding=UTF-8
+src.dir=src
+test.src.dir=test
diff --git a/TreesAndGraphs/MinimumPassesOfMatrix/nbproject/project.xml b/TreesAndGraphs/MinimumPassesOfMatrix/nbproject/project.xml
new file mode 100644
index 0000000..83c7682
--- /dev/null
+++ b/TreesAndGraphs/MinimumPassesOfMatrix/nbproject/project.xml
@@ -0,0 +1,15 @@
+
+
+ org.netbeans.modules.java.j2seproject
+
+
+ MinimumPassesOfMatrix
+
+
+
+
+
+
+
+
+
diff --git a/TreesAndGraphs/MinimumPassesOfMatrix/src/minimumpassesofmatrix/MinimumPassesOfMatrix.java b/TreesAndGraphs/MinimumPassesOfMatrix/src/minimumpassesofmatrix/MinimumPassesOfMatrix.java
new file mode 100644
index 0000000..05cb9b3
--- /dev/null
+++ b/TreesAndGraphs/MinimumPassesOfMatrix/src/minimumpassesofmatrix/MinimumPassesOfMatrix.java
@@ -0,0 +1,97 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package minimumpassesofmatrix;
+
+import java.util.ArrayList;
+
+/**
+ * Time Complexity O(w*h)^2
+ * Space Complexity O(w * h)
+ * @author souravpalit
+ */
+public class MinimumPassesOfMatrix {
+
+ public static void main(String[] args) {
+ // TODO code application logic here
+ int[][] matrix = new int[][]{
+ {0, -1, -3, 2, 0},
+ {1, -2, -5, -1, -3},
+ {3, 0, 0, -4, -1}
+ };
+ int expected = 3;
+ int actual = minimumPassesOfMatrix(matrix);
+
+ System.out.println("Expected = " + expected + " Actual =" + actual);
+ }
+
+ public static int minimumPassesOfMatrix(int[][] matrix) {
+
+ int passCount = 0;
+ while (true) {
+ Result result = change(matrix);
+ if (result.isChanged) {
+ passCount++;
+ }
+
+ if (!result.isChanged && result.hasAtLeastOneNegativeValue) {
+ // This means no changes done but still negative value
+ // that means could not convert to negative
+ passCount = -1;
+ break;
+ } else if (result.isChanged && !result.hasAtLeastOneNegativeValue) {
+ // no negative value. all converted
+ break;
+ } else if (!result.isChanged && !result.hasAtLeastOneNegativeValue) {
+ break;
+ }
+ }
+
+ return passCount;
+ }
+
+ public static Result change(int[][] matrix) {
+ Result result = new Result();
+ ArrayList ignoreList = new ArrayList();
+ for (int i = 0; i < matrix.length; i++) {
+ for (int j = 0; j < matrix[0].length; j++) {
+ if (matrix[i][j] < 0 && hasPositiveAdjacent(matrix, i, j, ignoreList)) {
+ ignoreList.add(getKey(i, j));
+ result.isChanged = true;
+ matrix[i][j] = -matrix[i][j];
+ } else if (matrix[i][j] < 0) {
+ result.hasAtLeastOneNegativeValue = true;
+ }
+ }
+ }
+
+ return result;
+ }
+
+ public static boolean hasPositiveAdjacent(int[][] matrix, int i, int j, ArrayList ignoreList) {
+ if (j + 1 < matrix[0].length && matrix[i][j + 1] > 0 && !ignoreList.contains(getKey(i, j + 1))) {
+ return true;
+ } else if (j - 1 >= 0 && matrix[i][j - 1] > 0 && !ignoreList.contains(getKey(i, j - 1))) {
+ return true;
+ } else if (i + 1 < matrix.length && matrix[i + 1][j] > 0 && !ignoreList.contains(getKey(i + 1, j))) {
+ return true;
+ } else if (i - 1 >= 0 && matrix[i - 1][j] > 0 && !ignoreList.contains(getKey(i - 1, j))) {
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ public static String getKey(int i, int j) {
+ return i + "," + j;
+ }
+
+ static class Result {
+
+ boolean isChanged;
+ boolean hasAtLeastOneNegativeValue;
+ }
+
+}
diff --git a/TreesAndGraphs/MinimumPassesOfMatrix/src/minimumpassesofmatrix/MinimumPassesOfMatrixOptimized.java b/TreesAndGraphs/MinimumPassesOfMatrix/src/minimumpassesofmatrix/MinimumPassesOfMatrixOptimized.java
new file mode 100644
index 0000000..6eb043a
--- /dev/null
+++ b/TreesAndGraphs/MinimumPassesOfMatrix/src/minimumpassesofmatrix/MinimumPassesOfMatrixOptimized.java
@@ -0,0 +1,129 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package minimumpassesofmatrix;
+
+import java.util.ArrayList;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Queue;
+import java.util.Stack;
+
+/**
+ * Time Complexity O(w * h)
+ * Space Complexity O(w * h)
+ * @author souravpalit
+ */
+public class MinimumPassesOfMatrixOptimized {
+
+ public static void main(String[] args) {
+ int[][] matrix = new int[][]{
+ {0, -1, -3, 2, 0},
+ {1, -2, -5, -1, -3},
+ {3, 0, 0, -4, -1}
+ };
+ int expected = 3;
+ int actual = minimumPassesOfMatrix(matrix);
+
+ System.out.println("Expected = " + expected + " Actual = " + actual);
+ }
+
+ public static int minimumPassesOfMatrix(int[][] matrix) {
+ return convertNegatives(matrix);
+ }
+
+ // This function will convert all the negativevalue of a matrix to positive
+ // value if possible and return the number of pass required to do that.
+ // If not possible to convert all negatives to positive then will return -1
+ public static int convertNegatives(int [][] matrix) {
+ Queue nextQ = getAllPositiveNumbers(matrix);
+ int passCount = 0;
+
+ while (!nextQ.isEmpty()) {
+ Queue currentQ = nextQ;
+ nextQ = new LinkedList();
+
+ while (!currentQ.isEmpty()) {
+ Cell cell = (Cell) currentQ.poll();
+ List negativeNeighbors = findNegativeAdjacent(matrix, cell.row, cell.col);
+
+ for (Cell negCell : negativeNeighbors) {
+ matrix[negCell.row][negCell.col] = matrix[negCell.row][negCell.col] * -1;
+ nextQ.add(negCell);
+ }
+
+ }
+ passCount++;
+ }
+
+
+ if (hasNegativeNumber(matrix)) {
+ return - 1;
+ }
+
+ return passCount - 1;
+ }
+
+ public static Queue getAllPositiveNumbers(int [][] matrix) {
+ Queue q = new LinkedList();
+
+ for (int i = 0; i < matrix.length; i++) {
+ for (int j = 0; j < matrix[0].length; j++) {
+ if (matrix[i][j] > 0) {
+ q.add(new Cell(i, j));
+ }
+ }
+ }
+
+ return q;
+ }
+
+ public static boolean hasNegativeNumber(int [][] matrix) {
+ for (int i = 0; i < matrix.length; i++) {
+ for (int j = 0; j < matrix[0].length; j++) {
+ if (matrix[i][j] < 0) {
+ return true;
+ }
+ }
+ }
+
+ return false;
+ }
+
+
+ public static List findNegativeAdjacent(int[][] matrix, int i, int j) {
+ List negativeNeighbors = new ArrayList();
+
+ if (j + 1 < matrix[0].length && matrix[i][j + 1] < 0) {
+ negativeNeighbors.add(new Cell(i, j + 1));
+ }
+
+ if (j - 1 >= 0 && matrix[i][j - 1] < 0) {
+ negativeNeighbors.add(new Cell(i, j - 1));
+ }
+
+ if (i + 1 < matrix.length && matrix[i + 1][j] < 0) {
+ negativeNeighbors.add(new Cell(i + 1, j));
+ }
+
+ if (i - 1 >= 0 && matrix[i - 1][j] < 0) {
+ negativeNeighbors.add(new Cell(i - 1, j));
+ }
+
+ return negativeNeighbors;
+ }
+
+ static class Cell {
+
+ int row;
+ int col;
+
+ public Cell(int row, int col) {
+ this.row = row;
+ this.col = col;
+ }
+ }
+
+}
diff --git a/TreesAndGraphs/MinimumPassesOfMatrix/src/minimumpassesofmatrix/MinimumPassesOfMatrixOptimizedFinal.java b/TreesAndGraphs/MinimumPassesOfMatrix/src/minimumpassesofmatrix/MinimumPassesOfMatrixOptimizedFinal.java
new file mode 100644
index 0000000..04e1f51
--- /dev/null
+++ b/TreesAndGraphs/MinimumPassesOfMatrix/src/minimumpassesofmatrix/MinimumPassesOfMatrixOptimizedFinal.java
@@ -0,0 +1,128 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package minimumpassesofmatrix;
+
+import java.util.ArrayList;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Queue;
+import java.util.Stack;
+
+/**
+ * Time Complexity O(w * h)
+ * Space Complexity O(w * h)
+ * @author souravpalit
+ */
+public class MinimumPassesOfMatrixOptimizedFinal {
+
+ public static void main(String[] args) {
+ int[][] matrix = new int[][]{
+ {0, -1, -3, 2, 0},
+ {1, -2, -5, -1, -3},
+ {3, 0, 0, -4, -1}
+ };
+ int expected = 3;
+ int actual = minimumPassesOfMatrix(matrix);
+
+ System.out.println("Expected = " + expected + " Actual = " + actual);
+ }
+
+ public static int minimumPassesOfMatrix(int[][] matrix) {
+ return convertNegatives(matrix);
+ }
+
+ // This function will convert all the negativevalue of a matrix to positive
+ // value if possible and return the number of pass required to do that.
+ // If not possible to convert all negatives to positive then will return -1
+ public static int convertNegatives(int [][] matrix) {
+ Queue currentQ = getAllPositiveNumbers(matrix);
+
+ int passCount = 0;
+
+ while (!currentQ.isEmpty()) {
+ int currentSize = currentQ.size();
+
+ while (currentSize > 0) {
+ Cell cell = (Cell) currentQ.poll();
+ currentSize--;
+ List negativeNeighbors = findNegativeAdjacent(matrix, cell.row, cell.col);
+
+ for (Cell negCell : negativeNeighbors) {
+ matrix[negCell.row][negCell.col] = matrix[negCell.row][negCell.col] * -1;
+ currentQ.add(negCell);
+ }
+ }
+ passCount++;
+ }
+
+ if (hasNegativeNumber(matrix)) {
+ return - 1;
+ }
+
+ return passCount - 1;
+ }
+
+ public static Queue getAllPositiveNumbers(int [][] matrix) {
+ Queue q = new LinkedList();
+
+ for (int i = 0; i < matrix.length; i++) {
+ for (int j = 0; j < matrix[0].length; j++) {
+ if (matrix[i][j] > 0) {
+ q.add(new Cell(i, j));
+ }
+ }
+ }
+
+ return q;
+ }
+
+ public static boolean hasNegativeNumber(int [][] matrix) {
+ for (int i = 0; i < matrix.length; i++) {
+ for (int j = 0; j < matrix[0].length; j++) {
+ if (matrix[i][j] < 0) {
+ return true;
+ }
+ }
+ }
+
+ return false;
+ }
+
+
+ public static List findNegativeAdjacent(int[][] matrix, int i, int j) {
+ List negativeNeighbors = new ArrayList| ();
+
+ if (j + 1 < matrix[0].length && matrix[i][j + 1] < 0) {
+ negativeNeighbors.add(new Cell(i, j + 1));
+ }
+
+ if (j - 1 >= 0 && matrix[i][j - 1] < 0) {
+ negativeNeighbors.add(new Cell(i, j - 1));
+ }
+
+ if (i + 1 < matrix.length && matrix[i + 1][j] < 0) {
+ negativeNeighbors.add(new Cell(i + 1, j));
+ }
+
+ if (i - 1 >= 0 && matrix[i - 1][j] < 0) {
+ negativeNeighbors.add(new Cell(i - 1, j));
+ }
+
+ return negativeNeighbors;
+ }
+
+ static class Cell {
+
+ int row;
+ int col;
+
+ public Cell(int row, int col) {
+ this.row = row;
+ this.col = col;
+ }
+ }
+
+}
diff --git a/TreesAndGraphs/ReconstructBST/build.xml b/TreesAndGraphs/ReconstructBST/build.xml
new file mode 100644
index 0000000..376a2c5
--- /dev/null
+++ b/TreesAndGraphs/ReconstructBST/build.xml
@@ -0,0 +1,73 @@
+
+
+
+
+
+
+
+
+
+
+ Builds, tests, and runs the project ReconstructBST.
+
+
+
diff --git a/TreesAndGraphs/ReconstructBST/manifest.mf b/TreesAndGraphs/ReconstructBST/manifest.mf
new file mode 100644
index 0000000..328e8e5
--- /dev/null
+++ b/TreesAndGraphs/ReconstructBST/manifest.mf
@@ -0,0 +1,3 @@
+Manifest-Version: 1.0
+X-COMMENT: Main-Class will be added automatically by build
+
diff --git a/TreesAndGraphs/ReconstructBST/nbproject/build-impl.xml b/TreesAndGraphs/ReconstructBST/nbproject/build-impl.xml
new file mode 100644
index 0000000..e837817
--- /dev/null
+++ b/TreesAndGraphs/ReconstructBST/nbproject/build-impl.xml
@@ -0,0 +1,1420 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must set src.dir
+ Must set test.src.dir
+ Must set build.dir
+ Must set dist.dir
+ Must set build.classes.dir
+ Must set dist.javadoc.dir
+ Must set build.test.classes.dir
+ Must set build.test.results.dir
+ Must set build.classes.excludes
+ Must set dist.jar
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must set javac.includes
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ No tests executed.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must set JVM to use for profiling in profiler.info.jvm
+ Must set profiler agent JVM arguments in profiler.info.jvmargs.agent
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must select some files in the IDE or set javac.includes
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ To run this application from the command line without Ant, try:
+
+ java -jar "${dist.jar.resolved}"
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must select one file in the IDE or set run.class
+
+
+
+ Must select one file in the IDE or set run.class
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must select one file in the IDE or set debug.class
+
+
+
+
+ Must select one file in the IDE or set debug.class
+
+
+
+
+ Must set fix.includes
+
+
+
+
+
+
+
+
+
+ This target only works when run from inside the NetBeans IDE.
+
+
+
+
+
+
+
+
+ Must select one file in the IDE or set profile.class
+ This target only works when run from inside the NetBeans IDE.
+
+
+
+
+
+
+
+
+ This target only works when run from inside the NetBeans IDE.
+
+
+
+
+
+
+
+
+
+
+
+
+ This target only works when run from inside the NetBeans IDE.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must select one file in the IDE or set run.class
+
+
+
+
+
+ Must select some files in the IDE or set test.includes
+
+
+
+
+ Must select one file in the IDE or set run.class
+
+
+
+
+ Must select one file in the IDE or set applet.url
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must select some files in the IDE or set javac.includes
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Some tests failed; see details above.
+
+
+
+
+
+
+
+
+ Must select some files in the IDE or set test.includes
+
+
+
+ Some tests failed; see details above.
+
+
+
+ Must select some files in the IDE or set test.class
+ Must select some method in the IDE or set test.method
+
+
+
+ Some tests failed; see details above.
+
+
+
+
+ Must select one file in the IDE or set test.class
+
+
+
+ Must select one file in the IDE or set test.class
+ Must select some method in the IDE or set test.method
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must select one file in the IDE or set applet.url
+
+
+
+
+
+
+
+
+ Must select one file in the IDE or set applet.url
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/TreesAndGraphs/ReconstructBST/nbproject/genfiles.properties b/TreesAndGraphs/ReconstructBST/nbproject/genfiles.properties
new file mode 100644
index 0000000..0861212
--- /dev/null
+++ b/TreesAndGraphs/ReconstructBST/nbproject/genfiles.properties
@@ -0,0 +1,8 @@
+build.xml.data.CRC32=25a2d110
+build.xml.script.CRC32=a9717b91
+build.xml.stylesheet.CRC32=8064a381@1.80.1.48
+# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
+# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
+nbproject/build-impl.xml.data.CRC32=25a2d110
+nbproject/build-impl.xml.script.CRC32=8addc976
+nbproject/build-impl.xml.stylesheet.CRC32=830a3534@1.80.1.48
diff --git a/TreesAndGraphs/ReconstructBST/nbproject/project.properties b/TreesAndGraphs/ReconstructBST/nbproject/project.properties
new file mode 100644
index 0000000..c5b4a2e
--- /dev/null
+++ b/TreesAndGraphs/ReconstructBST/nbproject/project.properties
@@ -0,0 +1,74 @@
+annotation.processing.enabled=true
+annotation.processing.enabled.in.editor=false
+annotation.processing.processor.options=
+annotation.processing.processors.list=
+annotation.processing.run.all.processors=true
+annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output
+build.classes.dir=${build.dir}/classes
+build.classes.excludes=**/*.java,**/*.form
+# This directory is removed when the project is cleaned:
+build.dir=build
+build.generated.dir=${build.dir}/generated
+build.generated.sources.dir=${build.dir}/generated-sources
+# Only compile against the classpath explicitly listed here:
+build.sysclasspath=ignore
+build.test.classes.dir=${build.dir}/test/classes
+build.test.results.dir=${build.dir}/test/results
+# Uncomment to specify the preferred debugger connection transport:
+#debug.transport=dt_socket
+debug.classpath=\
+ ${run.classpath}
+debug.test.classpath=\
+ ${run.test.classpath}
+# Files in build.classes.dir which should be excluded from distribution jar
+dist.archive.excludes=
+# This directory is removed when the project is cleaned:
+dist.dir=dist
+dist.jar=${dist.dir}/ReconstructBST.jar
+dist.javadoc.dir=${dist.dir}/javadoc
+excludes=
+includes=**
+jar.compress=false
+javac.classpath=
+# Space-separated list of extra javac options
+javac.compilerargs=
+javac.deprecation=false
+javac.external.vm=true
+javac.processorpath=\
+ ${javac.classpath}
+javac.source=1.8
+javac.target=1.8
+javac.test.classpath=\
+ ${javac.classpath}:\
+ ${build.classes.dir}
+javac.test.processorpath=\
+ ${javac.test.classpath}
+javadoc.additionalparam=
+javadoc.author=false
+javadoc.encoding=${source.encoding}
+javadoc.noindex=false
+javadoc.nonavbar=false
+javadoc.notree=false
+javadoc.private=false
+javadoc.splitindex=true
+javadoc.use=true
+javadoc.version=false
+javadoc.windowtitle=
+main.class=reconstructbst.ReconstructBST
+manifest.file=manifest.mf
+meta.inf.dir=${src.dir}/META-INF
+mkdist.disabled=false
+platform.active=default_platform
+run.classpath=\
+ ${javac.classpath}:\
+ ${build.classes.dir}
+# Space-separated list of JVM arguments used when running the project.
+# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value.
+# To set system properties for unit tests define test-sys-prop.name=value:
+run.jvmargs=
+run.test.classpath=\
+ ${javac.test.classpath}:\
+ ${build.test.classes.dir}
+source.encoding=UTF-8
+src.dir=src
+test.src.dir=test
diff --git a/TreesAndGraphs/ReconstructBST/nbproject/project.xml b/TreesAndGraphs/ReconstructBST/nbproject/project.xml
new file mode 100644
index 0000000..271a41b
--- /dev/null
+++ b/TreesAndGraphs/ReconstructBST/nbproject/project.xml
@@ -0,0 +1,15 @@
+
+
+ org.netbeans.modules.java.j2seproject
+
+
+ ReconstructBST
+
+
+
+
+
+
+
+
+
diff --git a/TreesAndGraphs/ReconstructBST/src/reconstructbst/BST.java b/TreesAndGraphs/ReconstructBST/src/reconstructbst/BST.java
new file mode 100644
index 0000000..146885d
--- /dev/null
+++ b/TreesAndGraphs/ReconstructBST/src/reconstructbst/BST.java
@@ -0,0 +1,45 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package reconstructbst;
+
+/**
+ *
+ * @author souravpalit
+ */
+public class BST {
+
+ public int value = 0;
+ public BST left;
+ public BST right;
+
+ public BST(int value) {
+ this.value = value;
+ }
+
+ public BST insert(int value) {
+ BST currentNode = this;
+
+ while (true) {
+ if (value < currentNode.value) {
+ if (currentNode.left == null) {
+ currentNode.left = new BST(value);
+ break;
+ } else {
+ currentNode = currentNode.left;
+ }
+ } else {
+ if (currentNode.right == null) {
+ currentNode.right = new BST(value);
+ break;
+ } else {
+ currentNode = currentNode.right;
+ }
+ }
+ }
+
+ return this;
+ }
+}
diff --git a/TreesAndGraphs/ReconstructBST/src/reconstructbst/ReconstructBST.java b/TreesAndGraphs/ReconstructBST/src/reconstructbst/ReconstructBST.java
new file mode 100644
index 0000000..0d832ea
--- /dev/null
+++ b/TreesAndGraphs/ReconstructBST/src/reconstructbst/ReconstructBST.java
@@ -0,0 +1,56 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package reconstructbst;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ *
+ * @author souravpalit
+ */
+public class ReconstructBST {
+
+ /**
+ * @param args the command line arguments
+ */
+ public static void main(String[] args) {
+ ArrayList preOrderTraversalValues =
+ new ArrayList(Arrays.asList(10, 4, 2, 1, 3, 17, 19, 18));
+ BST root = reconstructBst(preOrderTraversalValues);
+ }
+
+ public static BST reconstructBst(List preOrderTraversalValues) {
+ if (preOrderTraversalValues.size() == 0) {
+ return null;
+ }
+
+ int rootValue = preOrderTraversalValues.get(0);
+ // if no value found then this value work as out of array position which
+ // leads to empty array
+ int rightSubTreeIdx = preOrderTraversalValues.size();
+
+ for (int i = 1; i < preOrderTraversalValues.size(); i++) {
+ int value = preOrderTraversalValues.get(i);
+ if (value >= rootValue) {
+ rightSubTreeIdx = i;
+ break;
+ }
+ }
+ // from value inclusive and toValue exclusive
+ BST leftSubTree = reconstructBst(preOrderTraversalValues.subList(1, rightSubTreeIdx));
+ BST rightSubTree = reconstructBst(preOrderTraversalValues.subList(rightSubTreeIdx, preOrderTraversalValues.size()));
+
+ BST bst = new BST(rootValue);
+ bst.left = leftSubTree;
+ bst.right = rightSubTree;
+
+
+ return bst;
+ }
+
+}
diff --git a/TreesAndGraphs/ReconstructBST/src/reconstructbst/ReconstructBSTOptimized.java b/TreesAndGraphs/ReconstructBST/src/reconstructbst/ReconstructBSTOptimized.java
new file mode 100644
index 0000000..1b2a0a4
--- /dev/null
+++ b/TreesAndGraphs/ReconstructBST/src/reconstructbst/ReconstructBSTOptimized.java
@@ -0,0 +1,60 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package reconstructbst;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ *
+ * @author souravpalit
+ */
+public class ReconstructBSTOptimized {
+
+ /**
+ * @param args the command line arguments
+ */
+ public static void main(String[] args) {
+ ArrayList preOrderTraversalValues =
+ new ArrayList(Arrays.asList(10, 4, 2, 1, 5, 17, 19, 18));
+ TreeInfo treeInfo = new TreeInfo(0);
+ BST root = reconstructBst(Integer.MIN_VALUE, Integer.MAX_VALUE, preOrderTraversalValues, treeInfo);
+ System.out.println(root.value);
+ }
+
+ public static BST reconstructBst(int lowerBound, int upperBound, List preOrderTraversalValues, TreeInfo treeInfo) {
+ if (treeInfo.rootIdx >= preOrderTraversalValues.size()) {
+ return null;
+ }
+
+ int rootValue = preOrderTraversalValues.get(treeInfo.rootIdx);
+ // if no value found then this value work as out of array position which
+ // leads to empty array
+ if (rootValue < lowerBound || rootValue >= upperBound) {
+ return null;
+ }
+
+ treeInfo.rootIdx += 1;
+ // from value inclusive and toValue exclusive
+ BST leftSubTree = reconstructBst(lowerBound, rootValue, preOrderTraversalValues, treeInfo);
+ BST rightSubTree = reconstructBst(rootValue, upperBound, preOrderTraversalValues, treeInfo);
+
+ BST bst = new BST(rootValue);
+ bst.left = leftSubTree;
+ bst.right = rightSubTree;
+
+ return bst;
+ }
+
+ static class TreeInfo {
+ public int rootIdx;
+
+ public TreeInfo(int rootIdx) {
+ this.rootIdx = rootIdx;
+ }
+ }
+}
diff --git a/TreesAndGraphs/RightSmallerThan/src/rightsmallerthan/BSTAlternate.java b/TreesAndGraphs/RightSmallerThan/src/rightsmallerthan/BSTAlternate.java
new file mode 100644
index 0000000..267f60b
--- /dev/null
+++ b/TreesAndGraphs/RightSmallerThan/src/rightsmallerthan/BSTAlternate.java
@@ -0,0 +1,50 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package rightsmallerthan;
+
+/**
+ *
+ * @author souravpalit
+ */
+public class BSTAlternate {
+
+ public int value;
+ public BSTAlternate left;
+ public BSTAlternate right;
+ public int leftNodeCount;
+ public int numberOfSmallerAtInsertTime;
+ public int idx;
+
+ public BSTAlternate(int value, int idx, int numberOfSmallerAtInsertTime) {
+ this.value = value;
+ this.idx = idx;
+ this.numberOfSmallerAtInsertTime = numberOfSmallerAtInsertTime;
+ }
+
+ public void insert(int value, int idx) {
+ insertHelper(value, idx, 0);
+ }
+
+ private void insertHelper(int value, int idx, int numberOfSmallerAtInsertTime) {
+ if (value < this.value) {
+ leftNodeCount++;
+ if (left == null) {
+ left = new BSTAlternate(value, idx, numberOfSmallerAtInsertTime);
+ } else {
+ left.insertHelper(value, idx, numberOfSmallerAtInsertTime);
+ }
+ } else {
+ numberOfSmallerAtInsertTime += leftNodeCount;
+ if (value > this.value) numberOfSmallerAtInsertTime++;
+
+ if (right == null) {
+ right = new BSTAlternate(value, idx, numberOfSmallerAtInsertTime);
+ } else {
+ right.insertHelper(value, idx, numberOfSmallerAtInsertTime);
+ }
+ }
+ }
+}
diff --git a/TreesAndGraphs/RightSmallerThan/src/rightsmallerthan/RightSmallerThanAlternate.java b/TreesAndGraphs/RightSmallerThan/src/rightsmallerthan/RightSmallerThanAlternate.java
new file mode 100644
index 0000000..ad84a07
--- /dev/null
+++ b/TreesAndGraphs/RightSmallerThan/src/rightsmallerthan/RightSmallerThanAlternate.java
@@ -0,0 +1,74 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package rightsmallerthan;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Average Time Complexity: O(nlogn) and Space Complexity: O(N)
+ * Worst Time complexity: O(n^2) when BST is left aligned or right aligned
+ * @author souravpalit
+ */
+public class RightSmallerThanAlternate {
+
+ /**
+ * @param args the command line arguments
+ */
+ public static void main(String[] args) {
+ List input = new ArrayList();
+
+ /*input.add(8);
+ input.add(5);
+ input.add(2);
+ input.add(9);
+ input.add(5);
+ input.add(6);
+ input.add(3);*/
+
+ input.add(8);
+ input.add(5);
+ input.add(11);
+ input.add(-1);
+ input.add(3);
+ input.add(4);
+ input.add(2);
+
+ /*input.add(0);
+ input.add(1);
+ input.add(1);
+ input.add(1);
+ input.add(2);*/
+
+ List output = rightSmallerThan(input);
+
+ for (Integer value : output) {
+ System.out.print(value + " ");
+ }
+ }
+
+ public static List rightSmallerThan(List array) {
+ if (array.size() == 0) return new ArrayList();
+ int lastIdx = array.size() - 1;
+ BSTAlternate bst = new BSTAlternate(array.get(lastIdx), lastIdx, 0);
+
+ for (int i = lastIdx - 1; i >= 0; i--) {
+ bst.insert(array.get(i), i);
+ }
+
+ List result = new ArrayList(array);
+ getSmallerCount(bst, result);
+ return result;
+ }
+
+ public static void getSmallerCount(BSTAlternate bst, List result) {
+ if (bst == null) return;
+
+ result.set(bst.idx, bst.numberOfSmallerAtInsertTime);
+ getSmallerCount(bst.left, result);
+ getSmallerCount(bst.right, result);
+ }
+}
diff --git a/TreesAndGraphs/TwoEdgeConnectedGraph/build.xml b/TreesAndGraphs/TwoEdgeConnectedGraph/build.xml
new file mode 100644
index 0000000..c0c546a
--- /dev/null
+++ b/TreesAndGraphs/TwoEdgeConnectedGraph/build.xml
@@ -0,0 +1,73 @@
+
+
+
+
+
+
+
+
+
+
+ Builds, tests, and runs the project TwoEdgeConnectedGraph.
+
+
+
diff --git a/TreesAndGraphs/TwoEdgeConnectedGraph/manifest.mf b/TreesAndGraphs/TwoEdgeConnectedGraph/manifest.mf
new file mode 100644
index 0000000..328e8e5
--- /dev/null
+++ b/TreesAndGraphs/TwoEdgeConnectedGraph/manifest.mf
@@ -0,0 +1,3 @@
+Manifest-Version: 1.0
+X-COMMENT: Main-Class will be added automatically by build
+
diff --git a/TreesAndGraphs/TwoEdgeConnectedGraph/nbproject/build-impl.xml b/TreesAndGraphs/TwoEdgeConnectedGraph/nbproject/build-impl.xml
new file mode 100644
index 0000000..4055ae9
--- /dev/null
+++ b/TreesAndGraphs/TwoEdgeConnectedGraph/nbproject/build-impl.xml
@@ -0,0 +1,1420 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must set src.dir
+ Must set test.src.dir
+ Must set build.dir
+ Must set dist.dir
+ Must set build.classes.dir
+ Must set dist.javadoc.dir
+ Must set build.test.classes.dir
+ Must set build.test.results.dir
+ Must set build.classes.excludes
+ Must set dist.jar
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must set javac.includes
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ No tests executed.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must set JVM to use for profiling in profiler.info.jvm
+ Must set profiler agent JVM arguments in profiler.info.jvmargs.agent
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must select some files in the IDE or set javac.includes
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ To run this application from the command line without Ant, try:
+
+ java -jar "${dist.jar.resolved}"
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must select one file in the IDE or set run.class
+
+
+
+ Must select one file in the IDE or set run.class
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must select one file in the IDE or set debug.class
+
+
+
+
+ Must select one file in the IDE or set debug.class
+
+
+
+
+ Must set fix.includes
+
+
+
+
+
+
+
+
+
+ This target only works when run from inside the NetBeans IDE.
+
+
+
+
+
+
+
+
+ Must select one file in the IDE or set profile.class
+ This target only works when run from inside the NetBeans IDE.
+
+
+
+
+
+
+
+
+ This target only works when run from inside the NetBeans IDE.
+
+
+
+
+
+
+
+
+
+
+
+
+ This target only works when run from inside the NetBeans IDE.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must select one file in the IDE or set run.class
+
+
+
+
+
+ Must select some files in the IDE or set test.includes
+
+
+
+
+ Must select one file in the IDE or set run.class
+
+
+
+
+ Must select one file in the IDE or set applet.url
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must select some files in the IDE or set javac.includes
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Some tests failed; see details above.
+
+
+
+
+
+
+
+
+ Must select some files in the IDE or set test.includes
+
+
+
+ Some tests failed; see details above.
+
+
+
+ Must select some files in the IDE or set test.class
+ Must select some method in the IDE or set test.method
+
+
+
+ Some tests failed; see details above.
+
+
+
+
+ Must select one file in the IDE or set test.class
+
+
+
+ Must select one file in the IDE or set test.class
+ Must select some method in the IDE or set test.method
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must select one file in the IDE or set applet.url
+
+
+
+
+
+
+
+
+ Must select one file in the IDE or set applet.url
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/TreesAndGraphs/TwoEdgeConnectedGraph/nbproject/genfiles.properties b/TreesAndGraphs/TwoEdgeConnectedGraph/nbproject/genfiles.properties
new file mode 100644
index 0000000..d6a249a
--- /dev/null
+++ b/TreesAndGraphs/TwoEdgeConnectedGraph/nbproject/genfiles.properties
@@ -0,0 +1,8 @@
+build.xml.data.CRC32=52f6f04f
+build.xml.script.CRC32=c465f529
+build.xml.stylesheet.CRC32=8064a381@1.80.1.48
+# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
+# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
+nbproject/build-impl.xml.data.CRC32=52f6f04f
+nbproject/build-impl.xml.script.CRC32=6a842b92
+nbproject/build-impl.xml.stylesheet.CRC32=830a3534@1.80.1.48
diff --git a/TreesAndGraphs/TwoEdgeConnectedGraph/nbproject/project.properties b/TreesAndGraphs/TwoEdgeConnectedGraph/nbproject/project.properties
new file mode 100644
index 0000000..d55ac05
--- /dev/null
+++ b/TreesAndGraphs/TwoEdgeConnectedGraph/nbproject/project.properties
@@ -0,0 +1,74 @@
+annotation.processing.enabled=true
+annotation.processing.enabled.in.editor=false
+annotation.processing.processor.options=
+annotation.processing.processors.list=
+annotation.processing.run.all.processors=true
+annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output
+build.classes.dir=${build.dir}/classes
+build.classes.excludes=**/*.java,**/*.form
+# This directory is removed when the project is cleaned:
+build.dir=build
+build.generated.dir=${build.dir}/generated
+build.generated.sources.dir=${build.dir}/generated-sources
+# Only compile against the classpath explicitly listed here:
+build.sysclasspath=ignore
+build.test.classes.dir=${build.dir}/test/classes
+build.test.results.dir=${build.dir}/test/results
+# Uncomment to specify the preferred debugger connection transport:
+#debug.transport=dt_socket
+debug.classpath=\
+ ${run.classpath}
+debug.test.classpath=\
+ ${run.test.classpath}
+# Files in build.classes.dir which should be excluded from distribution jar
+dist.archive.excludes=
+# This directory is removed when the project is cleaned:
+dist.dir=dist
+dist.jar=${dist.dir}/TwoEdgeConnectedGraph.jar
+dist.javadoc.dir=${dist.dir}/javadoc
+excludes=
+includes=**
+jar.compress=false
+javac.classpath=
+# Space-separated list of extra javac options
+javac.compilerargs=
+javac.deprecation=false
+javac.external.vm=true
+javac.processorpath=\
+ ${javac.classpath}
+javac.source=1.8
+javac.target=1.8
+javac.test.classpath=\
+ ${javac.classpath}:\
+ ${build.classes.dir}
+javac.test.processorpath=\
+ ${javac.test.classpath}
+javadoc.additionalparam=
+javadoc.author=false
+javadoc.encoding=${source.encoding}
+javadoc.noindex=false
+javadoc.nonavbar=false
+javadoc.notree=false
+javadoc.private=false
+javadoc.splitindex=true
+javadoc.use=true
+javadoc.version=false
+javadoc.windowtitle=
+main.class=twoedgeconnectedgraph.TwoEdgeConnectedGraph
+manifest.file=manifest.mf
+meta.inf.dir=${src.dir}/META-INF
+mkdist.disabled=false
+platform.active=default_platform
+run.classpath=\
+ ${javac.classpath}:\
+ ${build.classes.dir}
+# Space-separated list of JVM arguments used when running the project.
+# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value.
+# To set system properties for unit tests define test-sys-prop.name=value:
+run.jvmargs=
+run.test.classpath=\
+ ${javac.test.classpath}:\
+ ${build.test.classes.dir}
+source.encoding=UTF-8
+src.dir=src
+test.src.dir=test
diff --git a/TreesAndGraphs/TwoEdgeConnectedGraph/nbproject/project.xml b/TreesAndGraphs/TwoEdgeConnectedGraph/nbproject/project.xml
new file mode 100644
index 0000000..c510f6d
--- /dev/null
+++ b/TreesAndGraphs/TwoEdgeConnectedGraph/nbproject/project.xml
@@ -0,0 +1,15 @@
+
+
+ org.netbeans.modules.java.j2seproject
+
+
+ TwoEdgeConnectedGraph
+
+
+
+
+
+
+
+
+
diff --git a/TreesAndGraphs/TwoEdgeConnectedGraph/src/twoedgeconnectedgraph/TwoEdgeConnectedGraph.java b/TreesAndGraphs/TwoEdgeConnectedGraph/src/twoedgeconnectedgraph/TwoEdgeConnectedGraph.java
new file mode 100644
index 0000000..d10e738
--- /dev/null
+++ b/TreesAndGraphs/TwoEdgeConnectedGraph/src/twoedgeconnectedgraph/TwoEdgeConnectedGraph.java
@@ -0,0 +1,92 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package twoedgeconnectedgraph;
+
+import java.util.Arrays;
+
+/**
+ * Time Complexity: O(v + e)
+ * Space Complexity: O(v)
+ * @author souravpalit
+ */
+public class TwoEdgeConnectedGraph {
+
+ /**
+ * @param args the command line arguments
+ */
+ public static void main(String[] args) {
+ int[][] input = new int[][]{{1, 2, 5}, {0, 2}, {0, 1, 3}, {2, 4, 5}, {3, 5}, {0, 3, 4}};
+
+ System.out.println("twoEdgeConnectedGraph " + twoEdgeConnectedGraph(input));
+ }
+
+ public static boolean twoEdgeConnectedGraph(int[][] edges) {
+ if (edges.length == 0) {
+ return true;
+ }
+
+ int arrivalTimes [] = new int[edges.length];
+
+ Arrays.fill(arrivalTimes, -1);
+ int startingVertex = 0;
+
+ if (getMinimumArrivalTimeOfAncestor(startingVertex, -1, 0, arrivalTimes, edges) == -1) {
+ return false;
+ }
+
+ return areAllVertecVisited(arrivalTimes);
+ }
+
+ public static int getMinimumArrivalTimeOfAncestor(
+ int currentVertex,
+ int parentVertex,
+ int currentTime,
+ int [] arrivalTimes,
+ int [][] edges
+ ) {
+
+ arrivalTimes[currentVertex] = currentTime;
+ int minimumArrivalTime = currentTime;
+
+ for (Integer destination : edges[currentVertex]) {
+ if (arrivalTimes[destination] == -1) {
+ // that means it is not visited
+ minimumArrivalTime = Math.min(minimumArrivalTime, getMinimumArrivalTimeOfAncestor(
+ destination,
+ currentVertex,
+ currentTime + 1,
+ arrivalTimes,
+ edges
+ ));
+ } else if (destination != parentVertex) {
+ // thats meann it is not parent but already visited so its minimumArrivalTime
+ // is in the arrivalTimes
+ minimumArrivalTime = Math.min(minimumArrivalTime, arrivalTimes[destination]);
+ }
+ }
+
+ if (minimumArrivalTime == currentTime && parentVertex != -1) {
+ // second part is for starting vertext where it has no parent
+ // as did not get backedge so return -1
+ return -1;
+ }
+
+ return minimumArrivalTime;
+
+ }
+
+
+ public static boolean areAllVertecVisited(int [] arrivalTimes) {
+ for (Integer value : arrivalTimes) {
+ if (value == -1) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+}
diff --git a/TreesAndGraphs/ValidateThreeNodes/build.xml b/TreesAndGraphs/ValidateThreeNodes/build.xml
new file mode 100644
index 0000000..40b527c
--- /dev/null
+++ b/TreesAndGraphs/ValidateThreeNodes/build.xml
@@ -0,0 +1,73 @@
+
+
+
+
+
+
+
+
+
+
+ Builds, tests, and runs the project ValidateThreeNodes.
+
+
+
diff --git a/TreesAndGraphs/ValidateThreeNodes/manifest.mf b/TreesAndGraphs/ValidateThreeNodes/manifest.mf
new file mode 100644
index 0000000..328e8e5
--- /dev/null
+++ b/TreesAndGraphs/ValidateThreeNodes/manifest.mf
@@ -0,0 +1,3 @@
+Manifest-Version: 1.0
+X-COMMENT: Main-Class will be added automatically by build
+
diff --git a/TreesAndGraphs/ValidateThreeNodes/nbproject/build-impl.xml b/TreesAndGraphs/ValidateThreeNodes/nbproject/build-impl.xml
new file mode 100644
index 0000000..533d64f
--- /dev/null
+++ b/TreesAndGraphs/ValidateThreeNodes/nbproject/build-impl.xml
@@ -0,0 +1,1420 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must set src.dir
+ Must set test.src.dir
+ Must set build.dir
+ Must set dist.dir
+ Must set build.classes.dir
+ Must set dist.javadoc.dir
+ Must set build.test.classes.dir
+ Must set build.test.results.dir
+ Must set build.classes.excludes
+ Must set dist.jar
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must set javac.includes
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ No tests executed.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must set JVM to use for profiling in profiler.info.jvm
+ Must set profiler agent JVM arguments in profiler.info.jvmargs.agent
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must select some files in the IDE or set javac.includes
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ To run this application from the command line without Ant, try:
+
+ java -jar "${dist.jar.resolved}"
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must select one file in the IDE or set run.class
+
+
+
+ Must select one file in the IDE or set run.class
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must select one file in the IDE or set debug.class
+
+
+
+
+ Must select one file in the IDE or set debug.class
+
+
+
+
+ Must set fix.includes
+
+
+
+
+
+
+
+
+
+ This target only works when run from inside the NetBeans IDE.
+
+
+
+
+
+
+
+
+ Must select one file in the IDE or set profile.class
+ This target only works when run from inside the NetBeans IDE.
+
+
+
+
+
+
+
+
+ This target only works when run from inside the NetBeans IDE.
+
+
+
+
+
+
+
+
+
+
+
+
+ This target only works when run from inside the NetBeans IDE.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must select one file in the IDE or set run.class
+
+
+
+
+
+ Must select some files in the IDE or set test.includes
+
+
+
+
+ Must select one file in the IDE or set run.class
+
+
+
+
+ Must select one file in the IDE or set applet.url
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must select some files in the IDE or set javac.includes
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Some tests failed; see details above.
+
+
+
+
+
+
+
+
+ Must select some files in the IDE or set test.includes
+
+
+
+ Some tests failed; see details above.
+
+
+
+ Must select some files in the IDE or set test.class
+ Must select some method in the IDE or set test.method
+
+
+
+ Some tests failed; see details above.
+
+
+
+
+ Must select one file in the IDE or set test.class
+
+
+
+ Must select one file in the IDE or set test.class
+ Must select some method in the IDE or set test.method
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Must select one file in the IDE or set applet.url
+
+
+
+
+
+
+
+
+ Must select one file in the IDE or set applet.url
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/TreesAndGraphs/ValidateThreeNodes/nbproject/genfiles.properties b/TreesAndGraphs/ValidateThreeNodes/nbproject/genfiles.properties
new file mode 100644
index 0000000..ba96a48
--- /dev/null
+++ b/TreesAndGraphs/ValidateThreeNodes/nbproject/genfiles.properties
@@ -0,0 +1,8 @@
+build.xml.data.CRC32=3d3cfec3
+build.xml.script.CRC32=870f29db
+build.xml.stylesheet.CRC32=8064a381@1.80.1.48
+# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
+# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
+nbproject/build-impl.xml.data.CRC32=3d3cfec3
+nbproject/build-impl.xml.script.CRC32=76192bfc
+nbproject/build-impl.xml.stylesheet.CRC32=830a3534@1.80.1.48
diff --git a/TreesAndGraphs/ValidateThreeNodes/nbproject/project.properties b/TreesAndGraphs/ValidateThreeNodes/nbproject/project.properties
new file mode 100644
index 0000000..803a7cc
--- /dev/null
+++ b/TreesAndGraphs/ValidateThreeNodes/nbproject/project.properties
@@ -0,0 +1,74 @@
+annotation.processing.enabled=true
+annotation.processing.enabled.in.editor=false
+annotation.processing.processor.options=
+annotation.processing.processors.list=
+annotation.processing.run.all.processors=true
+annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output
+build.classes.dir=${build.dir}/classes
+build.classes.excludes=**/*.java,**/*.form
+# This directory is removed when the project is cleaned:
+build.dir=build
+build.generated.dir=${build.dir}/generated
+build.generated.sources.dir=${build.dir}/generated-sources
+# Only compile against the classpath explicitly listed here:
+build.sysclasspath=ignore
+build.test.classes.dir=${build.dir}/test/classes
+build.test.results.dir=${build.dir}/test/results
+# Uncomment to specify the preferred debugger connection transport:
+#debug.transport=dt_socket
+debug.classpath=\
+ ${run.classpath}
+debug.test.classpath=\
+ ${run.test.classpath}
+# Files in build.classes.dir which should be excluded from distribution jar
+dist.archive.excludes=
+# This directory is removed when the project is cleaned:
+dist.dir=dist
+dist.jar=${dist.dir}/ValidateThreeNodes.jar
+dist.javadoc.dir=${dist.dir}/javadoc
+excludes=
+includes=**
+jar.compress=false
+javac.classpath=
+# Space-separated list of extra javac options
+javac.compilerargs=
+javac.deprecation=false
+javac.external.vm=true
+javac.processorpath=\
+ ${javac.classpath}
+javac.source=1.8
+javac.target=1.8
+javac.test.classpath=\
+ ${javac.classpath}:\
+ ${build.classes.dir}
+javac.test.processorpath=\
+ ${javac.test.classpath}
+javadoc.additionalparam=
+javadoc.author=false
+javadoc.encoding=${source.encoding}
+javadoc.noindex=false
+javadoc.nonavbar=false
+javadoc.notree=false
+javadoc.private=false
+javadoc.splitindex=true
+javadoc.use=true
+javadoc.version=false
+javadoc.windowtitle=
+main.class=validatethreenodes.ValidateThreeNodes
+manifest.file=manifest.mf
+meta.inf.dir=${src.dir}/META-INF
+mkdist.disabled=false
+platform.active=default_platform
+run.classpath=\
+ ${javac.classpath}:\
+ ${build.classes.dir}
+# Space-separated list of JVM arguments used when running the project.
+# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value.
+# To set system properties for unit tests define test-sys-prop.name=value:
+run.jvmargs=
+run.test.classpath=\
+ ${javac.test.classpath}:\
+ ${build.test.classes.dir}
+source.encoding=UTF-8
+src.dir=src
+test.src.dir=test
diff --git a/TreesAndGraphs/ValidateThreeNodes/nbproject/project.xml b/TreesAndGraphs/ValidateThreeNodes/nbproject/project.xml
new file mode 100644
index 0000000..6693cf6
--- /dev/null
+++ b/TreesAndGraphs/ValidateThreeNodes/nbproject/project.xml
@@ -0,0 +1,15 @@
+
+
+ org.netbeans.modules.java.j2seproject
+
+
+ ValidateThreeNodes
+
+
+
+
+
+
+
+
+
diff --git a/TreesAndGraphs/ValidateThreeNodes/src/validatethreenodes/ValidateThreeNodes.java b/TreesAndGraphs/ValidateThreeNodes/src/validatethreenodes/ValidateThreeNodes.java
new file mode 100644
index 0000000..837f49d
--- /dev/null
+++ b/TreesAndGraphs/ValidateThreeNodes/src/validatethreenodes/ValidateThreeNodes.java
@@ -0,0 +1,63 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package validatethreenodes;
+
+/**
+ * Time Complexity: O(h) as maximum h node need to visit
+ * Space Complexity: O(h) because of recursion call stack
+ * @author souravpalit
+ */
+public class ValidateThreeNodes {
+
+ /**
+ * @param args the command line arguments
+ */
+ public static void main(String[] args) {
+ // TODO code application logic here
+ }
+
+
+
+ public static boolean validateThreeNodes(BST nodeOne, BST nodeTwo, BST nodeThree) {
+ // Write your code here.
+ if (isDescendant(nodeOne, nodeTwo)) {
+ return isDescendant(nodeTwo, nodeThree);
+ }
+
+ if (isDescendant(nodeThree, nodeTwo)) {
+ return isDescendant(nodeTwo, nodeOne);
+ }
+
+ return false;
+ }
+
+ public static boolean isDescendant(BST node, BST target) {
+ if (node == target) {
+ return true;
+ } else if (node == null) {
+ return false;
+ }
+
+ if (target.value < node.value) {
+ node = node.left;
+ } else {
+ node = node.right;
+ }
+
+ return isDescendant(node, target);
+ }
+
+
+ static class BST {
+ public int value;
+ public BST left = null;
+ public BST right = null;
+
+ public BST(int value) {
+ this.value = value;
+ }
+ }
+}
diff --git a/TreesAndGraphs/ValidateThreeNodes/src/validatethreenodes/ValidateThreeNodesIteration.java b/TreesAndGraphs/ValidateThreeNodes/src/validatethreenodes/ValidateThreeNodesIteration.java
new file mode 100644
index 0000000..d28a238
--- /dev/null
+++ b/TreesAndGraphs/ValidateThreeNodes/src/validatethreenodes/ValidateThreeNodesIteration.java
@@ -0,0 +1,59 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package validatethreenodes;
+
+/**
+ * Time Complexity: O(h) as maximum h node need to visit
+ * Space Complexity: O(1)
+ * @author souravpalit
+ */
+public class ValidateThreeNodesIteration {
+
+ /**
+ * @param args the command line arguments
+ */
+ public static void main(String[] args) {
+ // TODO code application logic here
+ }
+
+
+
+ public static boolean validateThreeNodes(BST nodeOne, BST nodeTwo, BST nodeThree) {
+ // Write your code here.
+ if (isDescendant(nodeOne, nodeTwo)) {
+ return isDescendant(nodeTwo, nodeThree);
+ }
+
+ if (isDescendant(nodeThree, nodeTwo)) {
+ return isDescendant(nodeTwo, nodeOne);
+ }
+
+ return false;
+ }
+
+ public static boolean isDescendant(BST node, BST target) {
+ while (node != null && node != target) {
+ if (target.value < node.value) {
+ node = node.left;
+ } else {
+ node = node.right;
+ }
+ }
+
+ return node == target;
+ }
+
+
+ static class BST {
+ public int value;
+ public BST left = null;
+ public BST right = null;
+
+ public BST(int value) {
+ this.value = value;
+ }
+ }
+}
diff --git a/TreesAndGraphs/ValidateThreeNodes/src/validatethreenodes/ValidateThreeNodesOptimized.java b/TreesAndGraphs/ValidateThreeNodes/src/validatethreenodes/ValidateThreeNodesOptimized.java
new file mode 100644
index 0000000..98a01a1
--- /dev/null
+++ b/TreesAndGraphs/ValidateThreeNodes/src/validatethreenodes/ValidateThreeNodesOptimized.java
@@ -0,0 +1,81 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package validatethreenodes;
+
+/**
+ * Time Complexity: O(d) where d is th distance between nodeOne to nodeThree or
+ * nodeThree to nodeOne
+ * Space Complexity: O(1)
+ * @author souravpalit
+ */
+public class ValidateThreeNodesOptimized {
+
+ /**
+ * @param args the command line arguments
+ */
+ public static void main(String[] args) {
+ // TODO code application logic here
+ }
+
+
+
+ public static boolean validateThreeNodes(BST nodeOne, BST nodeTwo, BST nodeThree) {
+ BST searchOne = nodeOne;
+ BST searchTwo = nodeThree;
+
+ // Finding nodeTwo one nodeOne and nodeThree
+ while (true) {
+ boolean isNodeOneFoundFromNodeThree = searchTwo == nodeOne;
+ boolean isNodeThreeFoundFromNodeOne = searchOne == nodeThree;
+ boolean isNodeTwoFound = searchOne == nodeTwo || searchTwo == nodeTwo;
+ boolean isSearchFinished = searchOne == null && searchTwo == null;
+
+ if (isNodeOneFoundFromNodeThree || isNodeThreeFoundFromNodeOne || isNodeTwoFound || isSearchFinished) {
+ break;
+ }
+
+ if (searchOne != null) {
+ searchOne = (nodeTwo.value < searchOne.value) ? searchOne.left : searchOne.right;
+ }
+
+ if (searchTwo != null) {
+ searchTwo = (nodeTwo.value < searchTwo.value) ? searchTwo.left : searchTwo.right;
+ }
+ }
+
+ boolean isNodeTwoFound = searchOne == nodeTwo || searchTwo == nodeTwo;
+ boolean foundNodeFromOneAnother = searchOne == nodeThree || searchTwo == nodeOne;
+
+ if (!isNodeTwoFound || foundNodeFromOneAnother) {
+ return false;
+ }
+
+ return isDescendant(nodeTwo, (searchOne == nodeTwo) ? nodeThree : nodeOne);
+ }
+
+ public static boolean isDescendant(BST node, BST target) {
+ while (node != null && node != target) {
+ if (target.value < node.value) {
+ node = node.left;
+ } else {
+ node = node.right;
+ }
+ }
+
+ return node == target;
+ }
+
+
+ static class BST {
+ public int value;
+ public BST left = null;
+ public BST right = null;
+
+ public BST(int value) {
+ this.value = value;
+ }
+ }
+}
| | | | | | | | | | | | | | | | |