From BST (binary search tree) to linkedlist array - java

A binary search tree was created by traversing through an array from left to right and inserting each element. This tree may not be a balanced tree. Given a binary search tree with distinct elements, print all possible arrays that could have led to this tree.
To answer to this question I wrote the following code. Still, it seems that it doesn't print all possible arrays that could have lead to the the tree in all the cases. What do you think should be modified ?
public class Main {
public static LinkedList<Integer> passed = new LinkedList<>();
public static LinkedList<BinaryTree> notyet = new LinkedList<>();
public static ArrayList<LinkedList<Integer>> results = new ArrayList<LinkedList<Integer>>();
public static void main(String args[]) {
BinaryTree tr = readTree();
ArrayList<LinkedList<Integer>> result = allSequences(tr);
for (LinkedList<Integer> l : result){
for(int elem: l) System.out.print(elem+" ");
System.out.println("");
}
}
private static BinaryTree readTree() {
BinaryTree tr = new BinaryTree(2, null, null);
tr.left = new BinaryTree(1, null, null);
tr.right = new BinaryTree(3, null, null);
return tr;
}
public static ArrayList<LinkedList<Integer>> allSequences(BinaryTree tr){
// implement here
ArrayList<LinkedList<Integer>> result = new ArrayList<LinkedList<Integer>>();
findseqs(passed,notyet,tr);
//result=results.clone();
for(LinkedList<Integer> sample :results){
result.add(sample);
}
return result;
}
public static void findseqs(LinkedList<Integer> passed, LinkedList<BinaryTree> notyet, BinaryTree tr) {
passed.add(tr.value);
if (tr.left != null) notyet.add(tr.left);
if (tr.right != null) notyet.add(tr.right);
if (notyet.isEmpty()) {
results.add(passed);
}
for (BinaryTree elem: notyet) {
LinkedList<Integer> temp = (LinkedList<Integer>) passed.clone();
LinkedList<BinaryTree> ptemp = (LinkedList<BinaryTree>) notyet.clone();
ptemp.remove(elem);
findseqs(temp, ptemp, elem);
}
}

What holds about the array is that if A is ancestor of B in the graph then A precedes B in the array. Nothing else can be assumed.
So the arrays can be produced by the following recursive function.
function sourceArrays(Tree t)
// leafe node
if t == null
return empty list;
r = root(t);
append r to existing arrays;
la = sourceArrays(t.left);
ra = sourceArrays(t.right);
ac = createArrayCombitations(la, ra);
append ac to existing arrays;
end
function createArrayCombitations(la, ra)
foreach a in la
foreach b in ra
r = combineArrays(a,b);
add r to result;
end
end
end
function combineArrays(a, b)
generate all combinations of elements from two array such that order of elements in each array is preserved.
Ie if x precedes y in a or b the x precedes y in result

Related

How to get Cartesian product from multiple lists?

Say I have several List<T>s, I will put them into another list or other collections, so I don't know how many list<T> I have until I call List<List<T>>.size()
Take below List<Integer> as an example:
list1=[1,2]
list2=[3,4]
list3=[5,6]
....
listn=[2*n-1,2n];
How can I get the result of list1*list2*list3*...listn as a Cartesian product?
For example:
list1*list2*list3
should be:
[1,3,5],[1,3,6],[1,4,5],[1,4,6],[2,3,5],[2,3,6],[2,4,5],[2,4,6]
You can use recursion to achieve it, your base case of recursion is when input is empty then return empty list, else process the remaining elements. E.g.
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
public class CartesianProduct {
public static <T> List<List<T>> calculate(List<List<T>> input) {
List<List<T>> res = new ArrayList<>();
if (input.isEmpty()) { // if no more elements to process
res.add(new ArrayList<>()); // then add empty list and return
return res;
} else {
// we need to calculate the cartesian product
// of input and store it in res variable
process(input, res);
}
return res; // method completes , return result
}
private static <T> void process(List<List<T>> lists, List<List<T>> res) {
//take first element of the list
List<T> head = lists.get(0);
//invoke calculate on remaining element, here is recursion
List<List<T>> tail = calculate(lists.subList(1, lists.size()));
for (T h : head) { // for each head
for (List<T> t : tail) { //iterate over the tail
List<T> tmp = new ArrayList<>(t.size());
tmp.add(h); // add the head
tmp.addAll(t); // and current tail element
res.add(tmp);
}
}
}
public static void main(String[] args) {
//we invoke the calculate method
System.out.println(calculate(Arrays.asList(
Arrays.asList(1, 2),
Arrays.asList(3, 4),
Arrays.asList(5, 6))));
}
}
Output
[[1,3,5],[1,3,6],[1,4,5],[1,4,6],[2,3,5],[2,3,6],[2,4,5],[2,4,6]]
Thanks to #sol4me 's answer using tail recursion, here is another version which is not using tail recursion but I think is easier to understand.
public class CartesianProduct {
public static <T> List<List<T>> calculate(List<List<T>> input) {
List<List<T>> result = new ArrayList<List<T>>();
if (input.isEmpty()) { // If input an empty list
// add empty list and return
result.add(new ArrayList<T>());
return result;
} else {
// get the first list as a head
List<T> head = input.get(0);
// recursion to calculate a tail list
List<List<T>> tail = calculate(input.subList(1, input.size()));
// we merge every head element with every tail list.
for (T h : head) {
for (List<T> t : tail) {
List<T> resultElement = new ArrayList<T>();
resultElement.add(h);
resultElement.addAll(t);
result.add(resultElement);
}
}
}
return result;
}
public static void main(String[] args) {
List<List<Integer>> bigList = Arrays.asList(
Arrays.asList(1, 2),
Arrays.asList(3, 4),
Arrays.asList(5, 6),
Arrays.asList(7, 8));
System.out.println(calculate(bigList));
}
}
The map-and-reduce approach using nested loops
Prepare a list of lists List<List<T>> populated with a single empty value. This list is used further as a storage of intermediate results and as a final result.
Sequentially append the data from incoming lists List<List<T>> to the intermediate result and obtain the final result. Schematically, this iterative process looks as follows:
result0: [[]]
list1: [1,2]
-------
result1: [[1],[2]]
list2: [3,4]
-------
result2: [[1,3],[1,4],[2,3],[2,4]]
list3: [5,6]
-------
result3: [[1,3,5],[1,3,6],[1,4,5],[1,4,6],[2,3,5],[2,3,6],[2,4,5],[2,4,6]]
Try it online!
/**
* #param lists an arbitrary number of lists
* #param <T> the type of the elements
* #return the Cartesian product
*/
public static <T> List<List<T>> cartesianProduct(List<List<T>> lists) {
// check if incoming data is not null
if (lists == null) return Collections.emptyList();
// Cartesian product, intermediate result
List<List<T>> cp = Collections.singletonList(Collections.emptyList());
// iterate through incoming lists
for (List<T> list : lists) {
// non-null and non-empty lists
if (list == null || list.size() == 0) continue;
// intermediate result for next iteration
List<List<T>> next = new ArrayList<>();
// rows of current intermediate result
for (List<T> row : cp) {
// elements of current list
for (T el : list) {
// new row for next intermediate result
List<T> nRow = new ArrayList<>(row);
nRow.add(el);
next.add(nRow);
}
}
// pass to next iteration
cp = next;
}
// Cartesian product, final result
return cp;
}
public static void main(String[] args) {
List<List<Integer>> lists = prepareLists(3);
List<List<Integer>> cp = cartesianProduct(lists);
// output without spaces
System.out.println(lists.toString().replace(" ", ""));
System.out.println(cp.toString().replace(" ", ""));
}
// supplementary method, prepares lists for multiplication
public static List<List<Integer>> prepareLists(int n) {
List<List<Integer>> lists = new ArrayList<>(n);
for (int i = 1; i <= n; i++)
lists.add(Arrays.asList(i * 2 - 1, i * 2));
return lists;
}
Output:
[[1,2],[3,4],[5,6]]
[[1,3,5],[1,3,6],[1,4,5],[1,4,6],[2,3,5],[2,3,6],[2,4,5],[2,4,6]]
See also: Generate all combinations from multiple lists

converting BST to array

I've looked all over and can't seem to find any help for this.. for a school project I have a BST tree and I have to put all the ints from the tree into an int array called BSTarray. This is what I have so far:
public int [] toBSTArray() {
int size = 20;
int [] BSTarray = new int [size];
for(int i = 0; i <size; i++) {
makeArray(root);
BSTarray[i] = root.getValue();
}
return BSTarray;
}
//helper method called by toBSTArray
public void makeArray(BinarySearchTreeNode node) {
if (node != null) {
makeArray(node.getLeft());
makeArray(node.getRight());
// System.out.print(node.getValue() + " ");
}
}
I thought this method was supposed to go through the tree and add in the values it finds into different indexes in the BSTarray, but all it's doing is adding the same number into all the indexes in the array. Am I doing something wrong with the recursion?
Try this:
Integer[] values = extractValues(n).toArray(new Integer[] {});
with that method definition:
private static List<Integer> extractValues(Node n) {
List<Integer> result = new ArrayList<>();
if (n.getLeft() != null) {
result.addAll(extractValues(n.getLeft()));
}
if (n.getRight() != null) {
result.addAll(extractValues(n.getRight()));
}
result.add(n.getValue());
return result;
}
I assumed a node structure that is similar to yours. Of course, the method doesn't have to be static if you don't use it in a static way.
This method might not be the most efficient due to the list conversion but you don't have to bother with any array sizes. If you really need the function to return an array, just wrap it into another function or let the proposed function return an array (this would make it necessary to convert the list to an array before each return).
Concerning your code, you iterate over the ito fill the entire array (no matter where you know the size from) but you always set the value to the value of the root node. That's why you always have the same value. Your makeArray function calls itself recursively but it doesn't do anything (even if you add a sysout statement ;) )
Update:
And for the constraint of using no lists, here is another version that uses only arrays:
int size = 20;
int[] results = new int[size];
extractValues(n, results, 0);
with the method definition:
private static int extractValues(Node n, int[] results, int index) {
if (n.getLeft() != null) {
index = extractValues(n.getLeft(), results, index);
}
if (n.getRight() != null) {
index = extractValues(n.getRight(), results, index);
}
results[index] = n.getValue();
return index + 1;
}
Note, that the result will be in results, then. The size has to be either assumed to be larger the number of nodes or it has to be counted by traversing the tree, before.
How about this: (Your recursion does not make any changes to the array)
public int [] toBSTArray() {
int size = 20; //ASSUMING THIS IS LESS THAN OR EQUAL TO NUMBER OF NODES IN THE TREE
int [] BSTarray = new int [size];
makeArray(root, 0, BSTarray);
return BSTarray;
}
//helper method called by toBSTArray
public void makeArray(BinarySearchTreeNode node, int i, int [] BSTarray ) {
if (node != null) {
BSTarray[i] = root.getValue();
makeArray(node.getLeft(), 2*i+1, BSTarray);
makeArray(node.getRight(), 2*i+2, BSTarray);
}
}

Logical Error in finding subsets

I'm trying to generalize the code to find all subsets of a given string(elements that are repeated will be treated as distinct) into one that would work for any list.
public class Subsets{
private static <T> void RecursiveSubsets(List<List<T>> list, ArrayList<T> soFar, List<T> rest)
{
if(rest.isEmpty())
{
list.add(soFar);
}
else
{
List<T> remaining;
if(rest.size() == 1)
{
remaining = new ArrayList<T>();
}
else
{
remaining = rest.subList(1, rest.size() - 1);
}
//include the element
ArrayList<T> includeFirst = new ArrayList<T>(soFar);
includeFirst.add(rest.get(0));
RecursiveSubsets(list, includeFirst, remaining);
//exclude the element
RecursiveSubsets(list, soFar, remaining);
}
}
public static <T> List<List<T>> getAllSubsets(List<T> set)
{
List<List<T>> subsets = new ArrayList<List<T>>();
RecursiveSubsets(subsets,new ArrayList<T>(),set);
return subsets;
}
public static void main(String [] args)
{
List<Integer> ints = new ArrayList<Integer>(){
{
add(0);add(1);add(2);add(3);
}
};
List<List<Integer>> allSubsets = getAllSubsets(ints);
System.out.println("Total Subsets returned : " + allSubsets.size());
for(int i=0; i<allSubsets.size(); ++i)
{
for(int j=0; j<allSubsets.get(i).size(); ++j)
{
System.out.print(allSubsets.get(i).get(j) + " ");
}
System.out.println();
}
}
}
After a few attempts I was able to get this to compile but this is what I get as output.
Even if I have more integers, it still returns this. I'm not able to figure out what I have missed and need help finding it.
$ java Subsets
Total Subsets returned : 4
0 1
0
1
Your program is actually almost correct, and you just have the sublist logic a bit wrong.
The javadoc for List.sublist says
Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.
The word "exclusive" here is critical.
If you just change
remaining = rest.subList(1, rest.size() - 1);
to
remaining = rest.subList(1, rest.size());
your code works.
The logic of this (in pseudocode) is typically:
List<List<T>> subsets( List<T> list ){
if( list is empty ) return a list containing the empty list;
// else:
subsetsWithout = subsets( list w/o 0th element );
result.addAll(subsetsWithout);
for( subset in subsetsWithout )
result.add( subset + list[0] )
return result;
}
It looks like what you're doing is different, and the fact that you're trying to return things through the function parameters is making it more confusing.

Find a path algorithm in a graph using java

I have to implement a search algorithm in java for a school project. In this algorithm i need to find, in an undirected graph, a path that goes through each link only once and ends in the start node. I'm trying to use a DFS with backtracking to solve this problem, but i'm having trouble implementating it. Here's my code:
import java.util.*;
public class Graph {
private Map<Integer, LinkedHashSet<Integer>> map =
new HashMap<Integer, LinkedHashSet<Integer>>();
private int startNode;
private int numLinks;
public Graph(int startNode, int numLinks) {
super();
this.startNode = startNode;
this.numLinks = numLinks;
}
public void addEdge(int source, int destiny) {
LinkedHashSet<Integer> adjacente = map.get(source);
if(adjacente==null) {
adjacente = new LinkedHashSet<Integer>();
map.put(source, adjacente);
}
adjacente.add(destiny);
}
public void addLink(int source, int destiny) {
addEdge(source, destiny);
addEdge(destiny, source);
}
public LinkedList<Integer> adjacentNodes(int last) {
LinkedHashSet<Integer> adjacente = map.get(last);
System.out.println("adjacentes:" + adjacente);
if(adjacente==null) {
return new LinkedList<Integer>();
}
return new LinkedList<Integer>(adjacente);
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int numVertices = input.nextInt();
int numLinks = input.nextInt();
int startNode = input.nextInt();
int endNode = startNode;
Graph mapa = new Graph(startNode, numLinks);
for(int i = 0; i<numLinks; i++){
mapa.addLink(input.nextInt(), input.nextInt());
}
List<ArrayList<Integer>> paths = new ArrayList<ArrayList<Integer>>();
Integer currentNode = startNode;
List<Integer> visited = new ArrayList<Integer>();
visited.add(startNode);
mapa.findAllPaths(mapa, visited, paths, currentNode);
for(ArrayList<Integer> path : paths){
for (Integer node : path) {
System.out.print(node);
System.out.print(" ");
}
System.out.println();
}
}
private void findAllPaths(Graph mapa, List<Integer> visited,
List<ArrayList<Integer>> paths, Integer currentNode) {
if (currentNode.equals(startNode)) {
paths.add(new ArrayList<Integer>(visited));
return;
}
else {
LinkedList<Integer> nodes = mapa.adjacentNodes(currentNode);
for (Integer node : nodes) {
if (visited.contains(node)) {
continue;
}
List<Integer> temp = new ArrayList<Integer>();
temp.addAll(visited);
temp.add(node);
findAllPaths(mapa, temp, paths, node);
}
}
}
}
The program is supposed to receive integers on his input, where the first one is the number of nodes, the second one is the number of links, the third is the start node(wich is also the end node), all the integers that come after represent the links between nodes.
The goal is to, in the end, print a single line with integers. This integers represent the order i visit each node to complete the path. Currently testing, it only prints a single integer, wich represents the first node.
I think my problem is either populating the graph, populating the adjacent list. Can somebody help me?
The problem is that when you call mapa.findAllPaths(mapa, visited, paths, currentNode);, you don't actually find all paths. You only find one path (i.e. the current node) and you return:
private void findAllPaths(Graph mapa, List<Integer> visited,
List<ArrayList<Integer>> paths, Integer currentNode) {
if (currentNode.equals(startNode)) {
paths.add(new ArrayList<Integer>(visited));
return;// <--- WRONG!!!
} else {
// The else is never executed!
}
}
You should either have a loop or recursively call the findAllPaths until you find all the paths.
The first time you invoke the findAllPaths method you are passing the startNode as the last argument (the currentNode), which leads to currentNode.equals(startNode) being true and as such the only part of the method that gets executed is:
paths.add(new ArrayList<Integer>(visited));
return;
In essence, you only add the first node to your paths and then your algorithm finishes, thus always printing a single integer, the start node.

Search algorithm (graph) in a single java file

I have to implement a search algorithm for a school assignment. Right now, i'm having problems with sole java implementation. Here's the code i have at the moment (i have been basing myself in some code i found here in stackoverflow for the dfs search, then i have to add verifications to meet the project criteria):
import java.util.*;
public class dfs<Grafo> {
public static void main(String[] args) {
class Grafo{
private Map<Integer, LinkedHashSet<Integer>> map = new HashMap();
public void addEdge(int source, int destiny) {
LinkedHashSet<Integer> adjacente = map.get(source);
if(adjacente==null) {
adjacente = new LinkedHashSet();
map.put(source, adjacente);
}
adjacente.add(destiny);
}
public void addLink(int source, int destiny) {
addEdge(source, destiny);
addEdge(destiny, source);
}
public LinkedList<Integer> adjacentNodes(int last) {
LinkedHashSet<Integer> adjacente = map.get(last);
if(adjacente==null) {
return new LinkedList();
}
return new LinkedList<Integer>(adjacente);
}
}
Scanner input = new Scanner(System.in);
int numVertices = input.nextInt();
int numLinks = input.nextInt();
int startNode = input.nextInt();
int endNode = startNode;
Grafo mapa = new Grafo();
for(int i = 0; i<numLinks; i++){
mapa.addLink(input.nextInt(), input.nextInt());
}
List<ArrayList<Integer>> paths = new ArrayList<ArrayList<Integer>>();
Integer currentNode = startNode;
List<Integer> visited = new ArrayList<Integer>();
visited.add(startNode);
new dfs().findAllPaths(mapa, visited, paths, currentNode);
for(ArrayList<Integer> path : paths){
for (Integer node : path) {
System.out.print(node);
System.out.print(" ");
}
System.out.println();
}
}
private void findAllPaths(Grafo mapa, List<Integer> visited,
List<ArrayList<Integer>> paths, Integer currentNode) {
if (currentNode.equals(startNode)) {
paths.add(new ArrayList(Arrays.asList(visited.toArray())));
return;
}
else {
LinkedList<Integer> nodes = mapa.adjacentNodes(currentNode);
for (Integer node : nodes) {
if (visited.contains(node)) {
continue;
}
List<Integer> temp = new ArrayList<Integer>();
temp.addAll(visited);
temp.add(node);
findAllPaths(mapa, temp, paths, node);
}
}
}
}
The problem i have with this, is that i can only turn in a single java file, so i'm putting everything in this single file.
When i get to the findAllPaths function he does not recognise the "startNode" constant (eclipse says that it cannot be be resolved to a variable) and he says "adjacentNodes" function is not defined for the type Grafo.
Is there anyway i can solve this problem or do i have to rethink the way i'm doing this, if so, what's a good way to implement this?
Others seem to have addressed some of your coding issues, so I just wanted to illustrate how to work with the single file constraint.
Suppose you had 3 classes, Edge, Node, and Graph that you developed in three separate files. Here's how you could combine them:
// in file GraphSearchHomework.java
public class GraphSearchHomework {
public static class Edge {
// ... Edge code here ...
// You can reference Nodes and Graphs
// just as if this was in a separate file
}
public static class Node {
// ... Node code here ...
// You can reference Edges and Graphs
// just as if this was in a separate file
}
public static class Graph {
// ... Graph code here ...
// You can reference Edges and Nodes
// just as if this was in a separate file
}
public static void main(String args[]) {
// This main can instantiate Graphs, Nodes, Edges
// Keep this simple, though. Most code belongs outside of main.
}
}
You seem to have made some mistakes trying to force this into one file. If it helps you, go ahead and develop each class separately and then combine them.
I cleaned out your errors and warnings for you.
Still doesn't run well, but you can sort that out on your own..
Looks like you don't need the outer class dts. If you make the Grafo your top level class it becomes much cleaner.
If you make the startNode a class variable you can instantiate it in your main method and you can access it in your other methods.
Also when you instantiate generic classes you should specify the generic type like this:
new LinkedList<Integer>();
I wouldn't hand it in just yet, but at least the structure is a bit cleaner..
import java.util.*;
public class Grafo {
private Map<Integer, LinkedHashSet<Integer>> map = new HashMap<Integer, LinkedHashSet<Integer>>();
private int startNode;
public Grafo(int startNode) {
super();
this.startNode = startNode;
}
public void addEdge(int source, int destiny) {
LinkedHashSet<Integer> adjacente = map.get(source);
if(adjacente==null) {
adjacente = new LinkedHashSet<Integer>();
map.put(source, adjacente);
}
adjacente.add(destiny);
}
public void addLink(int source, int destiny) {
addEdge(source, destiny);
addEdge(destiny, source);
}
public LinkedList<Integer> adjacentNodes(int last) {
LinkedHashSet<Integer> adjacente = map.get(last);
if(adjacente==null) {
return new LinkedList<Integer>();
}
return new LinkedList<Integer>(adjacente);
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int numVertices = input.nextInt();
int numLinks = input.nextInt();
int startNode = input.nextInt();
int endNode = startNode;
Grafo mapa = new Grafo(startNode);
for(int i = 0; i<numLinks; i++){
mapa.addLink(input.nextInt(), input.nextInt());
}
List<ArrayList<Integer>> paths = new ArrayList<ArrayList<Integer>>();
Integer currentNode = startNode;
List<Integer> visited = new ArrayList<Integer>();
visited.add(startNode);
mapa.findAllPaths(mapa, visited, paths, currentNode);
for(ArrayList<Integer> path : paths){
for (Integer node : path) {
System.out.print(node);
System.out.print(" ");
}
System.out.println();
}
}
private void findAllPaths(Grafo mapa, List<Integer> visited,
List<ArrayList<Integer>> paths, Integer currentNode) {
if (currentNode.equals(startNode)) {
paths.add(new ArrayList<Integer>(visited));
return;
}
else {
LinkedList<Integer> nodes = mapa.adjacentNodes(currentNode);
for (Integer node : nodes) {
if (visited.contains(node)) {
continue;
}
List<Integer> temp = new ArrayList<Integer>();
temp.addAll(visited);
temp.add(node);
findAllPaths(mapa, temp, paths, node);
}
}
}
}
startNode is a local variable in main(), and thus is not recognized by findAllPaths() [remember, one might invoke this from a method which is not main...] java has static binding so it forbids this.
You can add it as a parameter to findAllPaths(), or you can make startNode a field in your class dfs, and then findAllPaths() will be able to access it.
Same idea for Grafo - you declared it as a method inner class, you should declare it as an inner class for the class - otherwise only main() will "know" how to use it.

Categories

Resources