Przepisanie kodu z Java na c++

0

Witam,
Jestem początkującym programistą i mam do wykonania drzewo, które zwraca ścieżkę liter od najstarszej leksykograficznie począwszy.
Kod działa, i robi to co trzeba jednak wykonuje się za wolno.
Potrzebuję przerobić go na C++(w którym jestem kompletnie zielony), chyba że ktoś wie w jaki sposób można go zoptymalizować.
Poniżej mój kod w Javie , z góry dzięki za pomoc :):

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

class Node {

	private char label;
	private Node left;
	private Node right;

	public char getLabel() {
		return label;
	}

	public void setLabel(char label) {
		this.label = label;
	}

	public Node getLeft() {
		return left;
	}

	public void setLeft(Node left) {
		this.left = left;
	}

	public Node getRight() {
		return right;
	}

	public void setRight(Node right) {
		this.right = right;
	}

	public void put(String path, char label ) {
		Node actualPosition = this;
		for ( char way : path.toCharArray() ) {
			if ( way == 'R' ) {
				goRight( actualPosition );
				actualPosition = actualPosition.getRight();
			}
			else if ( way == 'L' ) {
				goLeft( actualPosition );
				actualPosition = actualPosition.getLeft();
			}
		}
		actualPosition.setLabel( label );
	}
	
	private void goLeft( Node node )  {
		if ( node.getLeft() == null ) {
			node.setLeft( new Node() );
		}
	}
	
	private void goRight( Node node ) {
		if ( node.getRight() == null ) {
			node.setRight( new Node() );
		}
	}

	public void put( char label ) {
		this.label = label;
	}
	
	public boolean hasLeft() {
		return this.left != null;
	}
	
	public boolean hasRight() {
		return this.right != null;
	}
	
	public boolean isLeaf() {
		return !hasRight() && !hasLeft();
	}
 
	@Override
	public String toString() {
		String out = String.valueOf( this.label );
		if ( getLeft() != null  ) {
			out += getLeft().toString();
		}
		if ( getRight() != null ) {
			out += getRight().toString();
		}		
		return out;
	}
}


class WordSeeker {

	private static final int LEXICOGRAPHIC_LESS = 0;
	
	private Node root;
	private String bestWord = null;


	public WordSeeker( Node node ) {
		this.root = node;
	}
	
	public void goThroughTree() {
		goThroughTree( "", root );
	}
	
	public void goThroughTree( String prefix, Node node ) {
		String pref = node.getLabel() + prefix;
		if ( node.isLeaf() ) {
			if ( bestWord == null || isLexicographicLess( pref ) ) {
				bestWord = pref;
			}
		}
		if ( node.hasLeft() ) 
			goThroughTree( pref, node.getLeft() );
		
		if ( node.hasRight() ) 
			goThroughTree( pref, node.getRight() );
	}
	
	public String getBestWord() {
		return bestWord;
	}
	
	private boolean isLexicographicLess( String word ) {
		return bestWord.compareTo( word ) < LEXICOGRAPHIC_LESS;
	}

}

class NodeFactory {
	
	private static final Map< Character, Node > NODES = new HashMap<Character, Node>();
	
	public static Node getNode( char label ) {
		Node nullable =  NODES.get( label );
		if ( nullable == null ) {
			nullable = new Node();
			nullable.setLabel( label );
			NODES.put( label , nullable );
		}
		return nullable;
	}
	
}

public class Main {

	public static boolean inputIsRoot( String[] parts ) {
		return parts.length == 1; 
	}
	
	public static void main( String[] args ) throws IOException {
	
		
		Node root = new Node();
		BufferedReader reader = new BufferedReader( new InputStreamReader( System.in ) );
		for ( String line; reader.ready(); ) {
			line = reader.readLine();
			String[] parts = line.split("\\s+");
			char label = line.charAt( 0 );
			if ( inputIsRoot( parts ) ) {
				root.put( label );
			}
			else {
				root.put( parts[ 1 ], label );
			}
		}
		WordSeeker seeker = new WordSeeker( root );
		seeker.goThroughTree();
		
		System.out.print( seeker.getBestWord() );
		reader.close();
	}

}
0

No i z czym masz problem?

0

Tak jak napisałem, czy widzisz tutaj jakieś opcję do zoptymalizowania tego programu ? a jeżeli nie, to jakieś rady jak przepisać to na C++?

0

Użyj C++'sowych kontenerów (unordered_map zamiast HashMap), opakuj rozgałęzienia w unique_ptr, dopasuj input i z bani.

1

Może gdyby opisałeś co to generalnie robi to pojawiłoby się więcej chętnych pomóc.
Bo nie bardzo się chce przegryzać się przez ten bajzel.

0

Na dobry początek podziel program na 4 części i zmierz ich czasy wykonania:

  • ładowanie linii z wejścia
  • budowanie drzewa
  • przejście przez drzewo
  • znalezienie najlepszego slowa
0

Coś mi się chyba kojarzy. Czy to przypadkiem nie kilka ścieżek składających się z liter L i R przy czym zawsze tej samej długości?
Czy przypadkiem długość nie jest mniejsza od 64 znaków?
Jeżeli tak i tak to czytaj znak po znaku i zamieniaj te ścieżki na liczby - L=bit-0; R=bit-1;

1 użytkowników online, w tym zalogowanych: 0, gości: 1