I am making a method to create the codewords for a huffman tree. The symbol of the node to get the codeword from is passed into the method. I'm not exactly positive how to go about this it has to ret开发者_如何学Pythonurn an int[]. I coded up what I thought might work. How do I properly use int[] so that I can create an output such as 00101? Thanks
public int[] codeWordAsAry(int k) {
HuffTreeNode temp;
int[] codeWord;
int pos = 0;
temp = leaves[k];
while (temp.parentOf() != null){
if (temp.isLeftChild()){
codeWord[pos] = 1;
pos++;
}
else { //if isRightChild
codeWord[pos] = 0;
pos++;
}
}
return codeWord; }
Ok so I understand the initializing the size but now I'm just wondering if its possible using this way to print out something along the lines of 01011 or other combinations like is the way im doing the increment of positions correct in the int[] array. will that print out what im looking for?
You need to instantiate the integer array before attempting to access it.
For example,
int[] codeWord = new int[size];
精彩评论