我想了解一個簡單的拼寫檢查程序。該程序基本上讀取一個字典文件,把這些單詞放在一棵樹中。當它執行檢查功能時,它會從文件中一次一個字地檢查,並使用retrieve()來查看這個單詞是否在樹中。關於生成樹的recusive方法的問題
我的問題是,我不明白這個「添加」方法是如何工作的。它應該將字典中的單詞添加到樹結構中。你能幫我解釋一下這是如何工作的嗎?非常感謝!
private static BinarySearchTree<StringItem, String> tree = new BinarySearchTree<StringItem, String>();
//Does this create a tree with StringItems as nodes?
//In which part does this method assign value to the tree nodes?
private static ArrayList<String> dictionary;//number is the number of strings in the dictionary
private static TreeNode<StringItem> add (int number) { // adds to the tree
//the numberOfWords has been counted in other methods.
if (numberOfWords < 1) {
return null;
}
TreeNode<StringItem> left = add(number/2); //What does this do?
StringItem word = new StringItem(dictionary.remove(0) + "");
TreeNode<StringItem> right = add((number - 1)/2);
return new TreeNode<StringItem>(word, left, right); // I don't understand this part.
//which node are we returning here?
}
非常感謝!並感謝非常有用的材料。我可以看到你寫的put方法是基於它的鍵爲節點賦值的?但是我看不到我發佈的add方法中的哪部分代碼執行此功能。我在代碼中評論了我的問題。再次感謝。 – user3735871