Skip to main content
minor fix to formatting
Source Link
gb96
  • 121
  • 4

Note that in your implementation of  flattenInOrdeflattenInOrderr the if-clause if (tree.get().isLeft()) always returns, consequently the following "else"else is redundant and you should move the code in the else block outside (see code below).

Also note that the interface Function is pretty much java.util.function.Function provided in Java 8, so if you want to impress a potential employer for a Java job, learn and use Java 8 features wherever you can to demonstrate that you're technically at the forefront!

The following is your solution with the following changes:

  1. Java 8 Lambda expressions
  2. Added @Override annotation
  3. Removed unnecessary //end comments
  4. Removed redundant "else"
  5. Added final modifier where possible
/*
 * @see flatten.FlattenTree#flattenInOrder(flatten.Tree)
 */
@Override
public List<T> flattenInOrder(final Tree<T> tree) {
    if (tree == null)
        throw new IllegalArgumentException("Tree is null.");

    if (tree.get().isLeft()) {
        // Java 8:
        return Arrays.asList(tree.get().<T> ifLeft(p -> p));
    }
    return tree.get().ifRight(p -> {

        final List<T> nodes = new ArrayList<>();
        nodes.addAll(flattenInOrder(p.left()));
        nodes.addAll(flattenInOrder(p.middle()));
        nodes.addAll(flattenInOrder(p.right()));

        return nodes; // return all fetched nodes
    });
}

Note that in your implementation of flattenInOrder the if-clause if (tree.get().isLeft()) always returns, consequently the following "else" is redundant and you should move the code in the else block outside (see code below).

Also note that the interface Function is pretty much java.util.function.Function provided in Java 8, so if you want to impress a potential employer for a Java job, learn and use Java 8 features wherever you can to demonstrate that you're technically at the forefront!

The following is your solution with the following changes:

  1. Java 8 Lambda expressions
  2. Added @Override annotation
  3. Removed unnecessary //end comments
  4. Removed redundant "else"
  5. Added final modifier where possible
/*
 * @see flatten.FlattenTree#flattenInOrder(flatten.Tree)
 */
@Override
public List<T> flattenInOrder(final Tree<T> tree) {
    if (tree == null)
        throw new IllegalArgumentException("Tree is null.");

    if (tree.get().isLeft()) {
        // Java 8:
        return Arrays.asList(tree.get().<T> ifLeft(p -> p));
    }
    return tree.get().ifRight(p -> {

        final List<T> nodes = new ArrayList<>();
        nodes.addAll(flattenInOrder(p.left()));
        nodes.addAll(flattenInOrder(p.middle()));
        nodes.addAll(flattenInOrder(p.right()));

        return nodes; // return all fetched nodes
    });
}

Note that in your implementation of  flattenInOrder the if-clause if (tree.get().isLeft()) always returns, consequently the following else is redundant and you should move the code in the else block outside (see code below).

Also note that the interface Function is pretty much java.util.function.Function provided in Java 8, so if you want to impress a potential employer for a Java job, learn and use Java 8 features wherever you can to demonstrate that you're technically at the forefront!

The following is your solution with the following changes:

  1. Java 8 Lambda expressions
  2. Added @Override annotation
  3. Removed unnecessary //end comments
  4. Removed redundant "else"
  5. Added final modifier where possible
/*
 * @see flatten.FlattenTree#flattenInOrder(flatten.Tree)
 */
@Override
public List<T> flattenInOrder(final Tree<T> tree) {
    if (tree == null)
        throw new IllegalArgumentException("Tree is null.");

    if (tree.get().isLeft()) {
        // Java 8:
        return Arrays.asList(tree.get().<T> ifLeft(p -> p));
    }
    return tree.get().ifRight(p -> {

        final List<T> nodes = new ArrayList<>();
        nodes.addAll(flattenInOrder(p.left()));
        nodes.addAll(flattenInOrder(p.middle()));
        nodes.addAll(flattenInOrder(p.right()));

        return nodes; // return all fetched nodes
    });
}
deleted 69 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Note that in your implementation of flattenInOrder flattenInOrder the if-clause "if (tree.get().isLeft())"if (tree.get().isLeft()) always returns, consequently the following "else" is redundant and you should move the code in the else block outside (see code below).

  1. Java 8 Lambda expressions

    Java 8 Lambda expressions
  2. Added @Override annotation

    Added @Override annotation
  3. Removed unnecessary //end comments

    Removed unnecessary //end comments
  4. Removed redundant "else"

    Removed redundant "else"
  5. Added final modifier where possible

    /*
     * @see flatten.FlattenTree#flattenInOrder(flatten.Tree)
     */
    @Override
    public List<T> flattenInOrder(final Tree<T> tree) {
        if (tree == null)
            throw new IllegalArgumentException("Tree is null.");
    
        if (tree.get().isLeft()) {
            // Java 8:
            return Arrays.asList(tree.get().<T> ifLeft(p -> p));
        }
        return tree.get().ifRight(p -> {
    
            final List<T> nodes = new ArrayList<>();
            nodes.addAll(flattenInOrder(p.left()));
            nodes.addAll(flattenInOrder(p.middle()));
            nodes.addAll(flattenInOrder(p.right()));
    
            return nodes; // return all fetched nodes
            });
    }
    
    Added final modifier where possible
/*
 * @see flatten.FlattenTree#flattenInOrder(flatten.Tree)
 */
@Override
public List<T> flattenInOrder(final Tree<T> tree) {
    if (tree == null)
        throw new IllegalArgumentException("Tree is null.");

    if (tree.get().isLeft()) {
        // Java 8:
        return Arrays.asList(tree.get().<T> ifLeft(p -> p));
    }
    return tree.get().ifRight(p -> {

        final List<T> nodes = new ArrayList<>();
        nodes.addAll(flattenInOrder(p.left()));
        nodes.addAll(flattenInOrder(p.middle()));
        nodes.addAll(flattenInOrder(p.right()));

        return nodes; // return all fetched nodes
    });
}

Note that in your implementation of flattenInOrder the if-clause "if (tree.get().isLeft())" always returns, consequently the following "else" is redundant and you should move the code in the else block outside (see code below).

  1. Java 8 Lambda expressions

  2. Added @Override annotation

  3. Removed unnecessary //end comments

  4. Removed redundant "else"

  5. Added final modifier where possible

    /*
     * @see flatten.FlattenTree#flattenInOrder(flatten.Tree)
     */
    @Override
    public List<T> flattenInOrder(final Tree<T> tree) {
        if (tree == null)
            throw new IllegalArgumentException("Tree is null.");
    
        if (tree.get().isLeft()) {
            // Java 8:
            return Arrays.asList(tree.get().<T> ifLeft(p -> p));
        }
        return tree.get().ifRight(p -> {
    
            final List<T> nodes = new ArrayList<>();
            nodes.addAll(flattenInOrder(p.left()));
            nodes.addAll(flattenInOrder(p.middle()));
            nodes.addAll(flattenInOrder(p.right()));
    
            return nodes; // return all fetched nodes
            });
    }
    

Note that in your implementation of flattenInOrder the if-clause if (tree.get().isLeft()) always returns, consequently the following "else" is redundant and you should move the code in the else block outside (see code below).

  1. Java 8 Lambda expressions
  2. Added @Override annotation
  3. Removed unnecessary //end comments
  4. Removed redundant "else"
  5. Added final modifier where possible
/*
 * @see flatten.FlattenTree#flattenInOrder(flatten.Tree)
 */
@Override
public List<T> flattenInOrder(final Tree<T> tree) {
    if (tree == null)
        throw new IllegalArgumentException("Tree is null.");

    if (tree.get().isLeft()) {
        // Java 8:
        return Arrays.asList(tree.get().<T> ifLeft(p -> p));
    }
    return tree.get().ifRight(p -> {

        final List<T> nodes = new ArrayList<>();
        nodes.addAll(flattenInOrder(p.left()));
        nodes.addAll(flattenInOrder(p.middle()));
        nodes.addAll(flattenInOrder(p.right()));

        return nodes; // return all fetched nodes
    });
}
Source Link
gb96
  • 121
  • 4

Note that in your implementation of flattenInOrder the if-clause "if (tree.get().isLeft())" always returns, consequently the following "else" is redundant and you should move the code in the else block outside (see code below).

Also note that the interface Function is pretty much java.util.function.Function provided in Java 8, so if you want to impress a potential employer for a Java job, learn and use Java 8 features wherever you can to demonstrate that you're technically at the forefront!

The following is your solution with the following changes:

  1. Java 8 Lambda expressions

  2. Added @Override annotation

  3. Removed unnecessary //end comments

  4. Removed redundant "else"

  5. Added final modifier where possible

    /*
     * @see flatten.FlattenTree#flattenInOrder(flatten.Tree)
     */
    @Override
    public List<T> flattenInOrder(final Tree<T> tree) {
        if (tree == null)
            throw new IllegalArgumentException("Tree is null.");
    
        if (tree.get().isLeft()) {
            // Java 8:
            return Arrays.asList(tree.get().<T> ifLeft(p -> p));
        }
        return tree.get().ifRight(p -> {
    
            final List<T> nodes = new ArrayList<>();
            nodes.addAll(flattenInOrder(p.left()));
            nodes.addAll(flattenInOrder(p.middle()));
            nodes.addAll(flattenInOrder(p.right()));
    
            return nodes; // return all fetched nodes
            });
    }