我正在尝试在huffman中构建值的路径,但我得到了stackoverflow异常。
我的代码:
public String findPath(short target, int root, String path)
{
String result;
if (root < 0)
{
if ((result = findPath(target, root, path + '0')) == null) {
result = findPath(target, root, path + '1');
}
}
else {
result = (target == this.LEAF_NODES[root]) ? path : null;
System.out.println("? " + result);
}
return result;
}
java.lang.StackOverflowError
at java.lang.AbstractStringBuilder.ensureCapacityInternal(Unknown Source)
at java.lang.AbstractStringBuilder.append(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.server.network.Client.findPath(Client.java:551)
at com.server.network.Client.findPath(Client.java:551)
at com.server.network.Client.findPath(Client.java:551)
at com.server.network.Client.findPath(Client.java:551)
at com.server.network.Client.findPath(Client.java:551)
at com.server.network.Client.findPath(Client.java:551)
at com.server.network.Client.findPath(Client.java:551)
at com.server.network.Client.findPath(Client.java:551)
at com.server.network.Client.findPath(Client.java:551)
at com.server.network.Client.findPath(Client.java:551)
at com.server.network.Client.findPath(Client.java:551)
551行是:
if ((result = findPath(target, root, path + '0')) == null) {
哪里可能是问题?
我开始搜索:
String path = client.findPath((short)2563, -1, "");
答案 0 :(得分:0)
我修好了它:
public String findPath(short target, int root, String path)
{
String result;
if (root < 0)
{
if ((result = findPath(target, this.INTERNAL_NODES[(-root << 1) + 0], path + '0')) == null) {
result = findPath(target, this.INTERNAL_NODES[(-root << 1) + 1], path + '1');
}
}
else {
result = (target == this.LEAF_NODES[root]) ? path : null;
System.out.println("? " + result);
}
return result;
}
INTERNAL_NODES数组包含左右节点。它的长度是LEAF_NODES.length * 2 - 2