您好我正在尝试实现一个BFS方法,该方法将整数作为参数并遍历所述整数的BST,我遇到了一些错误并需要一些帮助。
我正在使用4个类,一个QNode类,一个BTNode类,一个Queue类和一个BST类。根据Eclipse,问题在于BFS方法本身。首先是BFS方法。
public Data BFS(int key) {
BTNode temp = this.root;
Q.enqueue(temp);
N.add(temp.getRecord().getData_val());
while (!Q.isEmpty()) {
temp.setRecord(Q.dequeue());
if (temp.getRecord().getData_val() == key) {
System.out.println(temp.getRecord().getData_val());
return temp.getRecord();
}
else if (temp.hasLeft()) {
Q.enqueue(temp.getLeft());
N.add(temp.getRecord().getData_val());
}
else if (temp.hasRight())
{
Q.enqueue(temp.getRight());
N.add(temp.getRecord().getData_val());
}
}
return temp.getRecord();
}
接下来是队列,队列也是一个链接列表。
public class Queue {
private QNode first;
private QNode last;
private int size;
public boolean isEmpty() {
return (this.first == null);
}
public int getSize(){
return this.size;
}
public boolean enqueue(BTNode a) {
QNode n = new QNode();
n.setData(a.getRecord());
if (this.isEmpty()) {
first = n;
last = n;
size++;
return true;
}
else if (!this.isEmpty()) {
last.setNext(n);
last = n;
size++;
return true;
}
else
return false;
}
public Data dequeue() {
BTNode node = new BTNode();
if(this.isEmpty()) {
this.last = null;
return null;
}
else {
QNode h = this.first;
first = h.getNext();
node.setRecord(h.getData());
size--;
return node.getRecord();
}
}
节点只是简单的节点,如果你已经看到一个节点,你已经看到了它们。 Data类以整数的形式保存数据,节点携带它。该类的基本getter和setter。当我运行驱动程序时,我需要永远运行并向我发送一系列异常。我创建了一个新的树实例并插入了一堆Data值然后运行BFS方法......它给了我以下内容。
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Unknown Source)
at java.util.Arrays.copyOf(Unknown Source)
at java.util.ArrayList.grow(Unknown Source)
at java.util.ArrayList.ensureExplicitCapacity(Unknown Source)
at java.util.ArrayList.ensureCapacityInternal(Unknown Source)
at java.util.ArrayList.add(Unknown Source)
at BST.BFS(BST.java:222)
at driver.main(driver.java:26)
感谢您的投入。
**其中一个人想看我的BTNode课程,所以这里也是如此。
public class BTNode implements IBTNode
{
private int height;
private BTNode left;
private BTNode right;
private BTNode parent;
private Data record;
/***************************************************************************
* Constructor
*
*/
public BTNode()
{
height = 0;
left = null;
right = null;
parent = null;
record = null;
}
public BTNode(Data record)
{
height = 0;
left = null;
right = null;
parent = null;
this.record = record;
}
public int getHeight() {
return this.height;
}
public void setHeight(int i) {
this.height = i;
}
public BTNode getLeft() {
return this.left;
}
public void setLeft(BTNode v) {
this.left = v;
}
public BTNode getParent() {
return this.parent;
}
public void setParent(BTNode v) {
this.parent = v;
}
public Data getRecord() {
return this.record;
}
public void setRecord(Object rec) {
this.record = (Data) rec;
}
public BTNode getRight() {
return this.right;
}
public void setRight(BTNode v) {
this.right = v;
}
public boolean hasLeft() {
if(this.left == null)
return false;
else
return true;
}
public boolean hasRight() {
if(this.right == null)
return false;
else
return true;
}
}