<?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 Problem]]></title><description><![CDATA[<p>Hi,<br />
ich hab ein Problem mit Bäumen -arbeite mich da gerade erst ein- und zwar möchte ich in einen Baum etwas einfügen und zwar möchte ich nichts nach groß/klein sortieren sondern einfach der Reihe nach immer rein: Links frei -&gt; Nach Links ansonsten Rechts...</p>
<p>Hier mein Code:</p>
<p>CPP:</p>
<pre><code class="language-cpp">#include &quot;SymTable.h&quot;

SymTableNode::SymTableNode( const SymTableValue &amp;Val ) : Left( NULL ), Right( NULL ), Value ( Val )
{
}

SymTableNode::~SymTableNode( void )
{
	if ( Left ) { printf( &quot;L: %s\n&quot;, Left-&gt;Value.Str); delete Left; }
	if ( Right ) { printf( &quot;R: %s\n&quot;, Right-&gt;Value.Str); delete Right; }
}

void SymTableNode::Insert( const SymTableValue &amp;Val )
{
	if ( ( Left == NULL ) || ( Right == NULL ) )
	{
		if ( Left == NULL )
		{
			if ( Left != NULL )
			{
				Left-&gt;Insert( Val );
				return;
			}

			Left = new SymTableNode( Val );
			return;
		}
		else if ( Right == NULL )
		{
			if ( Right != NULL )
			{
				Right-&gt;Insert( Val );
				return;
			}

			Right = new SymTableNode( Val );
			return;
		}
		else
		{
			printf( &quot;%i: Und nun!?\n&quot;, __LINE__ );
		}
	}
	else
	{
		printf( &quot;%i: Und nun!?\n&quot;, __LINE__ );
	}
}
</code></pre>
<p>Header:</p>
<pre><code class="language-cpp">#ifndef SYM_TABLE_H
#define SYM_TABLE_H

#include &lt;stdio.h&gt;

struct SymTableValue
{
	union 
	{ 
		char *Str;
	};

	SymTableValue()
	{
		Str = NULL;
	}
};

class SymTableNode
{
	private:
		SymTableValue Value;
		SymTableNode *Left;
		SymTableNode *Right;
	public:
		SymTableNode( const SymTableValue &amp;Val );
		virtual ~SymTableNode( void );
		void Insert( const SymTableValue &amp;Val );
		SymTableNode *GetLeft( void ) { return Left; } 
		SymTableNode *GetRight( void ) { return Right; } 
};

class SymTable
{
	private:
		SymTableNode Node;
	public:
		void Insert( void );
};

#endif /* SYM_TABLE_H */
</code></pre>
<p>Es mangelt mir ein wenig am Verständnis... Wäre über Hilfe dankbar, denn es wird<br />
nach 2 Werten nichts mehr eingeordnet... <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /></p>
<p>Danke</p>
<p>Gruß</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/162923/baum-problem</link><generator>RSS for Node</generator><lastBuildDate>Sat, 12 Sep 2026 21:38:58 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/162923.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 24 Oct 2006 13:30:29 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Baum Problem on Tue, 24 Oct 2006 13:30:29 GMT]]></title><description><![CDATA[<p>Hi,<br />
ich hab ein Problem mit Bäumen -arbeite mich da gerade erst ein- und zwar möchte ich in einen Baum etwas einfügen und zwar möchte ich nichts nach groß/klein sortieren sondern einfach der Reihe nach immer rein: Links frei -&gt; Nach Links ansonsten Rechts...</p>
<p>Hier mein Code:</p>
<p>CPP:</p>
<pre><code class="language-cpp">#include &quot;SymTable.h&quot;

SymTableNode::SymTableNode( const SymTableValue &amp;Val ) : Left( NULL ), Right( NULL ), Value ( Val )
{
}

SymTableNode::~SymTableNode( void )
{
	if ( Left ) { printf( &quot;L: %s\n&quot;, Left-&gt;Value.Str); delete Left; }
	if ( Right ) { printf( &quot;R: %s\n&quot;, Right-&gt;Value.Str); delete Right; }
}

void SymTableNode::Insert( const SymTableValue &amp;Val )
{
	if ( ( Left == NULL ) || ( Right == NULL ) )
	{
		if ( Left == NULL )
		{
			if ( Left != NULL )
			{
				Left-&gt;Insert( Val );
				return;
			}

			Left = new SymTableNode( Val );
			return;
		}
		else if ( Right == NULL )
		{
			if ( Right != NULL )
			{
				Right-&gt;Insert( Val );
				return;
			}

			Right = new SymTableNode( Val );
			return;
		}
		else
		{
			printf( &quot;%i: Und nun!?\n&quot;, __LINE__ );
		}
	}
	else
	{
		printf( &quot;%i: Und nun!?\n&quot;, __LINE__ );
	}
}
</code></pre>
<p>Header:</p>
<pre><code class="language-cpp">#ifndef SYM_TABLE_H
#define SYM_TABLE_H

#include &lt;stdio.h&gt;

struct SymTableValue
{
	union 
	{ 
		char *Str;
	};

	SymTableValue()
	{
		Str = NULL;
	}
};

class SymTableNode
{
	private:
		SymTableValue Value;
		SymTableNode *Left;
		SymTableNode *Right;
	public:
		SymTableNode( const SymTableValue &amp;Val );
		virtual ~SymTableNode( void );
		void Insert( const SymTableValue &amp;Val );
		SymTableNode *GetLeft( void ) { return Left; } 
		SymTableNode *GetRight( void ) { return Right; } 
};

class SymTable
{
	private:
		SymTableNode Node;
	public:
		void Insert( void );
};

#endif /* SYM_TABLE_H */
</code></pre>
<p>Es mangelt mir ein wenig am Verständnis... Wäre über Hilfe dankbar, denn es wird<br />
nach 2 Werten nichts mehr eingeordnet... <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /></p>
<p>Danke</p>
<p>Gruß</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1160324</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1160324</guid><dc:creator><![CDATA[BraucheHilfe]]></dc:creator><pubDate>Tue, 24 Oct 2006 13:30:29 GMT</pubDate></item><item><title><![CDATA[Reply to Baum Problem on Tue, 24 Oct 2006 16:50:19 GMT]]></title><description><![CDATA[<p>Das ist ja auch kein Wunder.</p>
<p>Du schreibst</p>
<pre><code>if ( ( Left == NULL ) || ( Right == NULL ) )
</code></pre>
<p>nach dem 2. Aufruf sind nunmal Left und Right beide != Null<br />
und du fügst nichts mehr ein.</p>
<p>Ansonsten ist deine Aufgabenstellung, immer nur links einzufügen<br />
sowieso nicht sinnvoll.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1160524</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1160524</guid><dc:creator><![CDATA[Javaner]]></dc:creator><pubDate>Tue, 24 Oct 2006 16:50:19 GMT</pubDate></item><item><title><![CDATA[Reply to Baum Problem on Tue, 24 Oct 2006 17:48:28 GMT]]></title><description><![CDATA[<p>Hmm,<br />
ich wollte eigentlich immer komplett füllen also erst links dann rechts dann in dann das gleiche in den dadurch neu enstandenen Knoten, aber da hab ich so meine Probleme. Wie mache ich das richtig?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1160596</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1160596</guid><dc:creator><![CDATA[BraucheHilfe]]></dc:creator><pubDate>Tue, 24 Oct 2006 17:48:28 GMT</pubDate></item><item><title><![CDATA[Reply to Baum Problem on Wed, 25 Oct 2006 20:15:56 GMT]]></title><description><![CDATA[<p>BraucheHilfe schrieb:</p>
<blockquote>
<p>ich wollte eigentlich immer komplett füllen also erst links dann rechts dann in dann das gleiche in den dadurch neu enstandenen Knoten</p>
</blockquote>
<p>Also wenn ich das wörtlich nehme, soll ein Baum mit z.B. 10 Elementen so</p>
<pre><code>1
      /     \
     2       3
   /   \    / \
  4     5  6   7
 / \   / 
8  9  10
</code></pre>
<p>aussehen. Wobei jede Zahl für ein Element steht. Die 1 für das Element, welches als erstes mit 'insert' aufgenommen wurde; die 2 für das nächste aufgenommenen usw.</p>
<p>BraucheHilfe schrieb:</p>
<blockquote>
<p>.., aber da hab ich so meine Probleme. Wie mache ich das richtig?</p>
</blockquote>
<p>Das wiederum ist 'ne richtige Denksportaufgabe; ich habe eine Weile gebraucht, bis ich eine 'einfache' Lösung gefunden habe. Die Idee dabei ist, dass jeder Knoten implizit eine Nummer besitzt und die Nummer des jeweiligen Parent-Knoten jeweils halb so groß ist. Im ganzen sieht es dann so aus:</p>
<pre><code class="language-cpp">#include &lt;cassert&gt;

template&lt; typename T &gt;
class SymTable
{
    class Node
    {
    public:
        explicit Node( const T&amp; x )
            : m_left( 0 ), m_right( 0 ), m_value( x )
        {}
        ~Node()
        {
            delete m_left;
            delete m_right;
        }

        static Node*&amp; get( Node*&amp; root, int nr )
        {
            if( nr == 1 )   return root;        // Root hat die Nummer 1
            Node* parent = get( root, nr/2 );   // 'nr/2' ist der Vater von 'nr'
            assert( parent );                   // dieser muss da sein, sonst gibt's auch kein Kind
            return nr%2 == 0? parent-&gt;m_left: parent-&gt;m_right;
        }
    private:
        Node* m_left;
        Node* m_right;
        T m_value;
    };

public:
    SymTable() : m_root( 0 ), m_cnt( 0 ) {}
    ~SymTable() { delete m_root; }

    void insert( const T&amp; x )
    {
        Node*&amp; p = Node::get( m_root, ++m_cnt ); // Knoten mit der Nummer m_cnt+1 besorgen
        assert( p == 0 ); // der Knoten ist neu, darf also noch nicht da sein
        p = new Node( x );
    }

private:
    Node* m_root;
    int m_cnt;  // aktuelle Anzahl der Elemente im Baum
    SymTable( const SymTable&amp; );    // -- Kopieren verhindern
    SymTable&amp; operator=( const SymTable&amp; );
};
</code></pre>
<p>Ich habe mir noch erlaubt das 'SymTableValue' durch den Template-Parameter T zu ersetzen und (SymTable)Node ist eine sogenannte nested class von SymTable.</p>
<p>Falls man jetzt noch zum Node die Ausgabe-Funktion - ab Zeile 25 (s.o.) -</p>
<pre><code class="language-cpp">static void print( const Node* nd, std::ostream&amp; out, int indent )
        {
            const int TAB_WIDTH = 4;
            if( nd == 0 ) return;
            print( nd-&gt;m_right, out, indent + TAB_WIDTH );
            if( indent ) out &lt;&lt; std::setw(indent) &lt;&lt; out.fill();
            out &lt;&lt; nd-&gt;m_value &lt;&lt; std::endl;
            print( nd-&gt;m_left, out, indent + TAB_WIDTH );
        }
</code></pre>
<p>hinzufügt und SymTable mit dem &lt;&lt;-operator für ostream versieht (Zeile 42),</p>
<pre><code class="language-cpp">friend std::ostream&amp; operator&lt;&lt;( std::ostream&amp; out, const SymTable&amp; st )
    {
        Node::print( st.m_root, out, 0 );
        return out;
    }
</code></pre>
<p>- nicht zu vergessen #include &lt;iostream&gt; und &lt;iomanip&gt; - so kann man dann folgende kleine Demo zum Laufen bringen.</p>
<pre><code class="language-cpp">#include &quot;SymTable.h&quot; // s.o.
#include &lt;iostream&gt;
#include &lt;string&gt;

int main()
{   
    using namespace std;
    const char* txt[] = { &quot;eins&quot;, &quot;zwei&quot;, &quot;drei&quot;, &quot;vier&quot;, &quot;fuenf&quot;, 
        &quot;sechs&quot;, &quot;sieben&quot;, &quot;acht&quot;, &quot;neun&quot;, &quot;zehn&quot; };
    SymTable&lt; string &gt; t;
    for( const char** s = txt; s != txt + sizeof(txt)/sizeof(*txt); ++s )
        t.insert( *s );
    cout &lt;&lt; t &lt;&lt; endl;
    return 0;
}
</code></pre>
<p>Und die Ausgabe sollte dann sein:</p>
<pre><code>sieben
    drei
        sechs
eins
        fuenf
            zehn
    zwei
            neun
        vier
            acht
</code></pre>
<p>Der gewünschte Baum - um 90Grad nach links gedreht.</p>
<p>Gruß<br />
Werner</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1161473</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1161473</guid><dc:creator><![CDATA[Werner Salomon]]></dc:creator><pubDate>Wed, 25 Oct 2006 20:15:56 GMT</pubDate></item></channel></rss>