HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Doing tree search in Java

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
javasearchdoingtree

Problem

I have this tiny library for building unbalanced, binary trees and doing tree search on them. I tried hard to define a generic API that would allow custom implementations of trees.

AbstractBinaryTreeNode.java:

package net.coderodde.graph.tree;

/**
 * This class defines the API for a binary tree node.
 * 
 * @author Rodion Efremov
 * @version 1.6
 * @param  the type of satellite data.
 * @param  the actual node implementation type.
 */
public abstract class
        AbstractBinaryTreeNode, 
                               N extends AbstractBinaryTreeNode> {

    /**
     * Stores the satellite datum of this node.
     */
    protected final E element;

    /**
     * The left child node.
     */
    protected N left;

    /**
     * The right child node.
     */
    protected N right;

    /**
     * Constructs a new binary tree node with element as the 
     * satellite datum.
     * 
     * @param element the satellite datum.
     */
    public AbstractBinaryTreeNode(final E element) {
        checkNotNull(element);
        this.element = element;
    }

    /**
     * Returns the element of this node.
     * 
     * @return the element of this node.
     */
    public E getElement() {
        return element;
    }

    /**
     * Returns the left child node.
     * 
     * @return the left child node.
     */
    public N getLeft() {
        return left;
    }

    /**
     * Returns the right child node.
     * 
     * @return the right child node.
     */
    public N getRight() {
        return right;
    }

    /**
     * Checks that the input element is not null.
     * 
     * @param element the element to check.
     */
    private void checkNotNull(final E element) {
        if (element == null) {
            throw new NullPointerException(
                    "The element for a " + getClass().getSimpleName() + 
                    " is null.");
        }
    }
}


AbstractTreeNodeFinder.java:

```
package net.coderodde.graph.tree;

import java.uti

Solution

On the whole, your code is neat, the style is good, generics look OK. I have a concern with two major 'design' points though.

The first is the AbstractBinaryTreeNode class. This encapsulates the basic functionality of a node in the binary tree, and it makes it seem that there would be a way to customize the implementation.

Unfortunately, you can't.

Your BinaryTree class creates a concrete implementation as a static-internal:

public static final class BinaryTreeNode> 
   extends AbstractBinaryTreeNode> {


and then only uses this implementation for the root:

/**
 * The root node of this tree.
 */
private BinaryTreeNode root;


Thus the whole thing is moot, and redundant.

The other issue I see is in the AbstractTreeNodeFinder. This has two problems.

The first problem is the concrete method tracebackPath, which has lousy JavaDoc, so I am not sure of its purpose, but the implementation reads like a static method...

I would consider removing that class entirely, and reducing it to a @FunctionalInterface, and then you can pass it in for each search.

There's abstraction overkill in there.

Code Snippets

public static final class BinaryTreeNode<E extends Comparable<? super E>> 
   extends AbstractBinaryTreeNode<E, BinaryTreeNode<E>> {
/**
 * The root node of this tree.
 */
private BinaryTreeNode<E> root;

Context

StackExchange Code Review Q#90294, answer score: 4

Revisions (0)

No revisions yet.