簡體   English   中英

Java:樹創建

[英]Java: Tree Creation

給定字母和每個字母的莫爾斯電碼值...

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

我試圖創建一棵樹,通過掃描代碼在樹中找到字母的位置,然后向左分支一個點,向右分支一個破折號。

我創建了一個節點類,其中包含典型的numberNode leftmorseCode numberNode 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("");
    }

我在指示的地方出現錯誤,關於出什么問題的任何想法?

首先,當您嘗試打印出值時,編譯器說的是System.out.println(n.getleft()。getletter()。toString())上的錯誤; //這里的錯誤?

每個字母都應該是它自己樹的根嗎? 如果假設它在樹上,則我將在開頭具有一個引用指針,該指針將包含對字母表中每個字母的引用,而分支出每個字母的節點將具有表示該字母的摩爾斯電碼的特定序列。 但是,如果我有空做任何事情,我將簡單地創建一個包含26個字符的數組,其中包含一個MorseCode對象,該對象具有兩個字段,一個字段包含字母,另一個字段包含與該字母關聯的摩爾斯電碼。

了解該程序的預期輸出並弄清正在讀取到arrayList的文件中的內容將很有幫助。 該數據是什么樣的?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM