<?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[rekursiver aufruf mit pointern]]></title><description><![CDATA[<p>Hi,</p>
<p>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.<br />
Hat jemand eine Ahnung woran das liegt?<br />
Eingegeben werden Wörter, die den Regeln des Suchbaumes entsprechend links oder rechts angehängt werden. Bsp mit eingebauten Testausgaben:<br />
In: aaa<br />
---------<br />
In: bbb<br />
Out: aaa //rekursiver selbstaufruf in addnode mit testausgabe des aktuell betrachteten knoten<br />
Out: bbb // ist rechter knoten null, dann hänge bbb rechts an und prüfe nochmals rechtes kind von aaa nach dem anhängen<br />
---------<br />
In: ccc<br />
Out: aaa<br />
Out: rechts // springt in den fall sich selber neu auzurufen<br />
out: ccc</p>
<p>--&gt; gewünscht wäre:<br />
In: ccc<br />
Out: aaa<br />
Out: rechts<br />
Out: bbb<br />
Out: rechts<br />
Out: ccc</p>
<p>Grüße</p>
<p>ps: strcmp1 habe ich selber nochmal schreiben müssen, da mein Compiler kein strcmp kennt.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;string&gt;
using namespace std;

class node {
private:

	node *lchild;
	node *rchild;
	string word;

public:

	node() {
		node(&quot;&quot;);
	}

	node(string word) {
		this-&gt;word = word;
		this-&gt;lchild = 0;
		this-&gt;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 &lt; 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 &amp;root, string &amp;knot) {
		int min = knot.length();
		bool ret = false;
		if (root.length() &lt; knot.length()) {
			min = root.length();
		}
		for (int i = 0; i &lt; min; ++i) {
			if ((int) root[i] &lt; (int) knot[i]) {
				ret = true;
			}
		}
		if (!ret &amp;&amp; root.length() &lt; knot.length()) {
			ret = true;
		}
		return ret;
	}

	virtual void addnode(node *root, const node *knot) {
		cout &lt;&lt; root-&gt;word &lt;&lt; endl;
		if (!strcmp1(root-&gt;word.c_str(), knot-&gt;word.c_str())) {
			if (newbiggeractual(root-&gt;word, knot-&gt;word)) { //ok
				if (root-&gt;rchild == 0) {
					root-&gt;rchild = knot; // ok
					cout &lt;&lt; root-&gt;rchild-&gt;word &lt;&lt; endl; 
				} else {
					cout &lt;&lt; &quot;rechts&quot; &lt;&lt; endl;
					root-&gt;addnode(root-&gt;rchild, knot);
				}
			} else {
				if (root-&gt;lchild == 0) {
					cout &lt;&lt; &quot;puttet left&quot; &lt;&lt; endl;
					root-&gt;lchild = knot;
				} else {
					root-&gt;addnode(root-&gt;lchild, knot);
				}
			}
		}

	}

};

bool strcmp1(string a, string b){
		bool ret = true;
		if (a.length()!=b.length()){
			ret=false;
		} else {
			for (int i = 0; i &lt; a.length(); ++i) {
				if (a[i]!=b[i]){
					ret=false;
				}
			}
		}
		return ret;
	}

int main(void) {
	string s;
	cout &lt;&lt; &quot;enter first word :&quot; &lt;&lt; endl;
	cin &gt;&gt; s;
	node wurzel(s);
	node *tree = &amp;wurzel;
	while (!strcmp1(s.c_str(), &quot;.&quot;)) {
		cout &lt;&lt; &quot;enter a new word or cancel with &lt;.&gt;:&quot; &lt;&lt; endl;
		cin &gt;&gt; s;
		if (!strcmp1(s.c_str(), &quot;.&quot;)) {
			node x1(s);
			node *x2 = &amp;x1;
			tree-&gt;addnode(tree, x2);
		} else {
			cout &lt;&lt; &quot;ende&quot; &lt;&lt; endl;
		}
	}

	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/268338/rekursiver-aufruf-mit-pointern</link><generator>RSS for Node</generator><lastBuildDate>Tue, 01 Sep 2026 22:06:04 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/268338.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 07 Jun 2010 19:09:02 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to rekursiver aufruf mit pointern on Mon, 07 Jun 2010 19:09:02 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>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.<br />
Hat jemand eine Ahnung woran das liegt?<br />
Eingegeben werden Wörter, die den Regeln des Suchbaumes entsprechend links oder rechts angehängt werden. Bsp mit eingebauten Testausgaben:<br />
In: aaa<br />
---------<br />
In: bbb<br />
Out: aaa //rekursiver selbstaufruf in addnode mit testausgabe des aktuell betrachteten knoten<br />
Out: bbb // ist rechter knoten null, dann hänge bbb rechts an und prüfe nochmals rechtes kind von aaa nach dem anhängen<br />
---------<br />
In: ccc<br />
Out: aaa<br />
Out: rechts // springt in den fall sich selber neu auzurufen<br />
out: ccc</p>
<p>--&gt; gewünscht wäre:<br />
In: ccc<br />
Out: aaa<br />
Out: rechts<br />
Out: bbb<br />
Out: rechts<br />
Out: ccc</p>
<p>Grüße</p>
<p>ps: strcmp1 habe ich selber nochmal schreiben müssen, da mein Compiler kein strcmp kennt.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;string&gt;
using namespace std;

class node {
private:

	node *lchild;
	node *rchild;
	string word;

public:

	node() {
		node(&quot;&quot;);
	}

	node(string word) {
		this-&gt;word = word;
		this-&gt;lchild = 0;
		this-&gt;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 &lt; 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 &amp;root, string &amp;knot) {
		int min = knot.length();
		bool ret = false;
		if (root.length() &lt; knot.length()) {
			min = root.length();
		}
		for (int i = 0; i &lt; min; ++i) {
			if ((int) root[i] &lt; (int) knot[i]) {
				ret = true;
			}
		}
		if (!ret &amp;&amp; root.length() &lt; knot.length()) {
			ret = true;
		}
		return ret;
	}

	virtual void addnode(node *root, const node *knot) {
		cout &lt;&lt; root-&gt;word &lt;&lt; endl;
		if (!strcmp1(root-&gt;word.c_str(), knot-&gt;word.c_str())) {
			if (newbiggeractual(root-&gt;word, knot-&gt;word)) { //ok
				if (root-&gt;rchild == 0) {
					root-&gt;rchild = knot; // ok
					cout &lt;&lt; root-&gt;rchild-&gt;word &lt;&lt; endl; 
				} else {
					cout &lt;&lt; &quot;rechts&quot; &lt;&lt; endl;
					root-&gt;addnode(root-&gt;rchild, knot);
				}
			} else {
				if (root-&gt;lchild == 0) {
					cout &lt;&lt; &quot;puttet left&quot; &lt;&lt; endl;
					root-&gt;lchild = knot;
				} else {
					root-&gt;addnode(root-&gt;lchild, knot);
				}
			}
		}

	}

};

bool strcmp1(string a, string b){
		bool ret = true;
		if (a.length()!=b.length()){
			ret=false;
		} else {
			for (int i = 0; i &lt; a.length(); ++i) {
				if (a[i]!=b[i]){
					ret=false;
				}
			}
		}
		return ret;
	}

int main(void) {
	string s;
	cout &lt;&lt; &quot;enter first word :&quot; &lt;&lt; endl;
	cin &gt;&gt; s;
	node wurzel(s);
	node *tree = &amp;wurzel;
	while (!strcmp1(s.c_str(), &quot;.&quot;)) {
		cout &lt;&lt; &quot;enter a new word or cancel with &lt;.&gt;:&quot; &lt;&lt; endl;
		cin &gt;&gt; s;
		if (!strcmp1(s.c_str(), &quot;.&quot;)) {
			node x1(s);
			node *x2 = &amp;x1;
			tree-&gt;addnode(tree, x2);
		} else {
			cout &lt;&lt; &quot;ende&quot; &lt;&lt; endl;
		}
	}

	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1908537</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1908537</guid><dc:creator><![CDATA[kons]]></dc:creator><pubDate>Mon, 07 Jun 2010 19:09:02 GMT</pubDate></item><item><title><![CDATA[Reply to rekursiver aufruf mit pointern on Mon, 07 Jun 2010 20:59:26 GMT]]></title><description><![CDATA[<p>Schau dir mal Gültigkeitsbereiche von Variablen an <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
<pre><code class="language-cpp">if (!strcmp1(s.c_str(), &quot;.&quot;))
{
    node x1(s);
    node *x2 = &amp;x1;
    tree-&gt;addnode(tree, x2);
}
else
{
    cout &lt;&lt; &quot;ende&quot; &lt;&lt; endl;
}
</code></pre>
<p>- x1 geht beim verlassen der if flöten.<br />
- x2 zeigt somit dann ins Nirvana</p>
<p>Hier kannst du durchaus Speicher mit new anfordern. (<strong>Aber am Ende nicht vergessen, alles wieder mit delete freizugeben</strong>)</p>
<pre><code class="language-cpp">if (!strcmp1(s.c_str(), &quot;.&quot;))
{
    node *x2 = new node(s);
    tree-&gt;addnode(tree, x2);
}
else
{
    cout &lt;&lt; &quot;ende&quot; &lt;&lt; endl;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1908613</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1908613</guid><dc:creator><![CDATA[BasicMan01]]></dc:creator><pubDate>Mon, 07 Jun 2010 20:59:26 GMT</pubDate></item><item><title><![CDATA[Reply to rekursiver aufruf mit pointern on Mon, 07 Jun 2010 21:15:49 GMT]]></title><description><![CDATA[<p>ok, danke ... da hätte ich nicht gesucht <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /><br />
ist ja klar, dass dann x1 weg ist</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1908620</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1908620</guid><dc:creator><![CDATA[kons]]></dc:creator><pubDate>Mon, 07 Jun 2010 21:15:49 GMT</pubDate></item></channel></rss>