diff --git a/aula/u10 Árvores TRIE/unidade10a_trie.pdf b/aula/u10 Árvores TRIE/unidade10a_trie.pdf index 221eb02..7192324 100644 Binary files a/aula/u10 Árvores TRIE/unidade10a_trie.pdf and b/aula/u10 Árvores TRIE/unidade10a_trie.pdf differ diff --git a/aula/u10 Árvores TRIE/unidade10b_patricia.pdf b/aula/u10 Árvores TRIE/unidade10b_patricia.pdf index a3ecc8c..2a7a5d5 100644 Binary files a/aula/u10 Árvores TRIE/unidade10b_patricia.pdf and b/aula/u10 Árvores TRIE/unidade10b_patricia.pdf differ diff --git a/fonte/u10 Árvores TRIE/java/trie/ArvoreTrie.java b/fonte/u10 Árvores TRIE/java/trie/ArvoreTrie.java index db8b862..e5708fa 100644 --- a/fonte/u10 Árvores TRIE/java/trie/ArvoreTrie.java +++ b/fonte/u10 Árvores TRIE/java/trie/ArvoreTrie.java @@ -5,6 +5,25 @@ class ArvoreTrie { 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 { 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(){ mostrar("", raiz); } diff --git a/fonte/u10 Árvores TRIE/java/trie/No.java b/fonte/u10 Árvores TRIE/java/trie/No.java index 97cdc5e..ca8dc81 100644 --- a/fonte/u10 Árvores TRIE/java/trie/No.java +++ b/fonte/u10 Árvores TRIE/java/trie/No.java @@ -1,6 +1,6 @@ class No { public char elemento; - public int final tamanho = 255; + public final int tamanho = 255; public No[] prox; public boolean folha; diff --git a/fonte/u10 Árvores TRIE/java/trie/Patricia.java b/fonte/u10 Árvores TRIE/java/trie/Patricia.java deleted file mode 100644 index 67fb2ac..0000000 --- a/fonte/u10 Árvores TRIE/java/trie/Patricia.java +++ /dev/null @@ -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]); - - } -} \ No newline at end of file diff --git a/fonte/u10 Árvores TRIE/java/trie/Principal.java b/fonte/u10 Árvores TRIE/java/trie/Principal.java index 3d7b007..584da5f 100644 --- a/fonte/u10 Árvores TRIE/java/trie/Principal.java +++ b/fonte/u10 Árvores TRIE/java/trie/Principal.java @@ -18,20 +18,16 @@ class Principal { arv.mostrar(); for(int i = 0; i < array.length; i++){ System.out.println("Pesquisar(" + array[i] + "):" + arv.pesquisar(array[i])); - System.out.println("PesquisarI(" + array[i] + "):" + arv.pesquisarI(array[i])); } String s = "ABACA"; System.out.println("Pesquisar(" + s + "):" + arv.pesquisar(s)); - System.out.println("PesquisarI(" + s + "):" + arv.pesquisarI(s)); s = "ABACAXIS"; System.out.println("Pesquisar(" + s + "):" + arv.pesquisar(s)); - System.out.println("PesquisarI(" + s + "):" + arv.pesquisarI(s)); s = "gaga"; System.out.println("Pesquisar(" + s + "):" + arv.pesquisar(s)); - System.out.println("PesquisarI(" + s + "):" + arv.pesquisarI(s)); } }