Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- class Coppia {
- public int x;
- public String q;
- public Coppia sx;
- public Coppia dx;
- public Coppia(int x, String q) {
- this.x = x;
- this.q = q;
- dx = null;
- sx = null;
- }
- }
- public class Esercizio2 {
- public Coppia root;
- public Coppia addRecursive(Coppia current, int x, String q) {
- if (current == null) {
- return new Coppia(x,q);
- }
- if (x < current.x) {
- current.sx = addRecursive(current.sx, x, q);
- } else if (x >= current.x) {
- current.dx = addRecursive(current.dx, x, q);
- } else {
- // value already exists
- return current;
- }
- return current;
- }
- public void add(int x, String q) {
- root = addRecursive(root, x, q);
- }
- public void stampaInIntervalloELunghezza(Coppia current, int a, int b, int s){
- if(current == null){
- return;
- }
- if(current.x >= a && current.x <= b && current.q.length() <= s){
- System.out.println(current.x + " " + current.q);
- }
- if (current.x >= a ){
- stampaInIntervalloELunghezza(current.sx, a, b, s);
- }
- if (current.x <= b){
- stampaInIntervalloELunghezza(current.dx, a, b, s);
- }
- }
- public void stampaMaggioreUgualeC(Coppia current, int c){
- if(current == null){
- return;
- }
- if(current.x >= c){
- System.out.println(current.x + " " + current.q);
- }
- if (current.x >= c && current.sx != null){
- stampaMaggioreUgualeC(current.sx, c);
- }
- if (current.dx != null) {
- stampaMaggioreUgualeC(current.dx, c);
- }
- }
- public static void main(String[] args) {
- if (args.length != 5){
- System.out.println("devi mettere 5 paramentri in input");
- return;
- }
- String inputFile = args[0];
- int a = Integer.parseInt(args[1]);
- int b = Integer.parseInt(args[2]);
- int s = Integer.parseInt(args[3]);
- int c = Integer.parseInt(args[4]);
- Esercizio2 bt = new Esercizio2();
- try(BufferedReader br = new BufferedReader((new FileReader(inputFile)))){
- String line;
- while ((line = br.readLine()) != null){
- String[] parts = line.split(" ");
- int x = Integer.parseInt(parts[0]);
- String q = parts[1];
- bt.add(x, q);
- }
- }catch (IOException e){
- e.printStackTrace();
- return;
- }
- /*
- bt.add(170, "albergo");
- bt.add(130, "campionato");
- bt.add(90, "fiume");
- bt.add(20, "patate");
- bt.add(8, "frutta");
- bt.add(222, "eletto");
- bt.add(51, "sentieri");
- bt.add(130, "monti");
- bt.add(208, "valalla");
- bt.add(180, "promontorio");
- */
- System.out.println("Punto A: coppie con a <= x <= b e la lunghezza della stringa <= s");
- bt.stampaInIntervalloELunghezza(bt.root, a, b, s);
- System.out.println();
- System.out.println();
- System.out.println("Punto B: coppie con x >= c:");
- bt.stampaMaggioreUgualeC(bt.root, c);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment