<?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[Rekursive Baumerstellung]]></title><description><![CDATA[<p>Hallo,<br />
ich habe folgende Klasse geschrieben:<br />
tree.h:</p>
<pre><code>#ifndef _TREE_H_
#define _TREE_H_
#include &lt;string&gt; 
#include &lt;vector&gt;

class node {
    private:
        std::string name;
        std::vector&lt;node*&gt; children;
        static int node_id;
    public:
        // Konstruktor
        node(const std::string&amp; new_name = &quot;&quot;); 
        // Destruktor
        virtual ~node();
        // Elementfunktionen
        std::string get_name() const;
        void set_name(const std::string&amp;);
        unsigned int get_nr_children() const;
        node* get_child(unsigned int i) const; 
        void add_child(node*);          
};
extern node* create_complete_tree(unsigned int, unsigned int);
#endif
</code></pre>
<p>tree.cxx:</p>
<pre><code>#include &quot;tree.h&quot;
#include &lt;sstream&gt;
#include &lt;iostream&gt;

int node::node_id = 0;

// Konstruktor
node::node(const std::string&amp; new_name) {
    if(new_name == &quot;&quot;) {
        node_id++;
        std::stringstream str_sm;
        str_sm &lt;&lt; node_id;  
        std::string node_id_str = &quot;node_&quot; + str_sm.str();      
        set_name(node_id_str);
    } else {
        set_name(new_name);
    }
}       
// Destruktor
node::~node() {
    std::cout &lt;&lt; &quot;enter ~node of &quot; &lt;&lt; &quot;\&quot;&quot; &lt;&lt; get_name() &lt;&lt; &quot;\&quot;&quot; &lt;&lt; std::endl;
    for(unsigned int i = 0; i &lt; children.size(); i++) {
            delete get_child(i);           
    }
    std::cout &lt;&lt; &quot;leave ~node of &quot; &lt;&lt; &quot;\&quot;&quot; &lt;&lt; get_name() &lt;&lt; &quot;\&quot;&quot; &lt;&lt; std::endl;
}
// Elementfunktionen
std::string node::get_name() const {
    return name;
}

void node::set_name(const std::string&amp; new_name) {
    name = new_name;
}

unsigned int node::get_nr_children() const {
    return children.size();
}

node* node::get_child(unsigned int i) const {
    return children.at(i);
}

void node::add_child(node* child) {
    children.push_back(child);
}

node* create_complete_tree(unsigned int nr_child_nodes,unsigned int tree_depth) {           
    /*for(unsigned int i = 0; i &lt; nr_child_nodes; i++) {
        this-&gt;add_child(new node(&quot;&quot;));
        if(tree_depth &gt; 0) {
            this-&gt;children.at(children.size()-1)-&gt;create_complete_tree(nr_child_nodes, (tree_depth-2));
        }    
    }*/ 
}
</code></pre>
<p>Nun will ich einen kompletten Baum rekursiv durch eine externe Funktion (create_complete_tree) erstellen lassen, kriege das als solche aber nicht hin. Wenn das eine Elementfunktion wäre, wäre das kein Problem (siehe auskommentierten Teil der Funktion create_complete_tree).<br />
Ein weiteres Problem ist, dass ich notwendigerweise nur die Parameter verwenden darf: nr_child_nodes und tree_depth. Ich weiß nicht wie ich damit eine rekursive Funktion erstellen kann, denn wenn ich einen Zeiger auf einen Knoten als Parameter hätte, wäre das wiederum kein Problem, das würde dann ungefähr so wie bei der Tiefensuche funktionieren (statt zu suchen, fügt man Knoten hinzu).<br />
Ich weiß einfach nicht weiter. Kann mir jemand helfen?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/302968/rekursive-baumerstellung</link><generator>RSS for Node</generator><lastBuildDate>Tue, 11 Aug 2026 00:01:41 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/302968.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 02 May 2012 10:20:18 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Rekursive Baumerstellung on Wed, 02 May 2012 10:20:18 GMT]]></title><description><![CDATA[<p>Hallo,<br />
ich habe folgende Klasse geschrieben:<br />
tree.h:</p>
<pre><code>#ifndef _TREE_H_
#define _TREE_H_
#include &lt;string&gt; 
#include &lt;vector&gt;

class node {
    private:
        std::string name;
        std::vector&lt;node*&gt; children;
        static int node_id;
    public:
        // Konstruktor
        node(const std::string&amp; new_name = &quot;&quot;); 
        // Destruktor
        virtual ~node();
        // Elementfunktionen
        std::string get_name() const;
        void set_name(const std::string&amp;);
        unsigned int get_nr_children() const;
        node* get_child(unsigned int i) const; 
        void add_child(node*);          
};
extern node* create_complete_tree(unsigned int, unsigned int);
#endif
</code></pre>
<p>tree.cxx:</p>
<pre><code>#include &quot;tree.h&quot;
#include &lt;sstream&gt;
#include &lt;iostream&gt;

int node::node_id = 0;

// Konstruktor
node::node(const std::string&amp; new_name) {
    if(new_name == &quot;&quot;) {
        node_id++;
        std::stringstream str_sm;
        str_sm &lt;&lt; node_id;  
        std::string node_id_str = &quot;node_&quot; + str_sm.str();      
        set_name(node_id_str);
    } else {
        set_name(new_name);
    }
}       
// Destruktor
node::~node() {
    std::cout &lt;&lt; &quot;enter ~node of &quot; &lt;&lt; &quot;\&quot;&quot; &lt;&lt; get_name() &lt;&lt; &quot;\&quot;&quot; &lt;&lt; std::endl;
    for(unsigned int i = 0; i &lt; children.size(); i++) {
            delete get_child(i);           
    }
    std::cout &lt;&lt; &quot;leave ~node of &quot; &lt;&lt; &quot;\&quot;&quot; &lt;&lt; get_name() &lt;&lt; &quot;\&quot;&quot; &lt;&lt; std::endl;
}
// Elementfunktionen
std::string node::get_name() const {
    return name;
}

void node::set_name(const std::string&amp; new_name) {
    name = new_name;
}

unsigned int node::get_nr_children() const {
    return children.size();
}

node* node::get_child(unsigned int i) const {
    return children.at(i);
}

void node::add_child(node* child) {
    children.push_back(child);
}

node* create_complete_tree(unsigned int nr_child_nodes,unsigned int tree_depth) {           
    /*for(unsigned int i = 0; i &lt; nr_child_nodes; i++) {
        this-&gt;add_child(new node(&quot;&quot;));
        if(tree_depth &gt; 0) {
            this-&gt;children.at(children.size()-1)-&gt;create_complete_tree(nr_child_nodes, (tree_depth-2));
        }    
    }*/ 
}
</code></pre>
<p>Nun will ich einen kompletten Baum rekursiv durch eine externe Funktion (create_complete_tree) erstellen lassen, kriege das als solche aber nicht hin. Wenn das eine Elementfunktion wäre, wäre das kein Problem (siehe auskommentierten Teil der Funktion create_complete_tree).<br />
Ein weiteres Problem ist, dass ich notwendigerweise nur die Parameter verwenden darf: nr_child_nodes und tree_depth. Ich weiß nicht wie ich damit eine rekursive Funktion erstellen kann, denn wenn ich einen Zeiger auf einen Knoten als Parameter hätte, wäre das wiederum kein Problem, das würde dann ungefähr so wie bei der Tiefensuche funktionieren (statt zu suchen, fügt man Knoten hinzu).<br />
Ich weiß einfach nicht weiter. Kann mir jemand helfen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2207496</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2207496</guid><dc:creator><![CDATA[arnas]]></dc:creator><pubDate>Wed, 02 May 2012 10:20:18 GMT</pubDate></item><item><title><![CDATA[Reply to Rekursive Baumerstellung on Wed, 02 May 2012 10:43:26 GMT]]></title><description><![CDATA[<p>Dein Include-Guard erzeugt undefiniertes Verhalten.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2207505</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2207505</guid><dc:creator><![CDATA[314159265358979]]></dc:creator><pubDate>Wed, 02 May 2012 10:43:26 GMT</pubDate></item><item><title><![CDATA[Reply to Rekursive Baumerstellung on Wed, 02 May 2012 10:51:12 GMT]]></title><description><![CDATA[<p>Bei mir kompiliert alles fehlerfrei. Kannst du das genauer beschreiben?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2207508</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2207508</guid><dc:creator><![CDATA[arnas]]></dc:creator><pubDate>Wed, 02 May 2012 10:51:12 GMT</pubDate></item><item><title><![CDATA[Reply to Rekursive Baumerstellung on Wed, 02 May 2012 10:54:46 GMT]]></title><description><![CDATA[<p>Er entspricht nicht den Regeln für erlaubte Bezeichner.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2207510</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2207510</guid><dc:creator><![CDATA[314159265358979]]></dc:creator><pubDate>Wed, 02 May 2012 10:54:46 GMT</pubDate></item><item><title><![CDATA[Reply to Rekursive Baumerstellung on Wed, 02 May 2012 10:59:14 GMT]]></title><description><![CDATA[<p>Wie wäre es dann richtig?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2207514</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2207514</guid><dc:creator><![CDATA[arnas]]></dc:creator><pubDate>Wed, 02 May 2012 10:59:14 GMT</pubDate></item><item><title><![CDATA[Reply to Rekursive Baumerstellung on Wed, 02 May 2012 10:59:52 GMT]]></title><description><![CDATA[<p>Anders.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2207516</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2207516</guid><dc:creator><![CDATA[314159265358979]]></dc:creator><pubDate>Wed, 02 May 2012 10:59:52 GMT</pubDate></item><item><title><![CDATA[Reply to Rekursive Baumerstellung on Wed, 02 May 2012 11:03:17 GMT]]></title><description><![CDATA[<p>arnas schrieb:</p>
<blockquote>
<p>Wie wäre es dann richtig?</p>
</blockquote>
<p>Pauschal fährst du ganz gut, wenn du führende Unterstriche sowie doppelte Unterstriche grundsätzlich vermeidest. Bezeichner mit doppelten Unterstrichen sind grundsätzlich der Implementierung vorbehalten, Bezeichner mit führenden Unterstrichen unter bestimmten Bedingungen auch.</p>
<p>Bevor jetzt ein Klugscheißer daher kommt, der die Regeln ganz exakt kennt: das ist restriktiver als der Standard, aber es ist leichter zu merken und tut nicht weh.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2207517</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2207517</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Wed, 02 May 2012 11:03:17 GMT</pubDate></item><item><title><![CDATA[Reply to Rekursive Baumerstellung on Wed, 02 May 2012 11:03:54 GMT]]></title><description><![CDATA[<p>Ich hasse diese Witzbolde in dem Forum.<br />
Schreibt doch immer gleich RTFM.</p>
<p>Erst passiert mir das mit SeppJ und jetz machts Pi auch net besser...</p>
<p>Für Arnas:</p>
<blockquote>
<p>(However, names starting with one or two underscores, such as _GRANDFATHER_H and __GRANDFATHER_H, are reserved to the implementation and must not be used by the user.[1][2])</p>
</blockquote>
<p>Einfach führende Unterstriche weglassen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2207519</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2207519</guid><dc:creator><![CDATA[shisha]]></dc:creator><pubDate>Wed, 02 May 2012 11:03:54 GMT</pubDate></item><item><title><![CDATA[Reply to Rekursive Baumerstellung on Wed, 02 May 2012 11:07:45 GMT]]></title><description><![CDATA[<p>pumuckl schrieb:</p>
<blockquote>
<p>arnas schrieb:</p>
<blockquote>
<p>Wie wäre es dann richtig?</p>
</blockquote>
<p>Pauschal fährst du ganz gut, wenn du führende Unterstriche sowie doppelte Unterstriche grundsätzlich vermeidest. Bezeichner mit doppelten Unterstrichen sind grundsätzlich der Implementierung vorbehalten, Bezeichner mit führenden Unterstrichen unter bestimmten Bedingungen auch.</p>
<p>Bevor jetzt ein Klugscheißer daher kommt, der die Regeln ganz exakt kennt: das ist restriktiver als der Standard, aber es ist leichter zu merken und tut nicht weh.</p>
</blockquote>
<p>Ok, danke. Werde die Include Guards von _TREE_H_ auf TREE_H ändern.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2207520</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2207520</guid><dc:creator><![CDATA[arnas]]></dc:creator><pubDate>Wed, 02 May 2012 11:07:45 GMT</pubDate></item><item><title><![CDATA[Reply to Rekursive Baumerstellung on Wed, 02 May 2012 11:08:09 GMT]]></title><description><![CDATA[<p>RTFM</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2207521</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2207521</guid><dc:creator><![CDATA[314159265358979]]></dc:creator><pubDate>Wed, 02 May 2012 11:08:09 GMT</pubDate></item><item><title><![CDATA[Reply to Rekursive Baumerstellung on Wed, 02 May 2012 11:08:51 GMT]]></title><description><![CDATA[<p>arnas schrieb:</p>
<blockquote>
<p>Ein weiteres Problem ist, dass ich notwendigerweise nur die Parameter verwenden darf: nr_child_nodes und tree_depth. Ich weiß nicht wie ich damit eine rekursive Funktion erstellen kann, denn wenn ich einen Zeiger auf einen Knoten als Parameter hätte, wäre das wiederum kein Problem, das würde dann ungefähr so wie bei der Tiefensuche funktionieren (statt zu suchen, fügt man Knoten hinzu).</p>
</blockquote>
<p>Dann mach es mit einer rekursiven Hilfsfunktion, die du von create_complete_tree aus aufrufst.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2207522</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2207522</guid><dc:creator><![CDATA[Bashar]]></dc:creator><pubDate>Wed, 02 May 2012 11:08:51 GMT</pubDate></item><item><title><![CDATA[Reply to Rekursive Baumerstellung on Wed, 02 May 2012 11:17:50 GMT]]></title><description><![CDATA[<p>Bashar schrieb:</p>
<blockquote>
<p>arnas schrieb:</p>
<blockquote>
<p>Ein weiteres Problem ist, dass ich notwendigerweise nur die Parameter verwenden darf: nr_child_nodes und tree_depth. Ich weiß nicht wie ich damit eine rekursive Funktion erstellen kann, denn wenn ich einen Zeiger auf einen Knoten als Parameter hätte, wäre das wiederum kein Problem, das würde dann ungefähr so wie bei der Tiefensuche funktionieren (statt zu suchen, fügt man Knoten hinzu).</p>
</blockquote>
<p>Dann mach es mit einer rekursiven Hilfsfunktion, die du von create_complete_tree aus aufrufst.</p>
</blockquote>
<p>Die Funktion soll den Knoten doch <em>erstellen</em>, nicht als Argument erhalten. Die Lösung ist so einfach, dass ich sie einfach posten muss:</p>
<pre><code class="language-cpp">node* create_complete_tree(unsigned int nr_child_nodes, unsigned int tree_depth)
{
	std::auto_ptr&lt;node&gt; result(new node(whatever));
	if (tree_depth)
	{
		for (unsigned i = 0; i &lt; nr_child_nodes; ++i)
		{
			std::auto_ptr&lt;node&gt; child(
				create_complete_tree(nr_child_nodes, tree_depth - 1));

			//weil add_child werfen kann
			result-&gt;add_child(child.get());
			child.release();
		}
	}
	return result.release();
}
</code></pre>
<p>Ist die Aufgabe so gemeint?<br />
Natürlich sollte die Funktion <code>unique_ptr&lt;node&gt;</code> oder so zurückgeben, aber nichts geht über gute alte Ausnahmefrickelei.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2207529</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2207529</guid><dc:creator><![CDATA[TyRoXx]]></dc:creator><pubDate>Wed, 02 May 2012 11:17:50 GMT</pubDate></item><item><title><![CDATA[Reply to Rekursive Baumerstellung on Wed, 02 May 2012 11:18:54 GMT]]></title><description><![CDATA[<p>Bashar schrieb:</p>
<blockquote>
<p>arnas schrieb:</p>
<blockquote>
<p>Ein weiteres Problem ist, dass ich notwendigerweise nur die Parameter verwenden darf: nr_child_nodes und tree_depth. Ich weiß nicht wie ich damit eine rekursive Funktion erstellen kann, denn wenn ich einen Zeiger auf einen Knoten als Parameter hätte, wäre das wiederum kein Problem, das würde dann ungefähr so wie bei der Tiefensuche funktionieren (statt zu suchen, fügt man Knoten hinzu).</p>
</blockquote>
<p>Dann mach es mit einer rekursiven Hilfsfunktion, die du von create_complete_tree aus aufrufst.</p>
</blockquote>
<p>In meiner Aufgabenstellung heißt es, dass create_complete_tree selbst rekursiv sein soll. So wie ich das verstehe, darf man eben keine solche Hilfsfunktion verwenden... Das ist ja das ganze Problem.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2207531</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2207531</guid><dc:creator><![CDATA[arnas]]></dc:creator><pubDate>Wed, 02 May 2012 11:18:54 GMT</pubDate></item><item><title><![CDATA[Reply to Rekursive Baumerstellung on Wed, 02 May 2012 22:40:19 GMT]]></title><description><![CDATA[<p>Wenn der Knoten nun mit der Funktion erstellt wurde, kann man diesen doch sicher auch mit einer Überladung des &lt;&lt;-Operators ausgeben. Ich habe das mal versucht, aber ich scheine auf fehlerhafte Speicherbereiche zuzugreifen, da ich nichts brauchbares erhalte.</p>
<p>[cpp]std::ostream&amp; operator&lt;&lt;(std::ostream&amp; s, Node* n)<br />
{<br />
return s &lt;&lt; n-&gt;get_name();<br />
}[code]</p>
<p>Ausgabe macht sowas wie: 0FA513...</p>
<p>Jemand noch eine Idee, wie man mir da helfen könnte? Suche brachte mir keine Ergebnisse.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2207791</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2207791</guid><dc:creator><![CDATA[arnas]]></dc:creator><pubDate>Wed, 02 May 2012 22:40:19 GMT</pubDate></item></channel></rss>