rekursiver aufruf mit pointern
-
Hi,
ich sitze schon ne ganze Weile an folgenden Problem und komme nicht weiter. Ich verscuhe einen binären Suchbaum mit class zu erzeugen, aber scheitere am rekursiven aufruf in der addnode-Methode.
Hat jemand eine Ahnung woran das liegt?
Eingegeben werden Wörter, die den Regeln des Suchbaumes entsprechend links oder rechts angehängt werden. Bsp mit eingebauten Testausgaben:
In: aaa
---------
In: bbb
Out: aaa //rekursiver selbstaufruf in addnode mit testausgabe des aktuell betrachteten knoten
Out: bbb // ist rechter knoten null, dann hänge bbb rechts an und prüfe nochmals rechtes kind von aaa nach dem anhängen
---------
In: ccc
Out: aaa
Out: rechts // springt in den fall sich selber neu auzurufen
out: ccc--> gewünscht wäre:
In: ccc
Out: aaa
Out: rechts
Out: bbb
Out: rechts
Out: cccGrüße
ps: strcmp1 habe ich selber nochmal schreiben müssen, da mein Compiler kein strcmp kennt.
#include <iostream> #include <string> using namespace std; class node { private: node *lchild; node *rchild; string word; public: node() { node(""); } node(string word) { this->word = word; this->lchild = 0; this->rchild = 0; } /* vergleicht 2 strings */ bool strcmp1(string a, string b){ bool ret = true; if (a.length()!=b.length()){ ret=false; } else { for (int i = 0; i < a.length(); ++i) { if (a[i]!=b[i]){ ret=false; } } } return ret; } /* vergleicht 2 wörter nach dem alphabet um neues wort rechts oder links an den suchbaum anzufügen */ virtual bool newbiggeractual(string &root, string &knot) { int min = knot.length(); bool ret = false; if (root.length() < knot.length()) { min = root.length(); } for (int i = 0; i < min; ++i) { if ((int) root[i] < (int) knot[i]) { ret = true; } } if (!ret && root.length() < knot.length()) { ret = true; } return ret; } virtual void addnode(node *root, const node *knot) { cout << root->word << endl; if (!strcmp1(root->word.c_str(), knot->word.c_str())) { if (newbiggeractual(root->word, knot->word)) { //ok if (root->rchild == 0) { root->rchild = knot; // ok cout << root->rchild->word << endl; } else { cout << "rechts" << endl; root->addnode(root->rchild, knot); } } else { if (root->lchild == 0) { cout << "puttet left" << endl; root->lchild = knot; } else { root->addnode(root->lchild, knot); } } } } }; bool strcmp1(string a, string b){ bool ret = true; if (a.length()!=b.length()){ ret=false; } else { for (int i = 0; i < a.length(); ++i) { if (a[i]!=b[i]){ ret=false; } } } return ret; } int main(void) { string s; cout << "enter first word :" << endl; cin >> s; node wurzel(s); node *tree = &wurzel; while (!strcmp1(s.c_str(), ".")) { cout << "enter a new word or cancel with <.>:" << endl; cin >> s; if (!strcmp1(s.c_str(), ".")) { node x1(s); node *x2 = &x1; tree->addnode(tree, x2); } else { cout << "ende" << endl; } } return 0; }
-
Schau dir mal Gültigkeitsbereiche von Variablen an

if (!strcmp1(s.c_str(), ".")) { node x1(s); node *x2 = &x1; tree->addnode(tree, x2); } else { cout << "ende" << endl; }- x1 geht beim verlassen der if flöten.
- x2 zeigt somit dann ins NirvanaHier kannst du durchaus Speicher mit new anfordern. (Aber am Ende nicht vergessen, alles wieder mit delete freizugeben)
if (!strcmp1(s.c_str(), ".")) { node *x2 = new node(s); tree->addnode(tree, x2); } else { cout << "ende" << endl; }
-
ok, danke ... da hätte ich nicht gesucht

ist ja klar, dass dann x1 weg ist