1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
| package Model;
import java.io.*;
public class ArbreLexicographique {
private Noeud courant;
private Noeud tete;
private char lettre;
public ArbreLexicographique() {
Noeud[] fils= new Noeud[30];
this.tete= new Noeud('\u0000', false, 1, fils);
this.courant=tete;
this.RemplirArbre();
}
private void ajouterMot(String mot){
int i, nbFils;
System.out.println(mot);
if (mot.equals("")){
courant.setMot(true);
this.courant=tete;
}
else {
lettre=mot.charAt(0);
if (possedeFils(this.courant.getFils(), this.lettre)){
System.out.println("le noeud "+courant+" possède un fils qui contient la lettre "+lettre);
i=chercherIndiceFils(this.courant.getFils(), this.lettre);
this.courant = this.courant.getFils()[i];
if (!(mot.equals(""))){
mot=mot.substring(1,mot.length());
ajouterMot(mot);
}
else {
courant.setMot(true);
this.courant=tete;
}
}
else {
System.out.println("le noeud "+courant+" ne possède pas de fils qui contient la lettre "+lettre);
nbFils = nbFils(this.courant.getFils());
Noeud[] fils;
courant.addNoeud(new Noeud(lettre, false, this.courant.getNiveau()+1, fils= new Noeud[26]));
i=chercherIndiceFils(this.courant.getFils(), lettre);
this.courant = this.courant.getFils()[i];
if (!(mot.equals(""))){
mot=mot.substring(1,mot.length());
ajouterMot(mot);
}
else {
courant.setMot(true);
this.courant=tete;
}
}
}
}
String GenererMotAleatoire() {
int random, random2, i;
this.courant=tete;
String mot2 = null;
random=nbAleatoire(7, 7);
System.out.println("la taille alétoire du mot choisi est "+random);
for (i=0; i<random; i++){
random2=nbAleatoire(0, nbFils(courant.getFils()));
System.out.println("le noeud courant possede " +nbFils(courant.getFils())+" fils" );
System.out.println("le fils choisit dans le tableau est le fils "+random2);
char[] cs = new char[random];
try {
cs[i]= courant.getFils()[i].getLettre();
}
catch (NullPointerException e)
{
System.err.println("courant = "+courant);
System.err.println("Fils = "+courant.getFils());
System.err.println("random = "+random);
System.err.println("random2 = "+random2);
System.err.println("[] = "+courant.getFils()[random2]);
/*System.err.println("lettre = "+courant.getFils()[random2].getLettre());*/
}
if (courant.getFils()[i].isMot()){
mot2= new String(cs);
}
courant= courant.getFils()[random2];
}
return mot2;
}
private int nbAleatoire(int lower, int higher){
int random = (int)(Math.random() * (higher-lower)) + lower;
return random;
}
private boolean estDansArbre (String mot){
boolean est=false;
int i;
if ("".equals(mot) && (courant.isMot()==true)) {
est=true;
}
else {
if (courant.isMot()==false){
courant.setMot(true);
est=true;
}
else{
lettre=mot.charAt(0);
mot=mot.substring(1,mot.length());
}
if (possedeFils(courant.getFils(), lettre)){
i=chercherIndiceFils(courant.getFils(), lettre);
courant = courant.getFils()[i];
estDansArbre(mot);
}
else
est=false;
}
return est;
}
private boolean possedeFils(Noeud[] fils, char c){
int i;
Boolean bool=false;
for (i=0; i<fils.length; i++){
if(fils[i]== null){
continue;
}
else if(fils[i].getLettre() == c){
bool=true;
}
}
System.out.println("la lettre "+c+" vaut "+bool+" dans le tableau "+fils);
return bool;
}
private int chercherIndiceFils(Noeud[] fils, char c){
int i, j;
j=0;
for (i=0; i<fils.length; i++){
if(fils[i]== null){
continue;
}
else if(fils[i].getLettre() == c){
j=i;
}
}
return j;
}
private int nbFils (Noeud[] fils){
int i, cpt;
cpt=0;
for (i=0; i<fils.length; i++){
if (fils[i] != null ){
cpt++;
}
}
return cpt;
}
private void RemplirArbre(){
String fichier ="C:/Documents and Settings/Propriétaire.VIN-1D00C6226C7/Bureau/eclipse-java-indigo-SR1-win32/Pendu/src/Model/dico.txt";
try{
InputStream ips=new FileInputStream(fichier);
InputStreamReader ipsr=new InputStreamReader(ips);
BufferedReader br=new BufferedReader(ipsr);
String ligne;
int i=0;
while ((ligne=br.readLine())!=null){
this.ajouterMot(ligne);
i++;
System.out.println("ajout du mot numero "+i);
}
br.close();
}
catch (Exception e){
e.printStackTrace();
}
}
public Noeud getCourant() {
return courant;
}
public void setCourant(Noeud courant) {
this.courant = courant;
}
public Noeud getTete() {
return tete;
}
public void setTete(Noeud tete) {
this.tete = tete;
}
public char getLettre() {
return lettre;
}
public void setLettre(char lettre) {
this.lettre = lettre;
}
}
</code> |
Partager