tx-oliveira Posted December 16, 2023 at 08:39 PM Report #632447 Posted December 16, 2023 at 08:39 PM Olá Pessoal, Eu estou iniciar a minha programação em java, tenho de fazer um texto e numa estrutura de árvore binária tenho de ver nº de ocorrências de cada caráter e a sua frequência relativa num fila de prioridade, os heaps. E para linhas onde estão as palavras uma lista ligada. Será que alguém me pode orientar num que tenho de fazer ? Obrigado
thoga31 Posted December 17, 2023 at 01:26 AM Report #632448 Posted December 17, 2023 at 01:26 AM Podes-nos mostrar o que já tens feito até ao momento? Quais são as dúvidas específicas que gostarias de ver esclarecidas? Knowledge is free!
tx-oliveira Posted December 26, 2023 at 08:52 PM Author Report #632508 Posted December 26, 2023 at 08:52 PM Tenho de colocar este texto numa arvore binária para quantas as palavras e localizar palavras Tenho neste MAIN: public static void main(String[] args) { // TODO code application logic here String texto = "There are two ways of constructing a software design:\n" + "One way is to make it so simple that there are obviously no deficiencies, \n" + "and the other way is to make it so complicated that there are no obvious \n" + "deficiencies."; String[] palavras = texto.split("\\s+"); Node no = new Node(palavras[0]); for (int i=1; i<palavras.length; i++ ) { no.inserirPalavra(palavras[i]); System.out.println("Arvore" + palavras[i] ); } Class node: public class Node { public Node root; String palavra; Node esquerda; Node direita; //private int nivel; //int data; // Construtores public Node(String novaPalavra) { } public Node(String palavra, int nivel) { this.palavra = palavra; this.esquerda = null; this.direita = null; //this.nivel = nivel; } public Node(Node root) { this.root = root; } public Node(int key) { } public void inserirPalavra(String novaPalavra) { root = inserirRec(root, novaPalavra); } private Node inserirRec(Node root, String novaPalavra) { if (root == null) { return new Node(novaPalavra); } if (novaPalavra.compareTo(root.palavra) < 0) { root.esquerda = inserirRec(root.esquerda, novaPalavra); } else if (novaPalavra.compareTo(root.palavra) > 0) { root.direita = inserirRec(root.direita, novaPalavra); } return root; }
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now