<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Polymorphie Problem in Suchfunktion (Baum)]]></title><description><![CDATA[<p>Hallo,</p>
<p>möchte/muss einen Lexikon-Baum schreiben, in welchem jeder Knoten einen Vektor mit 26 Zeigern (Buchstaben) auf Kindknoten hat. Diese Knoten sind Cells. Der Endknoten soll ein von Cell abgeleite Node sein, die den vollen Text des Wortes speichert.</p>
<p>Habe nun das Anlegen implementieren können. Beim Einbau der Suchfunktionhabe ich nun das Problem, dass entgegen der insert-Funktion immer ein Cell Knoten als letzes angezeigt wird.</p>
<p>Cell.h</p>
<pre><code>using namespace std;

class Cell {
  private:
    Cell* cellChildrenPtr[26];

  public:
    Cell() {
      for (int n=0; n&lt;26; n++) cellChildrenPtr[n] = NULL; 
    }

    // Kind setzten -&gt; Rückgabewert ist Adresse
    Cell* setChild(int i) { 
      cellChildrenPtr[i] = new Cell; 
      return cellChildrenPtr[i];
    }

    // Abfrage ob Kind gesetzt/initialisiert
    bool childIsSet(int i) { return (cellChildrenPtr[i] != NULL); }

    // Kindadresse zurückgeben
    Cell* getChild(int i) {
      return cellChildrenPtr[i];
    }

    virtual ~Cell() {
      for (int n=0; n&lt;26; n++) delete cellChildrenPtr[n];
    }

    virtual void display(){};
    virtual void setText(string s){};
    virtual bool isNode(){ return false; }; // mal so ein Versuch

};

class Node: public Cell {
  private:
    string sText;

  public:
    Node(string s) { sText = s; }

    ~Node() {}

    void display() { cout &lt;&lt; sText &lt;&lt; endl; }
    void setText(string s) { sText = s; }
    bool isNode(){ return true; };

};

class Trie {
  private:
    Cell* cellRootPtr;
    Cell* cellChildPtr;

  public:
    Trie() { cellRootPtr = new Cell; }
    ~Trie() { delete cellRootPtr; }

    void insert(string sEingabe) {
      int iLetter;

      cellChildPtr = cellRootPtr;

      // Alle Buchstaben durchlaufen
      for (unsigned int i=0; i &lt; sEingabe.length(); i++) {
        iLetter = (((int)sEingabe[i])-97);

        // wenn die Cell schon initalisiert ist dann nur cellChildPtr 
        //darauf setzten sonst init.
        if (cellChildPtr-&gt;childIsSet(iLetter)) cellChildPtr = cellChildPtr-&gt;getChild(iLetter);
        else cellChildPtr = cellChildPtr-&gt;setChild(iLetter);
      }

      Node* myNode = new Node(sEingabe);

      // letze Cell durch Node &quot;ersetzen&quot; (down_cast)
      cellChildPtr = dynamic_cast&lt;Cell*&gt;(myNode);

[b]// HIER KLAPPT DAS!!! (Text wird angezeigt)[/b]
//      cellChildPtr-&gt;display(); // klappt

    }

    void erase(string sEingabe);

    bool search(string sEingabe) {
      int iLetter;

      cellChildPtr = cellRootPtr;

      // Alle Buchstaben durchlaufen
      for (unsigned int i=0; i &lt; sEingabe.length(); i++) {
        iLetter = (((int)sEingabe[i])-97);

        // wenn die Cell initalisiert ist dann cellChildPtr darauf 
        // setzten sonst false zurück.
        if (cellChildPtr-&gt;childIsSet(iLetter)) cellChildPtr = cellChildPtr-&gt;getChild(iLetter);
        else return false;

      }

// Bin jetzt also auf dem letzten Knoten des Wortes
[b]// UND WAS JETZT???[/b]

      // Verzweifelte Versuche
      // Node* myNode = static_cast&lt;Node*&gt;(cellChildPtr);
      //	if (myNode != NULL) return true;
      // myNode-&gt;display();

//			return false;
//cout &lt;&lt; &quot;durch\n&quot;;			
//			cellChildPtr-&gt;display(); // klappt

      // Ist letzter Knoten Node?
      //  return (myNode-&gt;isNode());
  };

  void display();

};
</code></pre>
<p>Und hier noch schnell die Steuerklasse:</p>
<pre><code>#include &lt;string&gt;
#include &lt;iostream&gt;
#include &quot;Cell.h&quot;

int main(int argc, char *argv[])
{
  Trie* t = new Trie;
  t-&gt;insert(&quot;hallo&quot;);

  if (t-&gt;search(&quot;hallo&quot;)) cout &lt;&lt; &quot;hallo ist drin\n&quot;;
  else cout &lt;&lt; &quot;hallo ist nicht drin\n&quot;;

  delete t;

  return 0;
}
</code></pre>
<p>Kann mir jemand einen Tipp geben - bin bald durch mit meinem Latein/C++ <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /></p>
<p>Cheers,<br />
sunny</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/125672/polymorphie-problem-in-suchfunktion-baum</link><generator>RSS for Node</generator><lastBuildDate>Mon, 24 Aug 2026 08:33:30 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/125672.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 08 Nov 2005 15:36:57 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Polymorphie Problem in Suchfunktion (Baum) on Tue, 08 Nov 2005 15:36:57 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>möchte/muss einen Lexikon-Baum schreiben, in welchem jeder Knoten einen Vektor mit 26 Zeigern (Buchstaben) auf Kindknoten hat. Diese Knoten sind Cells. Der Endknoten soll ein von Cell abgeleite Node sein, die den vollen Text des Wortes speichert.</p>
<p>Habe nun das Anlegen implementieren können. Beim Einbau der Suchfunktionhabe ich nun das Problem, dass entgegen der insert-Funktion immer ein Cell Knoten als letzes angezeigt wird.</p>
<p>Cell.h</p>
<pre><code>using namespace std;

class Cell {
  private:
    Cell* cellChildrenPtr[26];

  public:
    Cell() {
      for (int n=0; n&lt;26; n++) cellChildrenPtr[n] = NULL; 
    }

    // Kind setzten -&gt; Rückgabewert ist Adresse
    Cell* setChild(int i) { 
      cellChildrenPtr[i] = new Cell; 
      return cellChildrenPtr[i];
    }

    // Abfrage ob Kind gesetzt/initialisiert
    bool childIsSet(int i) { return (cellChildrenPtr[i] != NULL); }

    // Kindadresse zurückgeben
    Cell* getChild(int i) {
      return cellChildrenPtr[i];
    }

    virtual ~Cell() {
      for (int n=0; n&lt;26; n++) delete cellChildrenPtr[n];
    }

    virtual void display(){};
    virtual void setText(string s){};
    virtual bool isNode(){ return false; }; // mal so ein Versuch

};

class Node: public Cell {
  private:
    string sText;

  public:
    Node(string s) { sText = s; }

    ~Node() {}

    void display() { cout &lt;&lt; sText &lt;&lt; endl; }
    void setText(string s) { sText = s; }
    bool isNode(){ return true; };

};

class Trie {
  private:
    Cell* cellRootPtr;
    Cell* cellChildPtr;

  public:
    Trie() { cellRootPtr = new Cell; }
    ~Trie() { delete cellRootPtr; }

    void insert(string sEingabe) {
      int iLetter;

      cellChildPtr = cellRootPtr;

      // Alle Buchstaben durchlaufen
      for (unsigned int i=0; i &lt; sEingabe.length(); i++) {
        iLetter = (((int)sEingabe[i])-97);

        // wenn die Cell schon initalisiert ist dann nur cellChildPtr 
        //darauf setzten sonst init.
        if (cellChildPtr-&gt;childIsSet(iLetter)) cellChildPtr = cellChildPtr-&gt;getChild(iLetter);
        else cellChildPtr = cellChildPtr-&gt;setChild(iLetter);
      }

      Node* myNode = new Node(sEingabe);

      // letze Cell durch Node &quot;ersetzen&quot; (down_cast)
      cellChildPtr = dynamic_cast&lt;Cell*&gt;(myNode);

[b]// HIER KLAPPT DAS!!! (Text wird angezeigt)[/b]
//      cellChildPtr-&gt;display(); // klappt

    }

    void erase(string sEingabe);

    bool search(string sEingabe) {
      int iLetter;

      cellChildPtr = cellRootPtr;

      // Alle Buchstaben durchlaufen
      for (unsigned int i=0; i &lt; sEingabe.length(); i++) {
        iLetter = (((int)sEingabe[i])-97);

        // wenn die Cell initalisiert ist dann cellChildPtr darauf 
        // setzten sonst false zurück.
        if (cellChildPtr-&gt;childIsSet(iLetter)) cellChildPtr = cellChildPtr-&gt;getChild(iLetter);
        else return false;

      }

// Bin jetzt also auf dem letzten Knoten des Wortes
[b]// UND WAS JETZT???[/b]

      // Verzweifelte Versuche
      // Node* myNode = static_cast&lt;Node*&gt;(cellChildPtr);
      //	if (myNode != NULL) return true;
      // myNode-&gt;display();

//			return false;
//cout &lt;&lt; &quot;durch\n&quot;;			
//			cellChildPtr-&gt;display(); // klappt

      // Ist letzter Knoten Node?
      //  return (myNode-&gt;isNode());
  };

  void display();

};
</code></pre>
<p>Und hier noch schnell die Steuerklasse:</p>
<pre><code>#include &lt;string&gt;
#include &lt;iostream&gt;
#include &quot;Cell.h&quot;

int main(int argc, char *argv[])
{
  Trie* t = new Trie;
  t-&gt;insert(&quot;hallo&quot;);

  if (t-&gt;search(&quot;hallo&quot;)) cout &lt;&lt; &quot;hallo ist drin\n&quot;;
  else cout &lt;&lt; &quot;hallo ist nicht drin\n&quot;;

  delete t;

  return 0;
}
</code></pre>
<p>Kann mir jemand einen Tipp geben - bin bald durch mit meinem Latein/C++ <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /></p>
<p>Cheers,<br />
sunny</p>
]]></description><link>https://www.c-plusplus.net/forum/post/911894</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/911894</guid><dc:creator><![CDATA[sunny1975]]></dc:creator><pubDate>Tue, 08 Nov 2005 15:36:57 GMT</pubDate></item></channel></rss>