如何引用添加到binaryTree的方法?

问题描述 投票:-1回答:1

我的任务是建立一个代表摩尔斯电码的BinaryTree。每个点分别向左和向右分支。

但是,我无法弄清楚为什么添加Node的方法似乎不想使用BinaryTree对象。 IntelliJ说它“无法解决方法”。

我确信BinaryTree不是问题,因为我得到了关于如何由我的教师编写课程的详细说明。相反,我怀疑我可能在这里引用了错误的东西。我已经验证输入的参数不是问题。

 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;
}

BinaryTree类:

import java.io.Serializable; import java.util.Scanner;

公共类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);
    }
}

}

java binary-tree
1个回答
0
投票

你在addNode(...)中声明了readMorseCodeTree()方法,所以它不属于该类的范围。后一种方法应如下所示:

 public static BinaryTree<Character> readMorseCodeTree()
 {
    BinaryTree morse = new MorseCodeTree();
    Node<Character> newNode = new Node<Character>(null);
    morse.addNode(newNode, letter, position);
 }
© www.soinside.com 2019 - 2024. All rights reserved.