I am looking at a Java program in which a text file containing integers is read into a simplified LinkedList, and then all the possible subsets of the list are considered. At the bottom of my code below, there is an OR operator, denoted by ||, in the middle of a return statement. How does the compiler decide whether to express the left side or the right side of the || operator? I debugged the program and stepped through it line by line, and I noticed that both sides are expressed, recursively. How can both parts of an or statement be expressed? I'm new to this, so I find it confusing. Any help would be appreciated.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Target {
/*--------------LINKED LIST------------------*/
private Node head;
private Node tail;
public void add(int obj) {
if (head == null) {
head = new Node(obj);
tail = head;
} else {
tail.next = new Node(obj);
tail = tail.next;
}
}
class Node {
public Node next;
public int item;
public Node(int item) {
this.item = item;
}
}
/*--------------LINKED LIST------------------*/
public Target() {
head = null;
}
public static void main(String args[]) throws FileNotFoundException {
Scanner fileIn = new Scanner(new File("in.txt"));
Target list = new Target();
while (fileIn.hasNext()) {
list.add(fileIn.nextInt()); //Read the file into the linkedlist.
}
fileIn.close();
System.out.println(list.recursiveSolution(list, 0));
}
public boolean recursiveSolution(Target list, int target) {
return subsetSumXOR(list.head, 0, 0, target);
}
private boolean subsetSumXOR(Node node, long sumOne, long sumTwo, int target) {
if (node == null) {
return (sumOne ^ sumTwo) == target; //base case, when node is null we've reached the end of the list.
}
return subsetSumXOR(node.next, sumOne + node.item, sumTwo, target) || subsetSumXOR(node.next, sumOne, sumTwo + node.item, target);
}
}