我很难找到一种方法来为字母分配 0 和 1。我修复了我的优先级队列,以便它将所有节点首先变成具有最高优先级的树。我对如何分配每个字母的价值一无所知。我正在考虑使用中序遍历,但在将位添加到字母时,我被困在该代码的外观上。非常感谢所有帮助!我的节点类如下:
private class Node{
Node right;
Node left;
Node parent;
char letter;
int value;
String binaryValue = "";
private Node(char c, int in, Node parent, Node left, Node right){
letter = c;
value = in;
this.left = left;
this.right = right;
this.parent = parent;
}
@SuppressWarnings("unused")
private void setRight(Node right){
this.right = right;
}
@SuppressWarnings("unused")
private void setLeft(Node left){
this.left = left;
}
private void setParent(Node parent){
this.parent = parent;
}
private Node getParent(){
return parent;
}
@SuppressWarnings("unused")
private void setWeight(int weight){
this.value += weight;
}
private void setBinary(String binary){
binaryValue = binary;
}
private String getBinary(){
return binaryValue;
}
}