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();
}
}