我正在为类制作一个拼写检查程序,我需要在bst中插入一个大的.txt文件,这样我就可以遍历树并将每个单词与原始单词进行比较。我已经创建了bst本身,但是我不知道如何让.txt文件中的每一行都有自己的节点。这是bst代码
Node root;
public void addNode(int key, String word)
{
Node newNode = new Node(key, word);
if(root == null)
{
root = newNode;
}
else
{
Node focusNode = root;
Node parent;
while(true)
{
parent = focusNode;
if(key < focusNode.key)
{
focusNode = focusNode.leftChild;
if(focusNode == null)
{
parent.leftChild = newNode;
return;
}
}
else
{
focusNode = focusNode.rightChild;
if(focusNode == null)
{
parent.rightChild = newNode;
return;
}
}
}
}
}
public void preorderTraverseTree(Node focusNode)
{
if (focusNode != null)
{
System.out.println(focusNode);
preorderTraverseTree(focusNode.leftChild);
preorderTraverseTree(focusNode.rightChild);
}
}
}
节点代码
public class Node {
int key;
String word;
Node leftChild;
Node rightChild;
Node(int key, String word)
{
this.key = key;
this.word = word;
}
}
暂无答案!
目前还没有任何答案,快来回答吧!