Digitaler Suchbaum

Status
Nicht offen für weitere Antworten.

darkmoon221

Mitglied
Hallo zusammen;

hab die Aufgabe eine Stringsuche in Form von Tries zu realisieren.
Man hat nen Suchschlüßel der Zeichenweise betrachtet wird. Beispielsweise möchte ich den Namen TEST in den Baum einfügen, soll der Algorithmus pro Buchstaben einen Knoten erzeugen. Wird dann zb auch noch TESA eingetragen, wird verglichen welche Buchstaben schon da sind.Das A wir zum 2ten blatt von S.

Habe schon bissl angefangen, aber irgendwo hab ich momentan ne denkblockade und würde mich für nen Denkanstoß freuen.


Also das Programm soll aus 3 Klassen bestehen. Die erste ist die Shell aber die is kein Problem die hab ich schon.
Die 2te Klasse repräsentiert einen Knoten im baum. und zum schluß die klasse trie.





Code:
public class Node {

	Integer points; // Nimmt den Punktewert auf

	Node[] childs; // Verweise auf Kinder

	Node parent; // Verweis auf Vater

	char ch;

	public Node() { 
		parent = null;  // Erzeugt Wurzelknoten
		childs = new Node[26]; //Wurzelknoten kann 26 Verzeweigungen haben
	}

	public Node(char ch, Node parent) { //Erzeugt einen Knoten für das Zeichen ch und setzt Knoten parent

		this.ch = ch;
		this.parent = parent;
		childs = new Node[26];

	}

/** Setzt den Verweis des Kindknotens für das Zeichen chauf den Knoten child
**/
	private void setChild(char ch, Node child) { 
		if (parent == null) {
		
			
			

		}
                public Node get child(char ch){




               }


}

Mein Problem liegt darin dass ich nicht weiß wie ich die setChild beginnen soll. es kommen noch weitere methoden aber wenn ich mal nen anstoß habe dann werd ich die schon hinbekommen.

[/code]
 
A

Andreas Lochbihler

Gast
Hallo!

Einfach dranbleiben, sin doch noch 2 wochen!
Gruß bis nächste woche!
 
G

Guest

Gast
lol
ich hoffe, du bist noch selber darauf gekommen :D
glaubst du nicht, dass die verantwortlichen sich auch hier umschauen werden?
tolle aktion :D
 
P

Praktomat

Gast
Den online gestellten Code würde ich nicht so beim Praktomat einreichen und bewerten lassen. Hat ja jetzt einen gewissen Wiedererkennungswert :D

Viel Spaß beim weiterprogrammieren ;)
hacker.jpg
 
B

brightlight332

Gast
Folgender Code soll ganz cool sein, hab ich gehört :cool:
Aber *pssst*, nicht abschreiben, nur inspirieren lassen :wink:
Code:
public class Node {
    private static final int FIRSTCHAR = 'a';
    private static final int NUMBERCHARS = 26;
    private Integer points;
    private Node[] childs = new Node[NUMBERCHARS];
    private Node parent;
    private char ch;

    public Node() { this('#', null); }
    public Node(char ch, Node parent) {
        this.ch = ch;
        this.parent = parent;
        if (parent != null) parent.setChild(ch, this);
    }
    public Node getChild(char ch) {
        return isValidIndex(ch) ? childs[ch - FIRSTCHAR] : null;
    }
    public Node find(String key) {
        if (key.length() < 1) return null;
        char curKey = key.charAt(0);
        if (isValidIndex(curKey) && childs[curKey - FIRSTCHAR] != null) {
            if (key.length() == 1) return childs[curKey - FIRSTCHAR];
            else return childs[curKey - FIRSTCHAR].find(key.substring(1));
        } else return null;
    }
    public String toString() {
        String output = "";
        for (Node child : childs) 
            if (child != null) output += child.toString();
        if (output.length() > 0) output = "(" + output + ")";
        if (points != null) output = "[" + points.toString() + "]" + output;
        return ch + output;
    }
    public void deleteMe() { points = null; cleanup(); }
    public void setPoints(Integer points) { this.points = points; }
    public Integer getPoints() { return points; }
    private boolean hasSubTreePoints() {
	if(this.points!=null) return true;
        for (Node child : childs) {
            if (child != null) {
        	if(child.getPoints()!=null) return true;
        	return child.hasSubTreePoints();
            }
        }
        return false;
    }
    private void cleanup() {
        if (!hasSubTreePoints() && parent != null) {
            parent.setChild(ch, null);
            parent.cleanup();
        }
    }
    private boolean isValidIndex(char ch) {
        return !(ch < FIRSTCHAR || ch - FIRSTCHAR >= childs.length);
    }
    private void setChild(char ch, Node child) {
        if (isValidIndex(ch)) childs[ch - FIRSTCHAR] = child;
    }
}
 
G

Gast

Gast
Ja mei echt interessant wie leicht es is hier ICler wiederzutreffen :-D

Aber ich hab ja die Enigma scho- hat 4h gedauert- läuft wunderbar :-D

Scheeeeeeeerz :p

Insider :p

Wie siehts aus Herr Darkmoon- hats jetz hingehauen?

MfG
ein mitleidender Student ausm "Woid"
 
Status
Nicht offen für weitere Antworten.

Neue Themen


Oben