예제 #1
0
  private static void getSum(
      int totalSum, int curVal, IntNode root, List<IntNode> path, Set<List<IntNode>> paths) {
    if (root == null) {
      if (totalSum == curVal) {
        paths.add(path);
      } else {
        int subSum = 0;
        for (int i = path.size() - 1; i >= 0; i--) {
          IntNode node = path.get(i);
          subSum += node.getValue();
          if (subSum == totalSum) {
            List<IntNode> newPath = path.subList(i, path.size());
            paths.add(newPath);
            //                        break;//keep if you only want shortest list
          }
        }
      }
      return;
    }

    //        System.out.println("Node:"+root.getValue()+" Sum:"+newSum);
    path.add(root);
    ArrayList<IntNode> pathCopy = Lists.newArrayList(path);
    getSum(totalSum, curVal, root.getLeftNode(), path, paths);
    getSum(totalSum, curVal, root.getRightNode(), pathCopy, paths);
  }
예제 #2
0
 private static void printPath(Set<List<IntNode>> paths) {
   for (List<IntNode> path : paths) {
     for (IntNode node : path) {
       System.out.print(node.getValue() + ",");
     }
     System.out.println();
   }
 }
예제 #3
0
  public void testAsBoolean() throws Exception {
    assertEquals(false, BooleanNode.FALSE.asBoolean());
    assertEquals(true, BooleanNode.TRUE.asBoolean());
    assertEquals(false, IntNode.valueOf(0).asBoolean());
    assertEquals(true, IntNode.valueOf(1).asBoolean());
    assertEquals(false, LongNode.valueOf(0).asBoolean());
    assertEquals(true, LongNode.valueOf(-34L).asBoolean());
    assertEquals(true, new TextNode("true").asBoolean());
    assertEquals(false, new TextNode("false").asBoolean());
    assertEquals(false, new TextNode("barf").asBoolean());
    assertEquals(true, new TextNode("barf").asBoolean(true));

    assertEquals(true, new POJONode(Boolean.TRUE).asBoolean());
  }
예제 #4
0
 public void testAsInt() throws Exception {
   assertEquals(9, IntNode.valueOf(9).asInt());
   assertEquals(7, LongNode.valueOf(7L).asInt());
   assertEquals(13, new TextNode("13").asInt());
   assertEquals(0, new TextNode("foobar").asInt());
   assertEquals(27, new TextNode("foobar").asInt(27));
   assertEquals(1, BooleanNode.TRUE.asInt());
 }
예제 #5
0
  public void insertValue(int value) {
    if (rootNode == null) {
      rootNode = new IntNode(value);
    } else {
      IntNode currentNode = rootNode;

      while (currentNode.getValue() != value) {
        if (value > currentNode.getValue()) { // Right child path
          if (currentNode.getRightChild() == null) {
            currentNode.setRightChild(new IntNode(value));
          }
          currentNode = currentNode.getRightChild();
        } else { // Left child path
          if (currentNode.getLeftChild() == null) {
            currentNode.setLeftChild(new IntNode(value));
          }
          currentNode = currentNode.getLeftChild();
        }
      }
    }
  }
예제 #6
0
  public boolean search(int value) {
    IntNode currentNode = rootNode;

    while (currentNode != null && (currentNode.getValue() != value)) {
      if (value > currentNode.getValue()) { // Continue down the right child path
        currentNode = currentNode.getRightChild();
      } else { // Continue down the left child path
        currentNode = currentNode.getLeftChild();
      }
    }
    // If the current node exists and its value is equal to the input, return true, else return
    // false
    return currentNode != null && (currentNode.getValue() == value);
  }
예제 #7
0
  public static void main(String[] args) {
    IntNode d = new IntNode(5);
    d.setLeftNode(new IntNode(2));
    d.setRightNode(new IntNode(4));

    IntNode e = new IntNode(7);
    e.setLeftNode(new IntNode(-2));

    IntNode f = new IntNode(9);
    IntNode g = new IntNode(10);

    IntNode b = new IntNode(-6);
    b.setLeftNode(d);
    b.setRightNode(e);

    IntNode c = new IntNode(-3);
    c.setLeftNode(f);
    c.setRightNode(g);

    IntNode a = new IntNode(1);
    a.setLeftNode(b);
    a.setRightNode(c);

    printSums(a, 2);
  }
예제 #8
0
 // Other methods
 public void compress() {
   IntNode lead = head.getLink();
   IntNode trail = head;
   while (lead != null) {
     if (lead.getData() == trail.getData()) {
       trail.setLink(lead.getLink());
       IntNode aLead = lead.getLink();
       lead = aLead.getLink();
       trail = trail.getLink();
     } else {
       lead = lead.getLink();
       trail = trail.getLink();
     }
   }
 }