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

Generic binary search tree implementation

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

Problem

I use this interface for my BST node class:

public interface BinNode {
    public E getValue();
    public void setValue(E value);

    public BinNode getLeftChild();

    public BinNode getRightChild();

    public boolean isLeaf();
}


My BSTNode is implemented as follows:

public class BinarySearchTreeNode implements BinNode {
    private Key key;
    private E value;

    private BinarySearchTreeNode leftChild;
    private BinarySearchTreeNode rightChild;

    public BinarySearchTreeNode(Key key, E value,
        BinarySearchTreeNode leftChild,
        BinarySearchTreeNode rightChild) {
    this.key = key;
    this.value = value;
    this.leftChild = leftChild;
    this.rightChild = rightChild;
    }

    public Key getKey() {
    return this.key;
    }

    public void setKey(Key key) {
    this.key = key;
    }

    @Override
    public E getValue() {
    return this.value;
    }

    @Override
    public void setValue(E value) {
    this.value = value;
    }

    @Override
    public BinarySearchTreeNode getLeftChild() {
    return this.leftChild;
    }

    public void setLeftChild(BinarySearchTreeNode leftChild) {
    this.leftChild = leftChild;
    }

    @Override
    public BinarySearchTreeNode getRightChild() {
    return this.rightChild;
    }

    public void setRightChild(BinarySearchTreeNode rightChild) {
    this.rightChild = rightChild;
    }

    @Override
    public boolean isLeaf() {
    if (this.leftChild == null && this.rightChild == null) {
        return true;
    } else {
        return false;
    }
    }
}


And finally my binary search tree is implemented as:

```
public class BinarySearchTree, E>
implements Dictionary {

private BinarySearchTreeNode rootNode;
private int numberOfNodes;

public BinarySearchTree() {
this.rootNode = null;
this.numberOfNodes = 0;
}

@Override
public void clear() {
this.rootNode = null;
this.numberOfNodes = 0;
}

@Override
public void ins

Solution

When you have a custom implementation of something, and it has internal structures, like, in your case, the BinarySearchTreeNode, there is no real reason to have the interface for it. There is no public use of the interface, and not even your actual tree uses it. It is redundant. You can delete it, and remove the reference from the BinarySearchTreeNode

Additionally, there is no need for the BinarySearchTreeNode to be public. You never return the instance from the Tree, so there is no reason to expose it. Leaving it package-private (no public or private declaration) would be a decent choice, but commonly, the class is actually nested as a private-static inner class in the actual tree.

I am not sure what the Dictionary<> interface is ... Oh... really? java.util.Dictionary ... this should be java.util.Map since the documentation for Dictionary (which, in 10 years, I have never seen before now) says:


NOTE: This class is obsolete. New implementations should implement the Map interface, rather than extending this class.

Apart from that, the class looks pretty good.

public class BinarySearchTree, E>
implements Map {

    private static class BinarySearchTreeNode {

        ....

   }

   .....

}

Code Snippets

public class BinarySearchTree<Key extends Comparable<? super Key>, E>
implements Map<Key, E> {

    private static class BinarySearchTreeNode<Key, E> {

        ....

   }

   .....

}

Context

StackExchange Code Review Q#32270, answer score: 3

Revisions (0)

No revisions yet.