我编写了一个在二叉搜索树中搜索条目的方法。它成功搜索了一个值,但我不认为它会找到所有重复项。我知道它不起作用的原因是我试图编写另一种方法来返回一个包含搜索值所有重复项的arraylist。该方法仅返回它找到的第一个值。这是我的两种方法。对于arraylist方法,我认为我应该编写一个递归方法来获取所有值,但我不知道如何使其工作。
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;
}
答案 0 :(得分:0)
这似乎是一个有趣的问题所以我决定自己编写解决方案。这应该执行您的请求。
//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");
}
答案 1 :(得分:0)
这是我想出来的。我刚刚使用了与getEntry方法非常相似的东西,但添加了插入到arraylist中的代码,如果它有重复的话。
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;
}
答案 2 :(得分:-1)
让我再试一次......我认为这有效
import java.util.ArrayList;
import java.util.Queue;
public ArrayList<T> T getallenteries(T entry) {
ArrayList<T> result = new ArrayList<T>();
Queue<T> q=(Queue<T>) new Queue<T>();
boolean found = false;
BinaryNodeInterface<T> currentNode = getRootNode();
while ((!found||!q.isEmpty())&¤tNode!=null) {
if(!found)T currentEntry = currentNode.getData();
if(found)T currentEntry = q.poll();
if (entry.equals(currentEntry)) {
result.add(currentEntry);
found=true;
if(currentNode.getLeftChild()!=null)q.add(currentNode.getLeftChild());
if(currentNode.getRightChild()!=null)q.add(currentNode.getRightChild());
}
else if (entry.compareTo(currentEntry) < 0)
if(!found) currentNode = currentNode.getLeftChild();
else{
if(!found) currentNode = currentNode.getRightChild();
}
}
return result;}