【问题标题】:Java: Tree CreationJava:树的创建
【发布时间】:2015-12-04 23:46:29
【问题描述】:

给定字母表和每个字母的摩尔斯电码值...

a._
b_...
c_._.
d_..  //file to be read in
e.
f.._.

我正在尝试创建一棵树,通过扫描代码找到字母在树中的位置,然后向左分支以获得点,向右分支以获得破折号。

我用典型的numberNode leftnumberNode right 变量以及morseCodeletter 创建了一个节点类。这是我的功能。

aList 是从文件中读取的已创建节点的数组列表。 rootNode 是树的根,没有设置 lettermorsecode

    public static void createTree(ArrayList<numberNode> aList, numberNode rootNode)
{
    for (numberNode n : aList)  //for each numberNode in aList
    {
        int lengthOfCode = n.getmorseCode().length()-1;  //get the length of the morsecode
        numberNode currentNode = rootNode; //sets currentNode to the rootNode at first
        for (int i=0; i<lengthOfCode; i++) 
        {
            char c = n.getmorseCode().charAt(i); //for each char in morsecode until it gets to the end
            if (c == '.')
            {
                if (currentNode.getleft() = null) //if currentnode left is null
                {
                    numberNode newLeftNode = new numberNode(); //create new node
                    currentNode.setleft(newLeftNode); //set current node left to the new node
                    if (i == lengthOfCode)
                    {
                        currentNode.setleft(n); //if end of morse code, set the current node left's to n
                    }
                    else
                    {
                        currentNode = newLeftNode; //else change current node to the newly created leftnode
                    }

                }
                else //if current node left is not null
                {
                    if (i == lengthOfCode)
                    {
                        currentNode.setleft(n); //if at end of morse code
                    }
                    currentNode = currentNode.getleft(); //if not at end set current node to current node's left

                }

            }
            if (c == '_')
            {
                if (currentNode.right() =null)
                {
                    numberNode newRightNode = new numberNode();
                    currentNode.setleft(newRightNode);
                    if (i == lengthOfCode)
                    {
                        currentNode.setright(n);
                    }
                    else
                    {
                        currentNode = newRightNode;
                    }

                }
                else
                {
                    if (i == lengthOfCode)
                    {
                        currentNode.setright(n);
                    }
                    currentNode = currentNode.getright();

                }

            }
        }
    }
}

我有几个问题...

我的算法至少是正确的吗?

是否有另一种方法可以做到这一点而不会那么难看?

如果您需要查看我的更多代码,请随时提出。感谢您的宝贵时间,我真的很感激!

编辑:当前正在运行,但是当我尝试使用...打印输出时...

       for (numberNode n : nodeArray)
    {
        System.out.println(n.getletter());
        System.out.println(n.getleft().getletter().toString()); //error here
        System.out.println(n.getright().getletter());
        System.out.println("");
    }

我在指示的地方收到一个错误,关于出了什么问题有什么想法吗?

【问题讨论】:

  • 您能更准确地了解错误吗?你能在你的代码中放一些 println 来跟踪它,并验证你的算法吗?
  • 我得到一个 NPE,我至少在我的脑海里,验证了我的算法。在过去的几个小时里,我一直在运行它,似乎找不到任何问题。希望另一双眼睛能帮助我

标签: java object if-statement for-loop tree


【解决方案1】:

首先,当您尝试将值打印出来时,编译器所说的是 System.out.println(n.getleft().getletter().toString()); 上的错误。 //这里出错了?

每个字母都应该是它自己树的根吗?如果假设它位于树上,我将在开头有一个引用指针,该指针将包含对字母表中每个字母的引用,而从每个字母分支的节点将具有表示该字母的莫尔斯电码的特定序列。但是,如果我可以随意做任何事情,我只需创建一个包含 26 个字符的数组,其中包含一个 MorseCode 对象,该对象具有两个字段,一个包含字母,另一个包含与该字母关联的莫尔斯电码。

了解该程序的输出是什么并澄清您正在读入 arrayList 的文件中的内容会很有帮助。这些数据是什么样的?

【讨论】:

  • 那里是空指针异常错误。根据每个字母给定的莫尔斯电码中的._,每个字母都应该在一棵树上。输出现在不必是任何东西,只需构建树即可。
  • 我只是想打印出数据,这样我就可以看到每个节点的leftright 以查看树是否构建正确
  • 如果它崩溃并返回一个空指针,这意味着当你运行代码时没有左节点可以检索。这让我觉得也许你的代码没有创建左节点,或者如果第一个字母是 A,就不会有左节点。通常一棵树应该有叶子,无论它们是否包含数据。例如,如果你创建一个二叉搜索树,你从根开始,立即创建左叶和右叶,并将它们设置为空。如果第一个字母实际上是 A,请尝试 n.getRight.getLetter().toString() 并查看它是否崩溃。
  • 空节点是这样你不必处理空指针。您检查节点的值,如果它设置为 null,您的迭代器将不会继续。
猜你喜欢
  • 2021-04-06
  • 1970-01-01
  • 1970-01-01
  • 2021-10-28
  • 1970-01-01
  • 1970-01-01
  • 2013-12-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多