[英]Recursive call create a stackOverflow
我來尋求你的幫助,我做了一個學校一個項目,要求我們創建一個霍夫曼程序,直到現在一切都很好......我的問題是我負責解碼我的文本的遞歸函數似乎很糟糕,因為它在 ~3500 遞歸后給我一個 stackOverflow 以構建一個長文本。 我嘗試了很多東西,但我想不出解決主要問題的想法,這似乎是遞歸的數量。 我將在這里向您展示我的主要功能。
這是編碼之一:
public static String codeText(String textString, HashMap<String, String> tableCode) {
if(textCode.isEmpty()) {
return textCode;
}
String encode = ""; // Final code
String[] lineSplit = texteString.split(""); // I use a string so I need to split it
for (String element : lineSplit) {
if (tableCode.containsKey(element)) {
encode += tableCode.get(element.toLowerCase());//I use lower case cause I don't know how to detect the capitals
} else {
encode += tableCode.get("?"); // If the char isn't in our huffman tree
}
}
return encode;
}
現在的主要問題,我真的不知道如何使它與眾不同
// Launcher to build our arrayOfString
public static String decodeText(Node huffman, Node huffmanFull, String textCode) {
String textDecode = ""; //Final text
ArrayList<String> arrayOfCode = new ArrayList<>(); //Code split into an array
String[] textSplit = textCode.split("");
for (String element : textSplit) {
arrayOfCode.add(element);
}
return decodeText2(huffman, huffmanFull, arrayOfCode, textDecode);
}
public static String decodeText2(Node huffman, Node huffmanFull, ArrayList<String> arrayOfCode, Node textDecode) {
if (huffman.vide()) {
return textDecode;
}
if (arrayOfCode.size() == 0) {
textDecode += huffman.getData();// We add the last char
return textDecode;
}
if (huffman.getData() == null) { // We travel arround the tree in order to find the associate char
if (arrayOfCode.get(0).equals("0")) { // if the code is 0 go left child
arrayOfCode.remove(0); //we remove the actual index
return decodeText2(huffman.left(), huffmanFull, arrayOfCode, textDecode);
} else if (arrayOfCode.get(0).equals("1")) {// 1 we go right
arrayOfCode.remove(0);
return decodeText2(huffman.right(), huffmanFull, arrayOfCode, textDecode);
}
} else {
the char from the code has been found
textDecode += huffman.getData() ; // we add it to the string
return decodeText2(huffman = huffmanFull, huffmanFull, arrayOfCode, textDecode); // We reset the huffman tree with the full one
}
return textDecode;
}
我希望它足夠清楚,謝謝你的指導。
擺脫對decodeText2()
最后一次調用。 您的教師希望您使用遞歸來解碼一個Huffman 代碼,您正在使用decodeText2()
的前兩個遞歸調用進行decodeText2()
。 您應該使用循環來調用遞歸例程來解碼霍夫曼代碼序列。 您不應該使用遞歸來循環所有輸入。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.