Le problème que j'ai est que j'ai une recherche binaire trois, et tous les noeuds contiennent des valeurs de chaîne plutôt que des nombres entiers. Il obtient ces chaînes à partir d'un fichier txt et place les lignes de texte individuelles du fichier sous forme de nœuds dans l'arborescence. Il n'y a pas de problème avec ça.Trouver un mot dans un arbre de recherche binaire Java?
Mon problème est que j'ai besoin d'une méthode qui itère dans mon arbre et trouve un mot spécifique. J'ai déjà des classes séparées, BinarySearchTree et BinaryTreeNode pour servir de base à l'arbre que j'essaie de créer. Les mots qu'il doit trouver sont situés dans un fichier appelé "fichier de test de recherche copy.txt" et il doit écrire les mots qu'il trouve dans un autre fichier appelé "SearchResults.txt"
Je n'ai aucune idée de comment fais-le, alors je cherche des conseils.
C'est la méthode que je besoin d'aide avec:
public boolean SearchWord(String target){
//returns true if the target string exists in the dictionary
// otherwise it returns false
//ALSO you need to write the results of Search
//in an output file called "SearchResults.txt"
return false;
}
Voici tout mon code, à l'exclusion des deux autres classes mentionnées ci-dessus, si ça aide.
public class Dictionary {
public BinaryTreeNode theBinaryTree;
/**
* I need to read one string by one string
* and then insert it into a tree.
* I need to read all of the strings in the source file, line by line,
* and insert them into my dictionary.
* After readinga single string, it needs to put it into a tree.
* I first need to create the dictionary tree,
* and then implement these methods on the tree.
* The Dictionary type is string.
* @throws FileNotFoundException
*/
private BinaryTreeNode dictionaryTree; // this is the tree within your dictionary class
public Dictionary(String filePath) throws IOException{
BufferedReader br = new BufferedReader(new FileReader("Dictionary.txt"));
this.dictionaryTree = readFile(br);
br.close();
}
public BinaryTreeNode readFile(BufferedReader reader) throws IOException{
String word = reader.readLine();
if(word!=null){
return new BinaryTreeNode(word, readFile(reader), readFile(reader));
}else{
return new BinaryTreeNode() ; // empty node or null?
}
}
/**
* @return
* Once again, I already have this method written and modified
* within the BinarySearchTree class.
* I'm simply going to call it over here.
*/
public int CountWords(){
//returns the number of words in the dictionary
//This is just counting nodes.
BinarySearchTree Aria = new BinarySearchTree();
return Aria.countNodes(dictionaryTree);
}
public boolean SearchWord(String target){
//returns true if the target string exists in the dictionary
// otherwise it returns false
//ALSO you need to write the results of Search
//in an output file called "SearchResults.txt"
return false;
}
/**
* I already modified the print order method
* in BinarySearchTree
* to work with strings.
* So I just called it here on the dictionaryTree.
* @PrintOrderedDict()
*
* However, I also had to modify the method,
* so that it wrote whatever values the method recieved
* to teh output file.
*/
public void PrintOrderedDict() throws IOException{
//Print the dictionary words
//in order in a new file called "OrderedDictionary.txt".
//Just modify print order to work with strings.
try {
BinarySearchTree jojo = new BinarySearchTree();
FileWriter fstream = new FileWriter("OrderedDictionary.txt");
BufferedWriter out = new BufferedWriter(fstream);
out.write(jojo.inorderPrint(dictionaryTree));
out.close();}
catch (Exception e) {
System.err.println("Error");
}
}
public boolean DeleteWord(String target){
//delete the target word if it exits in the dictionary and return true
//otherwise return false
return false;
}
}
Toute aide ou conseil serait apprécié.
---- ---- EDIT
Ceci est aussi un petit échantillon du fichier "dictionary.txt" (Il est trop longtemps pour mettre la chose entière dans)
ourselves
out
over
own
same
shan't
she
all
Cette est le fichier « fichier de test recherche de copy.txt »:
the
program
a
ours
house
ME
ours
main
java
whom
with
Si je ne me trompe pas, c'est un cas simple de «si le mot que vous recherchez est plus bas que celui sur lequel le curseur est placé, puis déplacez-vous vers la gauche, sinon déplacez-vous vers la droite». Faites ceci récursivement jusqu'à ce que vous trouviez le mot et retournez vrai ou vous ne trouvez pas le mot du tout et renvoyez faux. –
Essayez de faire avec un crayon et du papier, pour voir comment cela fonctionne. –