gpt4 book ai didi

java - 如何从递归 getHeight 方法修复堆栈溢出

转载 作者:行者123 更新时间:2023-12-02 09:57:23 28 4
gpt4 key购买 nike

当我运行计算二叉搜索树高度的方法的代码时,它会导致堆栈溢出错误,但仅限于具有多个节点的树(我的程序中的 BSTElements)。我已了解到这是由于错误的递归调用造成的,但无法识别我的代码中的问题。

public int getHeight() {

return getHeight(this.getRoot());
}

private int getHeight(BSTElement<String,MorseCharacter> element) {

int height=0;

if (element == null) {
return -1;
}

int leftHeight = getHeight(element.getLeft());
int rightHeight = getHeight(element.getRight());

if (leftHeight > rightHeight) {
height = leftHeight;
} else {
height = rightHeight;
}

return height +1;
}

完整代码如下:

public class MorseCodeTree {

private static BSTElement<String, MorseCharacter> rootElement;



public BSTElement<String, MorseCharacter> getRoot() {
return rootElement;
}

public static void setRoot(BSTElement<String, MorseCharacter> newRoot) {
rootElement = newRoot;
}



public MorseCodeTree(BSTElement<String,MorseCharacter> element) {
rootElement = element;
}

public MorseCodeTree() {
rootElement = new BSTElement("Root", "", new MorseCharacter('\0', null));
}
public int getHeight() {

return getHeight(this.getRoot());
}

private int getHeight(BSTElement<String,MorseCharacter> element) {

if (element == null) {
return -1;
} else {
int leftHeight = getHeight(element.getLeft());
int rightHeight = getHeight(element.getRight());


if (leftHeight < rightHeight) {
return rightHeight + 1;
} else {
return leftHeight + 1;
}
}
}
public static boolean isEmpty() {
return (rootElement == null);
}

public void clear() {
rootElement = null;
}

public static void add(BSTElement<String,MorseCharacter> newElement) {

BSTElement<String, MorseCharacter> target = rootElement;
String path = "";
String code = newElement.getKey();

for (int i=0; i<code.length(); i++) {
if (code.charAt(i)== '.') {
if (target.getLeft()!=null) {
target=target.getLeft();
} else {
target.setLeft(newElement);
target=target.getLeft();
}

} else {
if (target.getRight()!=null) {
target=target.getRight();
} else {
target.setRight(newElement);
target=target.getRight();
}
}
}
MorseCharacter newMorseChar = newElement.getValue();

newElement.setLabel(Character.toString(newMorseChar.getLetter()));
newElement.setKey(Character.toString(newMorseChar.getLetter()));
newElement.setValue(newMorseChar);

}

public static void main(String[] args) {
MorseCodeTree tree = new MorseCodeTree();
BufferedReader reader;

try {
reader = new BufferedReader(new FileReader(file));
String line = reader.readLine();


while (line != null) {

String[] output = line.split(" ");
String letter = output[0];
MorseCharacter morseCharacter = new MorseCharacter(letter.charAt(0), output[1]);

BSTElement<String, MorseCharacter> bstElement = new BSTElement(letter, output[1], morseCharacter);

tree.add(bstElement);

line = reader.readLine();

System.out.println(tree.getHeight());
}
reader.close();




} catch (IOException e) {
System.out.println("Exception" + e);
}

最佳答案

您向我们展示的代码似乎没有任何明显错误1

如果此代码为一棵小树提供了 StackOverflowException,则很可能意味着您的树创建不正确并且其中存在循环(循环)。如果您的递归算法在“树”中遇到循环,它将循环直到堆栈溢出2

为了确定这一诊断,我们需要查看一个 MVCE,其中包含构建展示该行为的示例树所需的所有代码。

<小时/>

1 - 高度计算中可能存在“相差一”错误,但这不会导致堆栈溢出。

2 - 当前的 Java 实现不进行尾部调用优化。

关于java - 如何从递归 getHeight 方法修复堆栈溢出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55896074/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com