以下是创建 Huffman 树的项目中文件的摘录。当“公共类HLinkedList”自行初始化时,HTreeNode的.next、.left、.right等方法的单独文件(也如下所示)在HLinkedList的文件中工作没有问题。一旦“extends java.util.Abstract~”放置到位,HTreeNode 方法就不能被引用,我不得不将 HTreeNode 复制粘贴到同一个文件中,这就是第一行代码显示它的原因。将这两者放在同一个文件中的问题是它会导致 HLinkedList.HTreeNode 和 HTreeNode 之间发生冲突(我认为),因为我保留了 HTreeNode 文件以防万一。
public class HLinkedList<HTreeNode> extends java.util.AbstractSequentialList<HTreeNode>
{
public class HTreeNode {
public HTreeNode left;
public HTreeNode right;
public HTreeNode next;
public int frequency;
public char value;
public String code;
public HTreeNode(int freq, char val, HTreeNode l, HTreeNode r, HTreeNode n, String code) // code is the path taken to this node, how to explain it in code?
{
value = val;
frequency = freq;
left = l;
right = r;
next = n;
code = ""; // just initialized ,but have to think through logic.
}
}
HTreeNode head;
static int nItem;
public HLinkedList() //constructor
{
head = null; //inital value
nItem = 0;//counter
}
public void insertIntoPosition(HTreeNode node, int position) //inserts into position
{
//first, the case where it's already in the list.
HTreeNode currNode = head;
while(currNode.next != null)
{
if(currNode.value == node.value)
{
currNode.frequency++;
}
currNode = currNode.next;
}
if(currNode.value == node.value)
{
currNode.frequency++;
}
HTreeNode 文件:
public class HTreeNode {
public static HTreeNode left;
public HTreeNode right;
public HTreeNode next;
public int frequency;
public char value;
public String code;
public HTreeNode(int freq, char val, HTreeNode l, HTreeNode r, HTreeNode n, String code) // code is the path taken to this node, how to explain it in code?
{
value = val;
frequency = freq;
left = l;
right = r;
next = n;
code = ""; // just initialized ,but have to think through logic.
}
}