0

I have written a method that searches for an entry in a binary search tree. It successfully searches for a value but i don't think it will find all duplicates. The reason i know it doesn't work is that i'm trying to write another method that returns an arraylist with all the duplicates of a search value. That method only returns the first value it finds. here are my two methods. for the arraylist method i think i should write a recursive method to get all the values but i'm not sure how to get that working.

public T getEntry(T entry) {
    T result = null;
    boolean found = false;

    BinaryNodeInterface<T> currentNode = getRootNode();
    while (!found && (currentNode != null)) {
        T currentEntry = currentNode.getData();

        if (entry.equals(currentEntry)) {
            result = currentEntry;
            found = true;
        } else if (entry.compareTo(currentEntry) < 0)
            currentNode = currentNode.getLeftChild();
        else
            currentNode = currentNode.getRightChild();
    }

    return result;
}

public ArrayList<T> getAllEntries(T searchVal) {
    BinaryNodeInterface<T> currentNode = getRootNode();
    ArrayList<T> array = new ArrayList<T>();
    T value = getEntry(searchVal);
    if (value == null)
        return array;
    else
        array.add(getEntry(searchVal));
    return array;
}
Rishabh Maurya
  • 1,448
  • 3
  • 22
  • 40
  • You might want to check the accepted answer to this: http://stackoverflow.com/questions/7707321/strategy-for-duplicate-entries-in-a-binary-search-tree – ajb Dec 10 '14 at 00:48

2 Answers2

0

This seems like a fun problem so I decided to code up a solution myself. This should perform your request.

//class variable with duplicates.
ArrayList<T> duplicates = new ArrayList<T>();

public ArrayList<T> getDuplicates(T searchVal, BinaryNodeInterface<T> currentNode) {
    if (currentNode == null)
        return;

    if (currentNode.getData().equals(searchVal)) 
        duplicates.add(currentNode.getData()); //not sure why you just want to store the data. You can change this to store the node, which would be more useful.
    else if (currentNode.getData().compareTo(searchVal) < 0)
        getDuplicates(searchVal, currentNode.getLeftChild());
    else
        getDuplicates(searchVal, currentNode.getRightChild());

}

public ArrayList<T> getAllEntries(T searchVal) {
    BinaryNodeInterface<T> root = getRootNode();
    T value = getEntry(searchVal);
    if (value == null)
        return null;
    else
        getDuplicates(searchVal, root);


    if (duplicates.size() > 1)
        System.out.println("Tree has " + duplicates.size() + " duplicates.");
    else
        System.out.println("There are no duplicates in the tree");
}
David.Jones
  • 1,413
  • 8
  • 16
0

Here is what i figured out. I just used something very similar to my getEntry method but added code that inserted into the arraylist if it had duplicates.

public T getEntry(T entry) {
    T result = null;
    boolean found = false;

    BinaryNodeInterface<T> currentNode = getRootNode();
    while (!found && (currentNode != null)) {
        T currentEntry = currentNode.getData();

        if (entry.equals(currentEntry)) {
            result = currentEntry;
        }
        if (entry.compareTo(currentEntry) < 0)
            currentNode = currentNode.getLeftChild();
        if(entry.compareTo(currentEntry) >= 0)
            currentNode = currentNode.getRightChild();
    }

    return result;
}

public ArrayList<T> getAllEntries(T searchVal) {

    ArrayList<T> array = new ArrayList<T>();
    T result = null;
    boolean found = false;

    BinaryNodeInterface<T> currentNode = getRootNode();
    while (!found && (currentNode != null)) {
        T currentEntry = currentNode.getData();

        if (searchVal.equals(currentEntry)) {
            array.add(currentEntry);
        }
        if (searchVal.compareTo(currentEntry) < 0)
            currentNode = currentNode.getLeftChild();
        if(searchVal.compareTo(currentEntry) >= 0)
            currentNode = currentNode.getRightChild();
    }
    return array;
}