@Test
 public void testSingleLeafNode() {
   TreeNode n1 = new LeafTreeNode(1.0);
   EvaluateTreeVisitor v1 = new EvaluateTreeVisitor();
   n1.accept(v1);
   assertEquals(1.0, v1.getResult(), DELTA);
 }
Exemplo n.º 2
0
 @Test
 public void testMaxDepth3() {
   TreeNode n0 = new TreeNode(0);
   TreeNode n1 = new TreeNode(1);
   n0.left = n1;
   int result = s.maxDepth(n0);
   assertTrue(result == 2);
 }
 @Test
 public void nonNumberNodeForUnaryMinus() {
   TreeNode n1 = new LeafTreeNode(1.0);
   TreeNode n2 = new LeafTreeNode(2.0);
   TreeNode n3 = new AdditionTreeNode(n1, n2);
   TreeNode n4 = new UnaryMinusTreeNode(n3);
   EvaluateTreeVisitor v1 = new EvaluateTreeVisitor();
   n4.accept(v1);
   assertEquals(-3.0, v1.getResult(), DELTA);
 }
 @Test
 public void testUnaryMinusBinaryTree() {
   TreeNode n1 = new LeafTreeNode(4.0);
   TreeNode n2 = new LeafTreeNode(5.0);
   TreeNode n3 = new UnaryMinusTreeNode(n1);
   TreeNode n4 = new UnaryMinusTreeNode(n2);
   TreeNode n5 = new MultiplicationTreeNode(n3, n4);
   EvaluateTreeVisitor v1 = new EvaluateTreeVisitor();
   n5.accept(v1);
   assertEquals(20.0, v1.getResult(), DELTA);
 }
Exemplo n.º 5
0
  @Test
  public void thisAlwaysPasses() {
    Solution sol = new Solution();

    TreeNode root = new TreeNode(3);

    root.left = new TreeNode(1);
    root.right = new TreeNode(5);

    root.left.right = new TreeNode(2);
    assertTrue(sol.isBalanced(root));
  }
 @Test
 public void treeEvalBalancedTree() {
   TreeNode n1 = new LeafTreeNode(1.0);
   TreeNode n2 = new LeafTreeNode(2.0);
   TreeNode n3 = new LeafTreeNode(3.0);
   TreeNode n4 = new LeafTreeNode(4.0);
   TreeNode n5 = new AdditionTreeNode(n1, n2);
   TreeNode n6 = new MultiplicationTreeNode(n3, n4);
   TreeNode n7 = new DivisionTreeNode(n6, n5);
   EvaluateTreeVisitor v1 = new EvaluateTreeVisitor();
   n7.accept(v1);
   assertEquals(4.0, v1.getResult(), DELTA);
 }
 @Test
 public void testMultiplicationNode() {
   TreeNode n1 = new LeafTreeNode(1.0);
   TreeNode n2 = new LeafTreeNode(2.0);
   TreeNode n3 = new MultiplicationTreeNode(n1, n2);
   TreeNode n4 = new MultiplicationTreeNode(n2, n1);
   EvaluateTreeVisitor v1 = new EvaluateTreeVisitor();
   n3.accept(v1);
   assertEquals(2.0, v1.getResult(), DELTA);
   EvaluateTreeVisitor v2 = new EvaluateTreeVisitor();
   n4.accept(v2);
   assertEquals(2.0, v2.getResult(), DELTA);
 }
Exemplo n.º 8
0
  @Test
  public void testTreeFindWorks() {
    TreeNode node1 = new TreeNode("one");
    TreeNode node1Child = new TreeNode("underNode1");
    TreeNode node1ChildChild = new TreeNode("underNode1Child");
    TreeNode node2 = new TreeNode("two");

    node1.add(node1Child);
    node1.add(node2);
    node1Child.add(node1ChildChild);

    assertEquals(node2, node1.find("two"));
    assertEquals(node1Child, node1.find("underNode1"));
    assertEquals(node1ChildChild, node1.find("underNode1Child"));

    assertNull(node1.find("bob"));
    assertNull(node1Child.find("one"));
  }
Exemplo n.º 9
0
  @Before
  public void init() {
    a = new TreeNode();
    TreeNode b = new TreeNode();
    TreeNode c = new TreeNode();
    TreeNode d = new TreeNode();
    TreeNode e = new TreeNode();
    TreeNode f = new TreeNode();
    TreeNode g = new TreeNode();

    g.setData("7");
    f.setData("6");
    e.setData("5");
    d.setData("4").setLeft(f);
    c.setData("3").setRight(g);
    b.setData("2").setLeft(d).setRight(e);
    a.setData("1").setLeft(b).setRight(c);

    maximumElement = new MaximumElement();
  }