100分求校正代码 本帖最后由 ADAM129XU 于 2009-10-20 19:33:05 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 改了某些地方,特别是add方法,至于main中所修改的部分代码,和你的相应原码作用是相同的.你可以对照原有代码查看,应该没有障碍.另外,最终输出的结果(System.out.println(BST[0].Search(searchword));)是没有啥意义的,输出为'[]',你可以跑跑看,下面是改过的代码:/*--------------------------------------------------------------------------------------BinarySearchTree.java--------------------------------------------------------------------------------------*/package other;public class BinarySearchTree<K extends Comparable<K>, V> { private class Node { K value; V linelist; Node left, right; Node(K val) { value = val; left = right = null; } Node(K val, Node leftChild, Node rightChild, V li) { value = val; left = leftChild; right = rightChild; linelist = li; } } public Node root = null; public BinarySearchTree(K x) { root = new Node(x); } public boolean add(K x, V li) { /*root = */add(x, li, root); return true; } public boolean contains(K x) { return contains(x, root); } public boolean isEmpty() { return root == null; } private Node add(K x, V li, Node bstree) { if (bstree == null) { Node n = new Node(x); n.linelist = li; return n; } if (x.compareTo(bstree.value) == 0) { bstree.linelist = li; } if (x.compareTo(bstree.value) < 0) { bstree.left = add(x, li, bstree.left); } else { bstree.right = add(x, li, bstree.right); } return bstree; } private boolean contains(K x, Node bstree) { if (bstree == null) { return false; } if (x.compareTo(bstree.value) == 0) { return true; } if (x.compareTo(bstree.value) < 0) { return contains(x, bstree.left); } else { return contains(x, bstree.right); } } public V getV(K x, Node bstree) { if (bstree == null) { return null; } if (x.compareTo(bstree.value) == 0) { return bstree.linelist; } if (x.compareTo(bstree.value) < 0) { return getV(x, bstree.left); } else { return getV(x, bstree.right); } } public V Search(K x) { return getV(x, root); } public Node getNode(K x, Node bstree) { if (bstree == null) { return null; } if (x.compareTo(bstree.value) == 0) { return bstree; } if (x.compareTo(bstree.value) < 0) { return getNode(x, bstree.left); } else { return getNode(x, bstree.right); } }}/*-----------------------------------------------------------------------------------Test.java-----------------------------------------------------------------------------------*/package other;import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.IOException;import java.util.ArrayList;import java.util.List;import java.util.Scanner;public class Test { public static void main(String[] args) throws IOException { String[] Letter = { "A", "B", "C" }; BinarySearchTree[] BST = new BinarySearchTree[3]; for (int i = 0; i < 3; i++) { BST[i] = new BinarySearchTree(Letter[i]); } Scanner keyboard = new Scanner(System.in); System.out.print("Enter the name of a file: "); String filename = keyboard.nextLine(); File file = new File(filename); if(file.exists()) System.out.println("file path-->"+file.getAbsolutePath());// Scanner inputFile = new Scanner(file); BufferedReader inputFile=new BufferedReader(new FileReader(file)); String line=null; while ((line=inputFile.readLine())!=null) { int linenumber = 1; String[] linewords = line.split("\\s+"); for (int i = 0; i < linewords.length; i++) { List<Integer> li = new ArrayList<Integer>(); switch (linewords[i].charAt(0)) { case 'A': if (BST[0].contains(linewords[i])) { li = (ArrayList<Integer>) BST[0].Search(linewords[i]); li.add(linenumber); } else BST[0].add(linewords[i], li); break; case 'B': if (BST[1].contains(linewords[i])) { li = (ArrayList<Integer>) BST[1].Search(linewords[i]); li.add(linenumber); } else BST[0].add(linewords[i], li); break; case 'C': if (BST[2].contains(linewords[i])) { li = (ArrayList<Integer>) BST[2].Search(linewords[i]); li.add(linenumber); } else BST[0].add(linewords[i], li); break; default: System.out.println(" "); } } linenumber++; } System.out.println("OVER*******"); System.out .println("Please enter the word you would like to search for:"); String searchword = keyboard.nextLine(); switch (searchword.charAt(0)) { case 'A': if (BST[0].contains(searchword)) { System.out.println(BST[0].Search(searchword)); } break; case 'B': BST[1].Search(searchword); break; case 'C': BST[2].Search(searchword); break; } inputFile.close(); }}/*---------------------------------------------------------------------------------1.txt---------------------------------------------------------------------------------*/ABC BAC CABBCA CBA ACB//输入测试:ABC//结果: [] 感谢各位了,现在只剩下一个问题了。就是怎样简化我这个程序,我不想用26个switch语句。还有那个26个字母的数组,我想用循环写出那个还有26个字母的数组,然后建立和查找树的时候取第一个字母,判断出来是26个字母的第几个,就去找那棵相应的树。想法就是这样。别的已经解决了。 可以使用hashmap 设置对应key值 。通过map来获取对应参数然后调用对应方法。 代码没仔细看 希望能有所帮助 大概看了看你的代码,你每个case:无非是BST[index].Search(linewords[i])中的下标index不太同所以直接封装成一个方法,index当成参数传进去:如:void simple(char index,int i,lineword[]) { //lineword,我没仔细看什么类型,自己加吧 index = Character.toUpperCase(index); //确保是大写 int intIndex = index - 65; //利用AsCii码,来确定下标,“A”-“Z” - 65 =0-25 if(BST[index].contains(linewords[i])) { li=(ArrayList <Integer>)BST[index].Search(linewords[i]); li.add(linenumber); BST[index].add(linewords[i],li); } li.add(linenumber); BST[index].add(linewords[i],li); }大概思路,可能有小问题 java写安装包 Java初学者,请各位帮帮忙 http://localhost:8080/index.jsp怎么打不开 多线程问题 大家都来帮忙或学习呀,如何得到一个指定的月中有几周? 谢谢 怎样自学才能增加自己的项目经验? Blog 中 图片不能显示问题~~~~ ▲▲▲ 高分: 姓名 插入到表里补了几个空格,在oracle下 ▲▲▲ 80分轻松就得——享受轻松的愉快和与人分享的快乐 JAVA是什么? 谁有jmf比较全面的学习文档啊? 本人怎么觉得AB都对呢!请给我个答案 谢谢。SCJP
--------------------------------------------------------------------------------------
BinarySearchTree.java
--------------------------------------------------------------------------------------
*/package other;public class BinarySearchTree<K extends Comparable<K>, V> {
private class Node {
K value;
V linelist; Node left, right; Node(K val) {
value = val;
left = right = null;
} Node(K val, Node leftChild, Node rightChild, V li) {
value = val;
left = leftChild;
right = rightChild;
linelist = li;
}
} public Node root = null; public BinarySearchTree(K x) {
root = new Node(x);
} public boolean add(K x, V li) {
/*root = */add(x, li, root); return true;
} public boolean contains(K x) {
return contains(x, root);
} public boolean isEmpty() {
return root == null;
} private Node add(K x, V li, Node bstree) {
if (bstree == null) {
Node n = new Node(x);
n.linelist = li;
return n;
}
if (x.compareTo(bstree.value) == 0) {
bstree.linelist = li;
}
if (x.compareTo(bstree.value) < 0) {
bstree.left = add(x, li, bstree.left);
} else {
bstree.right = add(x, li, bstree.right);
}
return bstree;
} private boolean contains(K x, Node bstree) {
if (bstree == null) {
return false;
}
if (x.compareTo(bstree.value) == 0) {
return true;
}
if (x.compareTo(bstree.value) < 0) {
return contains(x, bstree.left);
} else {
return contains(x, bstree.right);
}
} public V getV(K x, Node bstree) {
if (bstree == null) {
return null;
}
if (x.compareTo(bstree.value) == 0) {
return bstree.linelist;
}
if (x.compareTo(bstree.value) < 0) {
return getV(x, bstree.left);
} else {
return getV(x, bstree.right);
}
} public V Search(K x) { return getV(x, root); } public Node getNode(K x, Node bstree) {
if (bstree == null) {
return null;
}
if (x.compareTo(bstree.value) == 0) {
return bstree;
}
if (x.compareTo(bstree.value) < 0) {
return getNode(x, bstree.left);
} else {
return getNode(x, bstree.right);
}
}}
/*
-----------------------------------------------------------------------------------
Test.java
-----------------------------------------------------------------------------------
*/
package other;import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;public class Test {
public static void main(String[] args) throws IOException {
String[] Letter = { "A", "B", "C" }; BinarySearchTree[] BST = new BinarySearchTree[3];
for (int i = 0; i < 3; i++) { BST[i] = new BinarySearchTree(Letter[i]);
} Scanner keyboard = new Scanner(System.in); System.out.print("Enter the name of a file: ");
String filename = keyboard.nextLine(); File file = new File(filename);
if(file.exists())
System.out.println("file path-->"+file.getAbsolutePath());
// Scanner inputFile = new Scanner(file);
BufferedReader inputFile=new BufferedReader(new FileReader(file));
String line=null;
while ((line=inputFile.readLine())!=null) {
int linenumber = 1;
String[] linewords = line.split("\\s+");
for (int i = 0; i < linewords.length; i++) {
List<Integer> li = new ArrayList<Integer>();
switch (linewords[i].charAt(0)) {
case 'A': if (BST[0].contains(linewords[i])) {
li = (ArrayList<Integer>) BST[0].Search(linewords[i]);
li.add(linenumber); } else
BST[0].add(linewords[i], li);
break;
case 'B': if (BST[1].contains(linewords[i])) {
li = (ArrayList<Integer>) BST[1].Search(linewords[i]);
li.add(linenumber); } else
BST[0].add(linewords[i], li); break;
case 'C': if (BST[2].contains(linewords[i])) {
li = (ArrayList<Integer>) BST[2].Search(linewords[i]);
li.add(linenumber); } else
BST[0].add(linewords[i], li); break; default:
System.out.println(" ");
} } linenumber++;
}
System.out.println("OVER*******");
System.out
.println("Please enter the word you would like to search for:");
String searchword = keyboard.nextLine();
switch (searchword.charAt(0)) {
case 'A':
if (BST[0].contains(searchword)) {
System.out.println(BST[0].Search(searchword));
}
break;
case 'B':
BST[1].Search(searchword);
break;
case 'C':
BST[2].Search(searchword);
break;
} inputFile.close();
}
}/*
---------------------------------------------------------------------------------
1.txt
---------------------------------------------------------------------------------
*/
ABC BAC CAB
BCA CBA ACB
//输入测试:ABC
//结果: []
可以使用hashmap 设置对应key值 。通过map来获取对应参数然后调用对应方法。 代码没仔细看 希望能有所帮助
无非是BST[index].Search(linewords[i])中的下标index不太同
所以直接封装成一个方法,index当成参数传进去:
如:
void simple(char index,int i,lineword[]) { //lineword,我没仔细看什么类型,自己加吧
index = Character.toUpperCase(index); //确保是大写
int intIndex = index - 65; //利用AsCii码,来确定下标,“A”-“Z” - 65 =0-25
if(BST[index].contains(linewords[i])) {
li=(ArrayList <Integer>)BST[index].Search(linewords[i]);
li.add(linenumber);
BST[index].add(linewords[i],li);
}
li.add(linenumber);
BST[index].add(linewords[i],li);
}
大概思路,可能有小问题