Merge branch 'master' of https://github.com/icei-pucminas/aeds2
This commit is contained in:
commit
66791cf39b
Binary file not shown.
Binary file not shown.
|
|
@ -5,6 +5,25 @@ class ArvoreTrie {
|
||||||
raiz = new No();
|
raiz = new No();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public boolean pesquisar(String s) throws Exception {
|
||||||
|
return pesquisar(s, raiz, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean pesquisar(String s, No no, int i) throws Exception {
|
||||||
|
boolean resp;
|
||||||
|
if(no.prox[s.charAt(i)] == null){
|
||||||
|
resp = false;
|
||||||
|
} else if(i == s.length() - 1){
|
||||||
|
resp = (no.prox[s.charAt(i)].folha == true);
|
||||||
|
} else if(i < s.length() - 1 ){
|
||||||
|
resp = pesquisar(s, no.prox[s.charAt(i)], i + 1);
|
||||||
|
} else {
|
||||||
|
throw new Exception("Erro ao pesquisar!");
|
||||||
|
}
|
||||||
|
return resp;
|
||||||
|
}
|
||||||
|
|
||||||
public void inserir(String s) throws Exception {
|
public void inserir(String s) throws Exception {
|
||||||
inserir(s, raiz, 0);
|
inserir(s, raiz, 0);
|
||||||
}
|
}
|
||||||
|
|
@ -30,26 +49,6 @@ class ArvoreTrie {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public boolean pesquisar(String s) throws Exception {
|
|
||||||
return pesquisar(s, raiz, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean pesquisar(String s, No no, int i) throws Exception {
|
|
||||||
boolean resp;
|
|
||||||
if(no.prox[s.charAt(i)] == null){
|
|
||||||
resp = false;
|
|
||||||
} else if(i == s.length() - 1){
|
|
||||||
resp = (no.prox[s.charAt(i)].folha == true);
|
|
||||||
} else if(i < s.length() - 1 ){
|
|
||||||
resp = pesquisar(s, no.prox[s.charAt(i)], i + 1);
|
|
||||||
} else {
|
|
||||||
throw new Exception("Erro ao pesquisar!");
|
|
||||||
}
|
|
||||||
return resp;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public void mostrar(){
|
public void mostrar(){
|
||||||
mostrar("", raiz);
|
mostrar("", raiz);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
class No {
|
class No {
|
||||||
public char elemento;
|
public char elemento;
|
||||||
public int final tamanho = 255;
|
public final int tamanho = 255;
|
||||||
public No[] prox;
|
public No[] prox;
|
||||||
public boolean folha;
|
public boolean folha;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,104 +0,0 @@
|
||||||
public class Trie {
|
|
||||||
|
|
||||||
// Alphabet size (# of symbols)
|
|
||||||
static final int ALPHABET_SIZE = 26;
|
|
||||||
|
|
||||||
// trie node
|
|
||||||
static class TrieNode
|
|
||||||
{
|
|
||||||
TrieNode[] children = new TrieNode[ALPHABET_SIZE];
|
|
||||||
|
|
||||||
// isEndOfWord is true if the node represents
|
|
||||||
// end of a word
|
|
||||||
boolean isEndOfWord;
|
|
||||||
|
|
||||||
TrieNode(){
|
|
||||||
isEndOfWord = false;
|
|
||||||
for (int i = 0; i < ALPHABET_SIZE; i++)
|
|
||||||
children[i] = null;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
static TrieNode root;
|
|
||||||
|
|
||||||
// If not present, inserts key into trie
|
|
||||||
// If the key is prefix of trie node,
|
|
||||||
// just marks leaf node
|
|
||||||
static void insert(String key)
|
|
||||||
{
|
|
||||||
int level;
|
|
||||||
int length = key.length();
|
|
||||||
int index;
|
|
||||||
|
|
||||||
TrieNode pCrawl = root;
|
|
||||||
|
|
||||||
for (level = 0; level < length; level++)
|
|
||||||
{
|
|
||||||
index = key.charAt(level) - 'a';
|
|
||||||
if (pCrawl.children[index] == null)
|
|
||||||
pCrawl.children[index] = new TrieNode();
|
|
||||||
|
|
||||||
pCrawl = pCrawl.children[index];
|
|
||||||
}
|
|
||||||
|
|
||||||
// mark last node as leaf
|
|
||||||
pCrawl.isEndOfWord = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Returns true if key presents in trie, else false
|
|
||||||
static boolean search(String key)
|
|
||||||
{
|
|
||||||
int level;
|
|
||||||
int length = key.length();
|
|
||||||
int index;
|
|
||||||
TrieNode pCrawl = root;
|
|
||||||
|
|
||||||
for (level = 0; level < length; level++)
|
|
||||||
{
|
|
||||||
index = key.charAt(level) - 'a';
|
|
||||||
|
|
||||||
if (pCrawl.children[index] == null)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
pCrawl = pCrawl.children[index];
|
|
||||||
}
|
|
||||||
|
|
||||||
return (pCrawl != null && pCrawl.isEndOfWord);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Driver
|
|
||||||
public static void main(String args[])
|
|
||||||
{
|
|
||||||
// Input keys (use only 'a' through 'z' and lower case)
|
|
||||||
String keys[] = {"the", "a", "there", "answer", "any",
|
|
||||||
"by", "bye", "their"};
|
|
||||||
|
|
||||||
String output[] = {"Not present in trie", "Present in trie"};
|
|
||||||
|
|
||||||
|
|
||||||
root = new TrieNode();
|
|
||||||
|
|
||||||
// Construct trie
|
|
||||||
int i;
|
|
||||||
for (i = 0; i < keys.length ; i++)
|
|
||||||
insert(keys[i]);
|
|
||||||
|
|
||||||
// Search for different keys
|
|
||||||
if(search("the") == true)
|
|
||||||
System.out.println("the --- " + output[1]);
|
|
||||||
else System.out.println("the --- " + output[0]);
|
|
||||||
|
|
||||||
if(search("these") == true)
|
|
||||||
System.out.println("these --- " + output[1]);
|
|
||||||
else System.out.println("these --- " + output[0]);
|
|
||||||
|
|
||||||
if(search("their") == true)
|
|
||||||
System.out.println("their --- " + output[1]);
|
|
||||||
else System.out.println("their --- " + output[0]);
|
|
||||||
|
|
||||||
if(search("thaw") == true)
|
|
||||||
System.out.println("thaw --- " + output[1]);
|
|
||||||
else System.out.println("thaw --- " + output[0]);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -18,20 +18,16 @@ class Principal {
|
||||||
arv.mostrar();
|
arv.mostrar();
|
||||||
for(int i = 0; i < array.length; i++){
|
for(int i = 0; i < array.length; i++){
|
||||||
System.out.println("Pesquisar(" + array[i] + "):" + arv.pesquisar(array[i]));
|
System.out.println("Pesquisar(" + array[i] + "):" + arv.pesquisar(array[i]));
|
||||||
System.out.println("PesquisarI(" + array[i] + "):" + arv.pesquisarI(array[i]));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String s = "ABACA";
|
String s = "ABACA";
|
||||||
System.out.println("Pesquisar(" + s + "):" + arv.pesquisar(s));
|
System.out.println("Pesquisar(" + s + "):" + arv.pesquisar(s));
|
||||||
System.out.println("PesquisarI(" + s + "):" + arv.pesquisarI(s));
|
|
||||||
|
|
||||||
s = "ABACAXIS";
|
s = "ABACAXIS";
|
||||||
System.out.println("Pesquisar(" + s + "):" + arv.pesquisar(s));
|
System.out.println("Pesquisar(" + s + "):" + arv.pesquisar(s));
|
||||||
System.out.println("PesquisarI(" + s + "):" + arv.pesquisarI(s));
|
|
||||||
|
|
||||||
s = "gaga";
|
s = "gaga";
|
||||||
System.out.println("Pesquisar(" + s + "):" + arv.pesquisar(s));
|
System.out.println("Pesquisar(" + s + "):" + arv.pesquisar(s));
|
||||||
System.out.println("PesquisarI(" + s + "):" + arv.pesquisarI(s));
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue