这是来自文件的Java二进制搜索树的代码。代码正在从csv文件创建二叉搜索树。但是当我运行此代码时出现错误。是的。
**Exception in thread "main" java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(StringTokenizer.java:349)
at java.util.StringTokenizer.nextElement(StringTokenizer.java:407)
at binarytree.Node.<init>(Node.java:28)
at binarytree.BinaryTree.main(BinaryTree.java:241)**
以下是Node for String的代码:
import java.util.StringTokenizer;
public class Node {
String key;
int value;
Node left;
Node right;
Node(String line )
{
// parse the string into variables
StringTokenizer st = new StringTokenizer(line," ");
this.key= st.nextElement().toString();
this.value = Integer.parseInt(st.nextElement().toString());
}
Node(String key, int value) {
this.key = key;
this.value = value;
}
public String toString() {
return key + " " + value;
}
}
这里是树的代码,包括插入,搜索和排序。
import java.io.FileReader;
import java.io.IOException;
public class BinaryTree {
Node root;
int MAX = 0;
int MIN = 0;
/***
* Add Node to the tree
*@param Country Name
*@param Population
*/
public void addNode(String key, int value) {
// Create a new Node and initialize it
Node newNode = new Node(key, value);
// If no root this will become root
if (root == null) {
root = newNode;
} else {
// Set root as the Node and start traversing the tree
Node focusNode = root;
// Future parent for new Node
Node parent;
while (true) {
// root is the top parent
parent = focusNode;
// Check if the new node should go on the left of parent
if (key.compareToIgnoreCase(focusNode.key) < 0) {
// focus to the left child
focusNode = focusNode.left;
// If the left child has no children
if (focusNode == null) {
// then place the new node on the left of it
parent.left = newNode;
return;
}
} else {
// If we get here put the node on the right
focusNode = focusNode.right;
// If the right child has no children
if (focusNode == null) {
// then place the new node on the right of it
parent.right = newNode;
return;
}
}
}
}
}
/***
* Order the tree ascending by key
*@param node
*@return
*/
public Node orderTreeByCountry(Node node)
{
if(node != null)
{
orderTreeByCountry(node.left);
System.out.println(node);
orderTreeByCountry(node.right);
}
return node;
}
/***
* Find node by providing key name
*@param Node
*@return Node
*/
public Node findNode(String key) {
// Start at the top of the tree
Node focusNode = root;
// While node not found keep looking
while (focusNode.key.compareToIgnoreCase(key) != 0) {
// If we should search to the left
if (key.compareToIgnoreCase(focusNode.key) < 0) {
// Shift the focus Node to the left child
focusNode = focusNode.left;
} else {
// Shift the focus Node to the right child
focusNode = focusNode.right;
}
// The node wasn't found
if (focusNode == null)
return null;
}
return focusNode;
}
/***
* Get the value of node with specific key
*@param key
*@return Value
*/
public int getValueForKey(String key)
{
// Start at the top of the tree
Node focusNode = root;
// While node not found
while (focusNode.key.compareToIgnoreCase(key) != 0) {
// If we should search to the left
if (key.compareToIgnoreCase(focusNode.key) < 0) {
// Shift the focus Node to the left child
focusNode = focusNode.left;
} else {
// Shift the focus Node to the right child
focusNode = focusNode.right;
}
// The node wasn't found
if (focusNode == null)
return -1;
}
return focusNode.value;
}
/***
* Get how many nodes in the tree
*@param node
*@return number of nodes
*/
public int getNodesCount(Node node)
{
if(node == null) {
return 0;
} else {
int count = 1;
count += getNodesCount(node.left);
count += getNodesCount(node.right);
return count;
}
}
/***
* Get min value for tree which is based on key, value attributes
*@param node
*@return min value
*/
public int getMinValue(Node node)
{
if(node != null)
{
getMinValue(node.left);
if(MIN == 0 ) {
MIN = node.value;
}
if(MIN > node.value) {
MIN = node.value;
}
getMinValue(node.right);
}
return MIN;
}
/***
* Get max value for tree which is based on key, value attributes
*@param node
*@return max value
*/
public int getMaxValue(Node node)
{
if(node != null)
{
getMaxValue(node.left);
if(MAX == 0 ) {
MAX = node.value;
}
if(MAX < node.value) {
MAX = node.value;
}
getMaxValue(node.right);
}
return MAX;
}
public static void main(String[] args)
{
BufferedReader br;
BinaryTree btree = new BinaryTree();
try {
br = new BufferedReader(new
FileReader("C:\\Users\\8Users\\Desktop/countries.txt"));
String line = "";
while ((line = br.readLine()) != null) {
/*Create Country node from the line */
Node country = new Node(line);
btree.addNode(country.key, country.value);
}
br.close();
}
catch (FileNotFoundException e) {
System.out.println(e.getLocalizedMessage());
}
catch (IOException e) {
System.out.println(e.getLocalizedMessage());
}
btree.orderTreeByCountry(btree.root);
System.out.println("Number of Countries is : " +
btree.getNodesCount(btree.root));
System.out.println("Min Population is : " +
btree.getMinValue(btree.root));
System.out.println("Max Population is : " +
btree.getMaxValue(btree.root));
int population = btree.getValueForKey("xxx");
if(population== -1)
System.out.println("Sorry this Country is not in this btree");
else
System.out.println("Population is : " + population);
}
}
希望你们能帮助我解决这个问题:(
答案 0 :(得分:0)
如果你在java.util中看到nextToken()方法的实现。 StringTokenizer类如下:
public String nextToken() { this.currentPosition = this.newPosition >= 0 && !this.delimsChanged ? this.newPosition: this.skipDelimiters(this.currentPosition); this.delimsChanged = false; this.newPosition = -1; if (this.currentPosition >= this.maxPosition) { throw new NoSuchElementException(); } else { int arg0 = this.currentPosition; this.currentPosition = this.scanToken(this.currentPosition); return this.str.substring(arg0, this.currentPosition); } }
如上所示,抛出 NoSuchElementException “if(this.currentPosition&gt; = this.maxPosition)” 因此输入文件“C:\ Users \ 8Users \ Desktop / countries.txt”中必定存在问题。