<?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[Binärer Baum]]></title><description><![CDATA[<p>Hallo,<br />
ich habe eine Frage. Ich gerade beim binären Baum. und habe bisher folgendes.<br />
Node.h</p>
<pre><code class="language-cpp">#ifndef Node_h
#define Node_h Node_h

class CNode
{
	private:
		CNode** pChildren;
		char value;
		int key;
	public:
		CNode(int key, char value);
		~CNode(void);

		//set
		void SetChild(int index, CNode* pChild);
		void SetKey(int key);
		void SetValue(char value);
		bool add(int key, char value);

		//get
		int GetKey();
		CNode* GetChild(int index);

};

#endif
</code></pre>
<p>Node.cpp</p>
<pre><code class="language-cpp">#include &quot;StdAfx.h&quot;
#include &quot;.\node.h&quot;
#include &lt;stdlib.h&gt;
CNode::CNode(int key, char value)
{
	pChildren = new CNode*[2];
	for(int i = 0; i &lt; 2; i++)
	{	
		pChildren[i] = NULL;
	}
	this-&gt;value = value;
	this-&gt;key = key;
}

CNode::~CNode(void)
{
}

//set
void CNode::SetChild(int index, CNode* pChild)
{
	this-&gt;pChildren[index] = pChild;
}

void CNode::SetKey(int key)
{
	this-&gt;key = key;
}

void CNode::SetValue(char value)
{
	this-&gt;value = value;
}

bool CNode::add(int key, char value)
{
  bool result = true;

	if (key &lt; this-&gt;key)
	{
		if (this-&gt;pChildren[0] != NULL)
		{
			result =  this-&gt;pChildren[0]-&gt;add(key, value);
		}
		else
		{
			// In diesem Fall muss ein neuer Knoten mit dem neuen Schlüssel erzeugt werden.
			this-&gt;pChildren[0] = new CNode(key, value);
		}
	}
	else if (key &gt; this-&gt;key)
	{
		if (this-&gt;pChildren[1] != NULL)
		{
			result = this-&gt;pChildren[1]-&gt;add(key, value);
		}
		else
		{
			// In diesem Fall muss ein neuer Knoten mit dem neuen Schlüssel erzeugt werden.
			this-&gt;pChildren[1] = new CNode(key, value);
		}
	}
	else if (key == this-&gt;key)
	{
		// Der Schlüssel existiert schon und darf nicht überschrieben werden.
  		result = false;
	}

	return result;
}

//get
CNode* CNode::GetChild(int index)
{
	return this-&gt;pChildren[index];
}

int CNode::GetKey()
{
	return this-&gt;key;
}
</code></pre>
<p>Tree.h</p>
<pre><code class="language-cpp">#ifndef Tree_h
#define Tree_h Tree_h
#include &quot;Node.h&quot;

class CTree
{
	private:
		CNode* pRoot;

	public:
		CTree(void);
		~CTree(void);

		bool Add(int key, char value);
};

#endif
</code></pre>
<p>Tree.cpp</p>
<pre><code class="language-cpp">#include &quot;StdAfx.h&quot;
#include &quot;.\tree.h&quot;
#include &lt;stdlib.h&gt;

CTree::CTree(void)
{
	pRoot = NULL;
}

CTree::~CTree(void)
{
}

bool CTree::Add(int key, char value)
{
	if(pRoot == NULL)
	{
		this-&gt;pRoot = new CNode(key, value);
		return true;
	}
	else
	{
		return this-&gt;pRoot-&gt;add(key, value);
	}
}
</code></pre>
<p>Eine Frage zur add Methode in Node.cpp.<br />
Diese Methode habe ich so teilweise von jemanden bekommen und in Tree.h die bool Add. Den Rest hab ich selber getippt.<br />
Ich verstehe nicht genau was die add methode in Node.cpp macht.<br />
Sie ist rekursiv, aber wo ist der Rekursionsankser und wo wird<br />
die bool Variable result ausgewrtet ?<br />
Ich hab nene Kopf wie eine Wassermelone.</p>
<p>Kan mir bitte jemand den Code aus der add Methode von Node.cpp erklären ?</p>
<p>Danke,<br />
cmos</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/159050/binärer-baum</link><generator>RSS for Node</generator><lastBuildDate>Thu, 10 Sep 2026 07:44:20 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/159050.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 11 Sep 2006 15:07:20 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Binärer Baum on Mon, 11 Sep 2006 15:07:20 GMT]]></title><description><![CDATA[<p>Hallo,<br />
ich habe eine Frage. Ich gerade beim binären Baum. und habe bisher folgendes.<br />
Node.h</p>
<pre><code class="language-cpp">#ifndef Node_h
#define Node_h Node_h

class CNode
{
	private:
		CNode** pChildren;
		char value;
		int key;
	public:
		CNode(int key, char value);
		~CNode(void);

		//set
		void SetChild(int index, CNode* pChild);
		void SetKey(int key);
		void SetValue(char value);
		bool add(int key, char value);

		//get
		int GetKey();
		CNode* GetChild(int index);

};

#endif
</code></pre>
<p>Node.cpp</p>
<pre><code class="language-cpp">#include &quot;StdAfx.h&quot;
#include &quot;.\node.h&quot;
#include &lt;stdlib.h&gt;
CNode::CNode(int key, char value)
{
	pChildren = new CNode*[2];
	for(int i = 0; i &lt; 2; i++)
	{	
		pChildren[i] = NULL;
	}
	this-&gt;value = value;
	this-&gt;key = key;
}

CNode::~CNode(void)
{
}

//set
void CNode::SetChild(int index, CNode* pChild)
{
	this-&gt;pChildren[index] = pChild;
}

void CNode::SetKey(int key)
{
	this-&gt;key = key;
}

void CNode::SetValue(char value)
{
	this-&gt;value = value;
}

bool CNode::add(int key, char value)
{
  bool result = true;

	if (key &lt; this-&gt;key)
	{
		if (this-&gt;pChildren[0] != NULL)
		{
			result =  this-&gt;pChildren[0]-&gt;add(key, value);
		}
		else
		{
			// In diesem Fall muss ein neuer Knoten mit dem neuen Schlüssel erzeugt werden.
			this-&gt;pChildren[0] = new CNode(key, value);
		}
	}
	else if (key &gt; this-&gt;key)
	{
		if (this-&gt;pChildren[1] != NULL)
		{
			result = this-&gt;pChildren[1]-&gt;add(key, value);
		}
		else
		{
			// In diesem Fall muss ein neuer Knoten mit dem neuen Schlüssel erzeugt werden.
			this-&gt;pChildren[1] = new CNode(key, value);
		}
	}
	else if (key == this-&gt;key)
	{
		// Der Schlüssel existiert schon und darf nicht überschrieben werden.
  		result = false;
	}

	return result;
}

//get
CNode* CNode::GetChild(int index)
{
	return this-&gt;pChildren[index];
}

int CNode::GetKey()
{
	return this-&gt;key;
}
</code></pre>
<p>Tree.h</p>
<pre><code class="language-cpp">#ifndef Tree_h
#define Tree_h Tree_h
#include &quot;Node.h&quot;

class CTree
{
	private:
		CNode* pRoot;

	public:
		CTree(void);
		~CTree(void);

		bool Add(int key, char value);
};

#endif
</code></pre>
<p>Tree.cpp</p>
<pre><code class="language-cpp">#include &quot;StdAfx.h&quot;
#include &quot;.\tree.h&quot;
#include &lt;stdlib.h&gt;

CTree::CTree(void)
{
	pRoot = NULL;
}

CTree::~CTree(void)
{
}

bool CTree::Add(int key, char value)
{
	if(pRoot == NULL)
	{
		this-&gt;pRoot = new CNode(key, value);
		return true;
	}
	else
	{
		return this-&gt;pRoot-&gt;add(key, value);
	}
}
</code></pre>
<p>Eine Frage zur add Methode in Node.cpp.<br />
Diese Methode habe ich so teilweise von jemanden bekommen und in Tree.h die bool Add. Den Rest hab ich selber getippt.<br />
Ich verstehe nicht genau was die add methode in Node.cpp macht.<br />
Sie ist rekursiv, aber wo ist der Rekursionsankser und wo wird<br />
die bool Variable result ausgewrtet ?<br />
Ich hab nene Kopf wie eine Wassermelone.</p>
<p>Kan mir bitte jemand den Code aus der add Methode von Node.cpp erklären ?</p>
<p>Danke,<br />
cmos</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1135271</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1135271</guid><dc:creator><![CDATA[c-mos]]></dc:creator><pubDate>Mon, 11 Sep 2006 15:07:20 GMT</pubDate></item><item><title><![CDATA[Reply to Binärer Baum on Mon, 11 Sep 2006 15:20:32 GMT]]></title><description><![CDATA[<p>Dieser Thread wurde von Moderator/in <a href="http://www.c-plusplus.net/forum/profile.php?mode=viewprofile&amp;u=2822" rel="nofollow">SideWinder</a> aus dem Forum <a href="http://www.c-plusplus.net/forum/viewforum.php?f=13" rel="nofollow">DOS und Win32-Konsole</a> in das Forum <a href="http://www.c-plusplus.net/forum/viewforum.php?f=15" rel="nofollow">C++</a> verschoben.</p>
<p>Im Zweifelsfall bitte auch folgende Hinweise beachten:<br />
<a href="http://www.c-plusplus.net/forum/viewtopic.php?t=39405" rel="nofollow">C/C++ Forum :: FAQ - Sonstiges :: Wohin mit meiner Frage?</a></p>
<p><em>Dieses Posting wurde automatisch erzeugt.</em></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1135289</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1135289</guid><dc:creator><![CDATA[C++ Forumbot]]></dc:creator><pubDate>Mon, 11 Sep 2006 15:20:32 GMT</pubDate></item></channel></rss>