<?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[Baum aus Inorder und Postorder rekonstruieren]]></title><description><![CDATA[<p>Hi Leute!</p>
<p>Ich hab hier einen Algo. entwickelt der mir durch Übergabe eines Baums in Inorder- und Postorder-Darstellung gespeichert in einem Baum, den ursprünglichen Baum wieder rekonstruiert. Hier ist meine Implementierung:</p>
<pre><code>tree_element1* searchtree1::traversierungPreIn(int preorder[], int fPre, int lPre, int inorder[], int fIn, int lIn, int start)
{
	//int rootVal;
	int p = 0;	//Position der Wurzel

	if(lPre &lt; fPre)
	{
		return NULL;
	}
	else
	{
		tree_element1* node = new tree_element1;

		node-&gt;val = preorder[fPre];
		p = positionRoot(inorder, node-&gt;val, fIn);
		//preorder = Array mit Preorder-Anordnung
		//fPre+1 = first-Index im Preorder-Array einen Index weiter
		//fPre+p = first-Index im Preorder-Array abhängig von Position der Wurzel (wird so zum last-Index)
		//inorder = Array mit Inorder-Anordnung
		//fIn = first-Index im Inorder-Array
		//fIn+p+1 = first-Index im Inorder-Array in Abhängigkeit von Position der Wurzel und einen Schritt weiter
		start = fIn+p-1 - fPre+p;
		node-&gt;left = traversierungPreIn(preorder, fPre+1, fPre+p, inorder, fIn, fIn+p-1, start);

		node-&gt;right = traversierungPreIn(preorder, fPre+p+1, lPre, inorder, fIn+p+1, lIn, start);

		return node;
	}
}
</code></pre>
<p>Das Problem an meiner Implementierung ist nun: Sie funktioniert schon fast. Ich hab nun den Tip bekommen, dass in den rekursiven Aufrufen so eine Art Schleifeninvariante mitziehen muss, die mir die Unterschiede zwischen den Arrays berechnet. Ich komme aber nicht drauf, wie ich die Variable start berechnen muss bzw. an welcher Stelle ich in den rekursiven Aufrufen die start-Variable mit einbeziehen muss...</p>
<p>Kann mir jemand helfen?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/316507/baum-aus-inorder-und-postorder-rekonstruieren</link><generator>RSS for Node</generator><lastBuildDate>Wed, 29 Jul 2026 22:57:49 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/316507.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 06 May 2013 14:33:45 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Baum aus Inorder und Postorder rekonstruieren on Mon, 06 May 2013 14:33:45 GMT]]></title><description><![CDATA[<p>Hi Leute!</p>
<p>Ich hab hier einen Algo. entwickelt der mir durch Übergabe eines Baums in Inorder- und Postorder-Darstellung gespeichert in einem Baum, den ursprünglichen Baum wieder rekonstruiert. Hier ist meine Implementierung:</p>
<pre><code>tree_element1* searchtree1::traversierungPreIn(int preorder[], int fPre, int lPre, int inorder[], int fIn, int lIn, int start)
{
	//int rootVal;
	int p = 0;	//Position der Wurzel

	if(lPre &lt; fPre)
	{
		return NULL;
	}
	else
	{
		tree_element1* node = new tree_element1;

		node-&gt;val = preorder[fPre];
		p = positionRoot(inorder, node-&gt;val, fIn);
		//preorder = Array mit Preorder-Anordnung
		//fPre+1 = first-Index im Preorder-Array einen Index weiter
		//fPre+p = first-Index im Preorder-Array abhängig von Position der Wurzel (wird so zum last-Index)
		//inorder = Array mit Inorder-Anordnung
		//fIn = first-Index im Inorder-Array
		//fIn+p+1 = first-Index im Inorder-Array in Abhängigkeit von Position der Wurzel und einen Schritt weiter
		start = fIn+p-1 - fPre+p;
		node-&gt;left = traversierungPreIn(preorder, fPre+1, fPre+p, inorder, fIn, fIn+p-1, start);

		node-&gt;right = traversierungPreIn(preorder, fPre+p+1, lPre, inorder, fIn+p+1, lIn, start);

		return node;
	}
}
</code></pre>
<p>Das Problem an meiner Implementierung ist nun: Sie funktioniert schon fast. Ich hab nun den Tip bekommen, dass in den rekursiven Aufrufen so eine Art Schleifeninvariante mitziehen muss, die mir die Unterschiede zwischen den Arrays berechnet. Ich komme aber nicht drauf, wie ich die Variable start berechnen muss bzw. an welcher Stelle ich in den rekursiven Aufrufen die start-Variable mit einbeziehen muss...</p>
<p>Kann mir jemand helfen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2321390</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2321390</guid><dc:creator><![CDATA[vip*r]]></dc:creator><pubDate>Mon, 06 May 2013 14:33:45 GMT</pubDate></item></channel></rss>