如何引用添加到二叉树的方法?

How do I reference a method to add to a binaryTree?

我的任务是构建代表摩尔斯电码的二叉树。每个点向左分支,每个破折号向右分支。

但是,我不明白为什么我添加节点的方法似乎不想与 BinaryTree 对象一起使用。 IntelliJ 说它 "can not resolve method".

我确定 BinaryTree 不是问题,因为我的导师详细指导了我如何编写 class。相反,我怀疑我可能在这里引用了错误的东西。我已经确认输入的参数不是问题所在。

 public static MorseCodeTree<Character> readMorseCodeTree()
 {
    MorseCodeTree<Character> morse = new MorseCodeTree<Character>();
    Node<Character> newNode = new Node<Character>(null);
    morse.addNode(newNode, letter, position);

    private Node<Character> addNode(Node<Character> currentNode, char data, String morseCode)
    {
    if (currentNode == null)
    {
        currentNode = new Node(null);
    }

    if (morseCode.charAt(0) == '*')
    {
        currentNode = addNode(currentNode.left, data, morseCode.substring(1));
    }
    else if (morseCode.charAt(0) == '-')
    {
        currentNode = addNode(currentNode.right, data, morseCode.substring(1));
    }
    else
    {
        currentNode.data = data;
    }
    return currentNode;
}

二叉树class:

进口java.io.Serializable; 导入 java.util.Scanner;

public class BinaryTree 实现 Serializable{

//implement Node class
protected static class Node<E> implements Serializable
{
    protected E data;
    protected Node<E> left;
    protected Node<E> right;

    public Node (E data)
    {
        this.data = data;
        this.left = null;
        this.right = null;
    }

    public String toString()
    {
        return data.toString();
    }
}

受保护的节点根;

public BinaryTree()
{
    root = null;
}

protected BinaryTree(Node<E> root)
{
    this.root = root;
}

public BinaryTree(E data, BinaryTree<E> leftTree, BinaryTree<E> rightTree)
{
    root = new Node<E>(data);
    if (leftTree != null)
    {
        root.left = leftTree.root;
    }
    else
    {
        root.left = null;
    }
    if (rightTree != null)
    {
        root.right = rightTree.root;
    }
    else
    {
        root.right = null;
    }
}

public BinaryTree<E> getLeftSubtree()
{
    if (root != null && root.left != null)
    {
        return new BinaryTree<E>(root.left);
    }
    else
    {
        return null;
    }
}

public BinaryTree<E> getRightSubtree()
{
    if (root != null && root.right != null)
    {
        return new BinaryTree<E>(root.right);
    }
    else
    {
        return null;
    }
}

public boolean isLeaf()
{
    return (root.left == null && root.right == null);
}

public String toString()
{
    StringBuilder sb = new StringBuilder();
    preOrderTraverse(root, 1, sb);
    return sb.toString();
}

private void preOrderTraverse(Node<E> node, int depth, StringBuilder sb)
{
    for (int i = 1; i < depth; i++)
    {
        sb.append(" ");
    }
    if (node == null)
    {
        sb.append("null\n");
    }
    else
    {
        sb.append(node.toString() + "\n");
        preOrderTraverse(node.left, depth + 1, sb);
        preOrderTraverse(node.right, depth + 1, sb);
    }
}

public static BinaryTree<String> readBinaryTree(Scanner scan)
{
    String data = scan.next();
    if (data.equals("null"))
    {
        return null;
    }
    else
    {
        BinaryTree<String> leftTree = readBinaryTree(scan);
        BinaryTree<String> rightTree = readBinaryTree(scan);
        return new BinaryTree<String>(data, leftTree, rightTree);
    }
}

}

您在 readMorseCodeTree() 中声明 addNode(...) 方法,因此它不在 class 的范围内。后一种方法应如下所示:

 public static BinaryTree<Character> readMorseCodeTree()
 {
    BinaryTree morse = new MorseCodeTree();
    Node<Character> newNode = new Node<Character>(null);
    morse.addNode(newNode, letter, position);
 }