<?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[Algorithmus optimieren - Binary Tree Search]]></title><description><![CDATA[<p>Hi,</p>
<p>wie kann ich folgenden algorithmus optimieren? Es sollen alle nodes welche sich in höhe h1 des binären baums befinden in eine liste gespeichert werden...</p>
<pre><code>typedef struct node_info {
		node_info(node *n1, int h): n(n1), height(h) {}
		node *n;
		int height;
	}node_info;

	void add_to_list(std::vector&lt;std::list&lt;node_info&gt;&gt; &amp;l, node *n, int height) {
	  std::vector&lt;std::list&lt;node_info&gt;&gt;::iterator it;
	  std::list&lt;node_info&gt;::iterator it2;
	  bool found = false;

	  for(it = l.begin(); it != l.end(); it++) {
		it2 = (*it).begin();

		if(it2-&gt;height == height) {
		  node_info t(n, height);
		  it-&gt;push_back(t);
		  found = true;
		  break;
		}
	  }

	  if(!found) {
		node_info t(n, height);
		std::list&lt;node_info&gt; l2;
		l2.push_back(t);
		l.push_back(l2);
	  }  
	}

    bool get_nodes_in_height(int h1, std::list&lt;node_info&gt; &amp;out) {
	  typedef struct n_info {
		n_info(node *nn, int h): n(nn), height(h) {}
		node *n;
		int height;	 
	  }n_info;

	  node *n = get_root();
	  std::vector&lt;std::list&lt;node_info&gt;&gt; l;
	  std::queue&lt;n_info&gt; s;
	  int height = 0;

	  s.push(n_info(n, height));

	  while(!s.empty()) {
		n_info nf = s.front();

		height = nf.height;
		node *t = nf.n;

		add_to_list(l, t, height);

		s.pop();

		if(t-&gt;right) {
		  s.push(n_info(t-&gt;right, height+1));
		}
		if(t-&gt;left) {
		  s.push(n_info(t-&gt;left, height+1));
		}
	  }

	  std::list&lt;node_info&gt;::iterator it2 = l[h1].begin();
	  if(it2-&gt;height == h1) {
		  out = l[h1];
		  return true;
	  }

	  return false;
	}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/314032/algorithmus-optimieren-binary-tree-search</link><generator>RSS for Node</generator><lastBuildDate>Sat, 01 Aug 2026 20:57:49 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/314032.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 18 Feb 2013 09:37:26 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Algorithmus optimieren - Binary Tree Search on Mon, 18 Feb 2013 09:37:26 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>wie kann ich folgenden algorithmus optimieren? Es sollen alle nodes welche sich in höhe h1 des binären baums befinden in eine liste gespeichert werden...</p>
<pre><code>typedef struct node_info {
		node_info(node *n1, int h): n(n1), height(h) {}
		node *n;
		int height;
	}node_info;

	void add_to_list(std::vector&lt;std::list&lt;node_info&gt;&gt; &amp;l, node *n, int height) {
	  std::vector&lt;std::list&lt;node_info&gt;&gt;::iterator it;
	  std::list&lt;node_info&gt;::iterator it2;
	  bool found = false;

	  for(it = l.begin(); it != l.end(); it++) {
		it2 = (*it).begin();

		if(it2-&gt;height == height) {
		  node_info t(n, height);
		  it-&gt;push_back(t);
		  found = true;
		  break;
		}
	  }

	  if(!found) {
		node_info t(n, height);
		std::list&lt;node_info&gt; l2;
		l2.push_back(t);
		l.push_back(l2);
	  }  
	}

    bool get_nodes_in_height(int h1, std::list&lt;node_info&gt; &amp;out) {
	  typedef struct n_info {
		n_info(node *nn, int h): n(nn), height(h) {}
		node *n;
		int height;	 
	  }n_info;

	  node *n = get_root();
	  std::vector&lt;std::list&lt;node_info&gt;&gt; l;
	  std::queue&lt;n_info&gt; s;
	  int height = 0;

	  s.push(n_info(n, height));

	  while(!s.empty()) {
		n_info nf = s.front();

		height = nf.height;
		node *t = nf.n;

		add_to_list(l, t, height);

		s.pop();

		if(t-&gt;right) {
		  s.push(n_info(t-&gt;right, height+1));
		}
		if(t-&gt;left) {
		  s.push(n_info(t-&gt;left, height+1));
		}
	  }

	  std::list&lt;node_info&gt;::iterator it2 = l[h1].begin();
	  if(it2-&gt;height == h1) {
		  out = l[h1];
		  return true;
	  }

	  return false;
	}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2299663</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2299663</guid><dc:creator><![CDATA[geri1]]></dc:creator><pubDate>Mon, 18 Feb 2013 09:37:26 GMT</pubDate></item><item><title><![CDATA[Reply to Algorithmus optimieren - Binary Tree Search on Mon, 18 Feb 2013 14:18:35 GMT]]></title><description><![CDATA[<p>Poste mal vollständigen, aber minimalen Code (am besten mit einem großen Beispiel-Baum zum Laufzeit-Vergleich). Dann kann man testen und gucken, ob Verbesserungen auch einen Vorteil bringen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2299716</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2299716</guid><dc:creator><![CDATA[Ramanujan]]></dc:creator><pubDate>Mon, 18 Feb 2013 14:18:35 GMT</pubDate></item><item><title><![CDATA[Reply to Algorithmus optimieren - Binary Tree Search on Mon, 18 Feb 2013 17:08:18 GMT]]></title><description><![CDATA[<p>hier der ganze code:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;stack&gt;
#include &lt;queue&gt;
#include &lt;list&gt;
#include &lt;vector&gt;
#include &lt;algorithm&gt;
using namespace std;

typedef struct node {
    struct node *left;
        struct node *right;
        struct node *parent;
        int value;
}node;

class tree {
private:
        node *root;

public:
        tree(): root(NULL) {}

        node *get_root() {
                return root;

        }

        node *create_node(int data) {
                node *n = new node;
                n-&gt;left = NULL;
                n-&gt;right = NULL;
                n-&gt;parent = NULL;
                n-&gt;value = data;
                return n;
        }

        int search_index(int  in_order[], int start, int end, int value) {
                for(int i = start; i &lt;= end; i++) {
                        if(in_order[i] == value) {
                                return i;
                        }
                }

                return -1;
        }

        node *create_tree_pre_in_order(int pre_order[], int in_order[], int start, int end) {
                static int pre_index = 0;

                if(start &gt; end) {
                        return NULL;
                }

                /* Pick current node from Preorder traversal using preIndex
                and increment preIndex */
                int root_val = pre_order[pre_index++];
                node *n = create_node(root_val);
                if(pre_index == 1) {
                        root = n;
                }

                /* If this node has no children then return */
                if(start == end) {
                        return n;
                }

                /* Else find the index of this node in Inorder traversal */
                int index = search_index(in_order, start, end, root_val);
                if(index == -1) {
                        return NULL;
                }

                /* Using index in Inorder traversal, construct left and
                right subtress */
                n-&gt;left = create_tree_pre_in_order(pre_order, in_order, start, index-1);
                n-&gt;right = create_tree_pre_in_order(pre_order, in_order, index+1, end);

                return n;
        }

        void insert(int value) {
                struct node *curr = root;
                struct node *prev = NULL;
                struct node *tmp = create_node(value);

                if(root == NULL) {
                        root = tmp;
                }
                else {
                        while(curr) {
                                if(value &lt; curr-&gt;value) {
                                        prev = curr;
                                        curr = curr-&gt;left;
                                }
                                else {
                                        prev = curr;
                                        curr = curr-&gt;right;
                                }
                        }

                        curr = prev;

                        if(value &lt; curr-&gt;value) {
                                curr-&gt;left = tmp;       
                        }
                        else {
                                curr-&gt;right = tmp;
                        }

                        tmp-&gt;parent = prev;
                }
        }

        typedef struct node_info {
                node_info(node *n1, int h): n(n1), height(h) {}
                node *n;
                int height;
        }node_info;

        void add_to_list(std::vector&lt;std::list&lt;node_info&gt; &gt; &amp;l, node *n, int height) {
          std::vector&lt;std::list&lt;node_info&gt; &gt;::iterator it;
          std::list&lt;node_info&gt;::iterator it2;
          bool found = false;

          for(it = l.begin(); it != l.end(); it++) {
                it2 = (*it).begin();

                if(it2-&gt;height == height) {
                  node_info t(n, height);
                  it-&gt;push_back(t);
                  found = true;
                  break;
                }
          }

          if(!found) {
                node_info t(n, height);
                std::list&lt;node_info&gt; l2;
                l2.push_back(t);
                l.push_back(l2);
          }  
        }

    bool get_list_nodes_of_height(std::list&lt;node_info&gt; &amp;out, int h1) {
          node *n = get_root();
          std::vector&lt;std::list&lt;node_info&gt; &gt; l;
          std::queue&lt;node_info&gt; s;
          int height = 0;

          s.push(node_info(n, height));

          while(!s.empty()) {
                node_info nf = s.front();

                height = nf.height;
                node *t = nf.n;

                add_to_list(l, t, height);

                s.pop();

                if(t-&gt;right) {
                  s.push(node_info(t-&gt;right, height+1));
                }
                if(t-&gt;left) {
                  s.push(node_info(t-&gt;left, height+1));
                }
          }

          std::list&lt;node_info&gt;::iterator it2 = l[h1].begin();
          if(it2-&gt;height == h1) {
                  out = l[h1];
                  return true;
          }

          return false;
        }
};

int main() {
   int in_order_traversal[] = {15,30,35,40,45,50,60,70,72,75,77,80};
   int pre_order_traversal[] = {50,30,15,40,35,45,70,60,80,75,72,77};

   tree tt;
   tt.create_tree_pre_in_order(pre_order_traversal, in_order_traversal, 0, sizeof(in_order_traversal)/sizeof(int)-1);

   std::list&lt;tree::node_info&gt; t;
   tt.get_list_nodes_of_height(t, 1);

   for(std::list&lt;tree::node_info&gt;::iterator it = t.begin(); it != t.end(); it++) {
           cout &lt;&lt; (*it).n-&gt;value &lt;&lt; endl; 
   }
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2299756</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2299756</guid><dc:creator><![CDATA[geri1]]></dc:creator><pubDate>Mon, 18 Feb 2013 17:08:18 GMT</pubDate></item><item><title><![CDATA[Reply to Algorithmus optimieren - Binary Tree Search on Mon, 18 Feb 2013 19:55:47 GMT]]></title><description><![CDATA[<p>geri1 schrieb:</p>
<blockquote>
<p>wie kann ich folgenden algorithmus optimieren?</p>
</blockquote>
<p>Optimieren kann man immer, wenn man etwas weglassen kann, was nicht zwingend notwendig ist. In Deinem Fall von <code>get_list_nodes_of_height</code> ist das einiges. Zunächst würde ich den <code>std::vector&lt;std::list&lt;node_info&gt; &gt;</code> einfach weglassen. Den braucht man nicht, weil es wird ja letztlich nur ein Element diese <code>vector</code> s benötigt. Damit fällt dann auch die Funktion <code>add_to_list</code> weg, und mit hier das Suchen nach dem Eintrag mit der Höhe ' <code>height</code> '. Und dann kann man noch das Einfügen von <code>node_info</code> -Objekte in die Queue weglassen, die bereits eine höhere Wert von <code>height</code> haben, als der gesuchte ' <code>h1</code> '.<br />
Der Member node_info::height als Ausgabeparameter von <code>get_list_nodes_of_height</code> ist auch nicht notwendig - zumindest nicht in der Demo - und die Information von <code>height</code> ist redundant - habe doch alle Werte den Wert von ' <code>h1</code> ' (Parameter von get_list_nodes_of_height).</p>
<p>Das ganze stelle ich mir so vor:</p>
<pre><code>void get_list_nodes_of_height( std::list&lt; node* &gt;&amp; out, int h1 ) {
        if( !get_root() ) // auch leere Bäume dürfen nicht zum Absturz der SW führen
            return;
        std::queue&lt; node_info &gt; q;
        for( q.push( node_info( get_root(), 0 ) ); !q.empty(); q.pop() ) // for geht bei mir vor while
        {
            const node_info&amp; top = q.front();
            if( top.height == h1 )
                out.push_back( top.n ); // &lt;-- Ausgabe
            else
            {   // falls schon top.height == h1 ist, so machen die Kindknoten keinen Sinn mehr
                if( top.n-&gt;left )
                    q.push( node_info( top.n-&gt;left, top.height + 1 ) );
                if( top.n-&gt;right )
                    q.push( node_info( top.n-&gt;right, top.height + 1 ) );
            }
        }    
    } // return-Wert ist auch unnötig, falls keine Elemente in der Ebene 'h1' vorhanden sind, bleibt out unverändert
</code></pre>
<p><code>add_to_list()</code> entfällt völlig und der Code im main - ab Zeile 187 - ändert sich zu:</p>
<pre><code>std::list&lt; node* &gt; t;
   tt.get_list_nodes_of_height(t, 1);

   for(std::list&lt;node*&gt;::iterator it = t.begin(); it != t.end(); ++it) {
           cout &lt;&lt; (*it)-&gt;value &lt;&lt; endl;
   }
</code></pre>
<p>Tipp: statt einem konkreten Container von Knoten-Pointern würde ich das als Template mit einem Iterator als Ausgabeparameter implementieren. Wenn Du dann in einen sequentiellen Container hinein schreiben möchtest, so geht das mit dem <a href="http://www.cplusplus.com/reference/iterator/back_inserter/" rel="nofollow">std:<img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f519.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--back_arrow"
      title=":back:"
      alt="🔙"
    />inserter</a>. So gewinnst Du Flexibilität.</p>
<p>Gruß<br />
Werner</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2299778</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2299778</guid><dc:creator><![CDATA[Werner Salomon]]></dc:creator><pubDate>Mon, 18 Feb 2013 19:55:47 GMT</pubDate></item><item><title><![CDATA[Reply to Algorithmus optimieren - Binary Tree Search on Mon, 18 Feb 2013 19:36:13 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/11784">@geri</a>: Leider erschließt sich mir Dein Plan überhaupt nicht.<br />
Aber die Ausgabe kriege ich wohl einfacher hin.</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;stack&gt;
#include &lt;queue&gt;
#include &lt;list&gt;
#include &lt;vector&gt;
#include &lt;algorithm&gt;
using namespace std;

typedef struct node {
    struct node *left;
        struct node *right;
        struct node *parent;
        int value;
}node;

class tree {
private:
        node *root;

public:
        tree(): root(NULL) {}

        node *get_root() {
                return root;

        }

        node *create_node(int data) {
                node *n = new node;
                n-&gt;left = NULL;
                n-&gt;right = NULL;
                n-&gt;parent = NULL;
                n-&gt;value = data;
                return n;
        }

        int search_index(int  in_order[], int start, int end, int value) {
                for(int i = start; i &lt;= end; i++) {
                        if(in_order[i] == value) {
                                return i;
                        }
                }

                return -1;
        }

        node *create_tree_pre_in_order(int pre_order[], int in_order[], int start, int end) {
                static int pre_index = 0;

                if(start &gt; end) {
                        return NULL;
                }

                /* Pick current node from Preorder traversal using preIndex
                and increment preIndex */
                int root_val = pre_order[pre_index++];
                node *n = create_node(root_val);
                if(pre_index == 1) {
                        root = n;
                }

                /* If this node has no children then return */
                if(start == end) {
                        return n;
                }

                /* Else find the index of this node in Inorder traversal */
                int index = search_index(in_order, start, end, root_val);
                if(index == -1) {
                        return NULL;
                }

                /* Using index in Inorder traversal, construct left and
                right subtress */
                n-&gt;left = create_tree_pre_in_order(pre_order, in_order, start, index-1);
                n-&gt;right = create_tree_pre_in_order(pre_order, in_order, index+1, end);

                return n;
        }

        void insert(int value) {
                struct node *curr = root;
                struct node *prev = NULL;
                struct node *tmp = create_node(value);

                if(root == NULL) {
                        root = tmp;
                }
                else {
                        while(curr) {
                                if(value &lt; curr-&gt;value) {
                                        prev = curr;
                                        curr = curr-&gt;left;
                                }
                                else {
                                        prev = curr;
                                        curr = curr-&gt;right;
                                }
                        }

                        curr = prev;

                        if(value &lt; curr-&gt;value) {
                                curr-&gt;left = tmp;
                        }
                        else {
                                curr-&gt;right = tmp;
                        }

                        tmp-&gt;parent = prev;
                }
        }

        typedef struct node_info {
                node_info(node *n1, int h): n(n1), height(h) {}
                node *n;
                int height;
        }node_info;

/*        void add_to_list(std::vector&lt;std::list&lt;node_info&gt; &gt; &amp;l, node *n, int height) {
          std::vector&lt;std::list&lt;node_info&gt; &gt;::iterator it;
          std::list&lt;node_info&gt;::iterator it2;
          bool found = false;

          for(it = l.begin(); it != l.end(); it++) {
                it2 = (*it).begin();

                if(it2-&gt;height == height) {
                  node_info t(n, height);
                  it-&gt;push_back(t);
                  found = true;
                  break;
                }
          }

          if(!found) {
                node_info t(n, height);
                std::list&lt;node_info&gt; l2;
                l2.push_back(t);
                l.push_back(l2);
          }
        }*/

    bool get_list_nodes_of_height(std::list&lt;node_info&gt; &amp;out, int h1) {//nodeinfo? komischer typ für rückgabe
        out.clear();
          std::stack&lt;node_info&gt; s;//stack oder queue, fast egal. stack vermutlich minimal schneller.

          s.push(node_info(get_root(), 0));//fehler wenn leer, siehe werner

          while(!s.empty()) {
                node_info nf=s.top();
                s.pop();
//                std::cout&lt;&lt;&quot;inspect &quot;&lt;&lt;nf.n-&gt;value&lt;&lt;'\n';
                if(nf.height&lt;h1){//umdehen, dann kan eine abfrage weg, siehe werner
                    if(nf.n-&gt;right) {
                      s.push(node_info(nf.n-&gt;right, nf.height+1));
                    }
                    if(nf.n-&gt;left) {
                      s.push(node_info(nf.n-&gt;left, nf.height+1));
                    }
                }
                else if(nf.height==h1){
//                    std::cout&lt;&lt;&quot;push &quot;&lt;&lt;nf.n-&gt;value&lt;&lt;'\n';
                    out.push_back(nf);
                }
          }
          return !out.empty();
        }
};

int main() {
   int in_order_traversal[] = {15,30,35,40,45,50,60,70,72,75,77,80};
   int pre_order_traversal[] = {50,30,15,40,35,45,70,60,80,75,72,77};

   tree tt;
   tt.create_tree_pre_in_order(pre_order_traversal, in_order_traversal, 0, sizeof(in_order_traversal)/sizeof(int)-1);

   std::list&lt;tree::node_info&gt; t;
   tt.get_list_nodes_of_height(t, 1);

   for(std::list&lt;tree::node_info&gt;::iterator it = t.begin(); it != t.end(); it++) {
           cout &lt;&lt; (*it).n-&gt;value &lt;&lt; endl;
   }
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2299782</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2299782</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Mon, 18 Feb 2013 19:36:13 GMT</pubDate></item><item><title><![CDATA[Reply to Algorithmus optimieren - Binary Tree Search on Mon, 18 Feb 2013 19:29:11 GMT]]></title><description><![CDATA[<p>Werner Salomon schrieb:</p>
<blockquote>
<pre><code>for( q.push( node_info( get_root(), 0 ) ); !q.empty(); q.pop() ) // for geht vor while
</code></pre>
</blockquote>
<p>Nö, nicht immer.<br />
Hier geht for nicht vor, würde ich sagen.<br />
for impliziert bei mir, daß die Anzahl irgendwie schon vorher feststeht.<br />
Das ist hier nicht gegeben, sondern es wird immer nachgelegt und selber produziert.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2299787</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2299787</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Mon, 18 Feb 2013 19:29:11 GMT</pubDate></item><item><title><![CDATA[Reply to Algorithmus optimieren - Binary Tree Search on Mon, 18 Feb 2013 19:56:17 GMT]]></title><description><![CDATA[<p>volkard schrieb:</p>
<blockquote>
<p>for impliziert bei mir, daß die Anzahl irgendwie schon vorher feststeht.</p>
</blockquote>
<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /> ... das hat 'for' bei mir noch nie impliziert - auch zu C-Zeiten nicht. Das würde ja bedeuten, dass Du auch eine Iteration über eine einfach verlinkte Liste nicht mit for lösen würdest - also eher:</p>
<pre><code>Node* p = start;
    while( p ) {
        // body
        p = p-&gt;next;
    }
</code></pre>
<p>statt:</p>
<pre><code>for( Node* p = start; p; p = p-&gt;next ) {
        // body
    }
</code></pre>
<p>Ich vertrete vielmehr die Ansicht, dass die Kontrolle der Schleife im wesentlichen innerhalb des for-Statements stehen soll - das ist in obigem Beispiel eher der Fall, als mit der while-Lösung.<br />
Aber das ist letztlich Geschmackssache - ich habe das oben mal abgeschwächt.</p>
<p>Gruß<br />
Werner</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2299797</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2299797</guid><dc:creator><![CDATA[Werner Salomon]]></dc:creator><pubDate>Mon, 18 Feb 2013 19:56:17 GMT</pubDate></item><item><title><![CDATA[Reply to Algorithmus optimieren - Binary Tree Search on Mon, 18 Feb 2013 20:05:49 GMT]]></title><description><![CDATA[<p>Werner Salomon schrieb:</p>
<blockquote>
<p>volkard schrieb:</p>
<blockquote>
<p>for impliziert bei mir, daß die Anzahl irgendwie schon vorher feststeht.</p>
</blockquote>
<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /> ... das hat 'for' bei mir noch nie impliziert - auch zu C-Zeiten nicht. Das würde ja bedeuten, dass Du auch eine Iteration über eine einfach verlinkte Liste nicht mit for lösen würdest</p>
</blockquote>
<p>Doch, da steht die Anzahl irgendwie fest. Ich ändere beim Durchlaufen den Container nicht. Ich laufe ungern auf etwas, was ich verändere.<br />
for impliziert bei mir zum Beispiel auch, daß ich die Laufvariable außer im Weiterschaltungsausdruck nicht verändere. Möchte ich das tun, wird while draus.</p>
<p>Werner Salomon schrieb:</p>
<blockquote>
<p>Ich vertrete vielmehr die Ansicht, dass die Kontrolle der Schleife im wesentlichen innerhalb des for-Statements stehen soll - das ist in obigem Beispiel eher der Fall, als mit der while-Lösung.</p>
</blockquote>
<p>Hmm. Ich kann mit stack eine Tiefensuche machen und mit queue eine Breitensuche.<br />
Das</p>
<pre><code>node_info nf=s.top();
               s.pop();
</code></pre>
<p>ist eine Einheit und wäre in anderen Sprachen ein</p>
<pre><code>node_info nf=s.pop();
</code></pre>
<p>Du hast Dich auf die queue festgelegt, indem Du das pop auseinandergerissen hast, um for benutzen zu können.</p>
<blockquote>
<p>Aber das ist letztlich Geschmackssache - ich habe das oben mal abgeschwächt.</p>
</blockquote>
<p>Jo, wollte es trotzdem mal erwähnt haben. Vielleicht hilft's ja mal.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2299803</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2299803</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Mon, 18 Feb 2013 20:05:49 GMT</pubDate></item><item><title><![CDATA[Reply to Algorithmus optimieren - Binary Tree Search on Mon, 18 Feb 2013 20:15:32 GMT]]></title><description><![CDATA[<p>jetzt?</p>
<pre><code>typedef struct node_info {
		node_info(node *n1, int h): n(n1), height(h) {}
		node *n;
		int height;
	}node_info;

	void add_to_list(std::vector&lt;std::list&lt;node_info&gt;&gt; &amp;l, node *n, int height) {
	  std::vector&lt;std::list&lt;node_info&gt;&gt;::iterator it;
	  std::list&lt;node_info&gt;::iterator it2;
	  bool found = false;

	  for(it = l.begin(); it != l.end(); it++) {
		it2 = (*it).begin();

		if(it2-&gt;height == height) {
		  node_info t(n, height);
		  it-&gt;push_back(t);
		  found = true;
		  break;
		}
	  }

	  if(!found) {
		node_info t(n, height);
		std::list&lt;node_info&gt; l2;
		l2.push_back(t);
		l.push_back(l2);
	  }  
	}

    bool get_nodes_in_height(int h1, std::list&lt;node_info&gt; &amp;out) {
	  typedef struct n_info {
		n_info(node *nn, int h): n(nn), height(h) {}
		node *n;
		int height;	 
	  }n_info;

	  node *n = get_root();
	  std::vector&lt;std::list&lt;node_info&gt;&gt; l;
	  std::queue&lt;n_info&gt; s;
	  int height = 0;

	  s.push(n_info(n, height));

	  while(!s.empty()) {
		n_info nf = s.front();

		height = nf.height;
		node *t = nf.n;

		add_to_list(l, t, height);

		s.pop();

		if(t-&gt;right) {
		  s.push(n_info(t-&gt;right, height+1));
		}
		if(t-&gt;left) {
		  s.push(n_info(t-&gt;left, height+1));
		}
	  }

	  if(it2-&gt;height == h1) {
		  out = l[h1];
		  return true;
	  }
const node_info&amp; top = q.front();
            if( top.height == h1 )
                out.push_back( top.n ); // &lt;-

	  return false;
	}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2299808</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2299808</guid><dc:creator><![CDATA[geri1]]></dc:creator><pubDate>Mon, 18 Feb 2013 20:15:32 GMT</pubDate></item><item><title><![CDATA[Reply to Algorithmus optimieren - Binary Tree Search on Mon, 18 Feb 2013 20:24:58 GMT]]></title><description><![CDATA[<p>geri1 schrieb:</p>
<blockquote>
<p>jetzt?</p>
</blockquote>
<p>Hallo geri1,</p>
<p>was willst Du uns jetzt damit sagen?<br />
Das Listing ist fast identisch mit Deiner ersten Version - bis auf die Einfügung ab Zeile 67. ' <code>q</code> ' ist aber gar nicht definiert - sollte also nicht kompilieren, wenn ich mich nicht geirrt habe.</p>
<p>Helfen Dir Volkards und meine Antwort oder hast Du noch Fragen?</p>
<p>Gruß<br />
Werner</p>
<p>PS.:</p>
<p>volkard schrieb:</p>
<blockquote>
<p>Jo, wollte es trotzdem mal erwähnt haben. Vielleicht hilft's ja mal.</p>
</blockquote>
<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /> wobei?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2299809</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2299809</guid><dc:creator><![CDATA[Werner Salomon]]></dc:creator><pubDate>Mon, 18 Feb 2013 20:24:58 GMT</pubDate></item><item><title><![CDATA[Reply to Algorithmus optimieren - Binary Tree Search on Mon, 18 Feb 2013 20:45:06 GMT]]></title><description><![CDATA[<p>So, ich hab mal ein wenig selbst programmiert:</p>
<p>Zum Testen hab ich einen binären Baum mit 10.000.000 Einträgen erstellt. Beim Suchen aller Elemente der Höhe 20 benötigt mein Algorithmus 50 ms, während deiner 4500ms benötigt. Die Anzahl der gefundenen Knoten stimmt überein, von daher gehe ich davon aus, dass mein Algorihtmus korrekt ist.</p>
<p>Weil 50ms eigentlich viel zu schnell ist, hab ich nochmal mit 50M Elementen und Höhe 30 getestet. Dein Algorithmus benötigte hier 102 Sekunden, meiner 22 Sekunden. Der größte Designfehler bei deinem Algorithmus ist, dass du den kompletten Baum durchgehst (daher der große Unterschied beim ersten Test).</p>
<p>Hier nur der neue Code (inkl. geänderter Main):</p>
<pre><code class="language-cpp">void get_list_nodes_of_height_fast(std::list&lt;node_info&gt; &amp;out, int h1) {
    	 std::vector&lt;node_info&gt; stack; // Knoten, dessen rechte Seite noch analysiert werden muss
    	 node* n = get_root(); // aktueller Knoten
    	 int h_akt = 0; // aktuelle Höhe

    	 while (true) {
    		 if (n == NULL) {
    			 // aktueller Ast vollständig ausgewertet: Nächstes Element aus dem Stack nehmen
    			 if (stack.empty())
    				 break;
    			 n = stack.back().n;
    			 h_akt = stack.back().height;
				 stack.pop_back();
    		 }
			 if (h_akt == h1) {
				 // Knoten der gesuchten Höhe
				 out.push_back(node_info(n, h_akt));
				 n = NULL;
			 } else {
				 node* left = n-&gt;left;
				 node* right = n-&gt;right;
				 if (left &amp;&amp; right)
					 // Linker und Rechter Ast existiert =&gt; Rechten zum Stack hinzufügen (und später analyieren
					 stack.push_back(node_info(right, h_akt+1));
				 if (left) {
					 // Linken Ast analysieren
					 n = left;
					 h_akt++;
				 } else if (right) {
					 // Rechten Ast analysieren, falls Linker nicht vorhanden war
					 n = right;
					 h_akt++;
				 } else {
					 // Ast ist komplett analysiert
					 n = NULL;
				 }
			 }
    	 }
     }
}; 

int main() {
	srand(time(0));
	tree tt;
	for (int i = 0; i &lt; 50000000; i++) {
		if (i % 1000000 == 0) cout &lt;&lt; i / 1000000 &lt;&lt; &quot;,&quot; &lt;&lt; flush;
		tt.insert(rand() * RAND_MAX + rand());
	}
	cout &lt;&lt; '\n';

	{
		std::list&lt;tree::node_info&gt; t;
		long long t0 = clock();
		tt.get_list_nodes_of_height(t, 30);
		cout &lt;&lt; &quot;TIME: &quot; &lt;&lt; (clock() - t0) &lt;&lt; endl;
		cout &lt;&lt; &quot;size: &quot; &lt;&lt; t.size() &lt;&lt; endl;

//		for(std::list&lt;tree::node_info&gt;::iterator it = t.begin(); it != t.end(); it++) {
//				cout &lt;&lt; (*it).n-&gt;value &lt;&lt; endl;
//		}
	}

	{
		std::list&lt;tree::node_info&gt; t;
		long long t0 = clock();
		tt.get_list_nodes_of_height_fast(t, 30);
		cout &lt;&lt; &quot;TIME: &quot; &lt;&lt; (clock() - t0) &lt;&lt; endl;
		cout &lt;&lt; &quot;size: &quot; &lt;&lt; t.size() &lt;&lt; endl;

//		for(std::list&lt;tree::node_info&gt;::iterator it = t.begin(); it != t.end(); it++) {
//				cout &lt;&lt; (*it).n-&gt;value &lt;&lt; endl;
//		}
	}
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2299813</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2299813</guid><dc:creator><![CDATA[Ramanujan]]></dc:creator><pubDate>Mon, 18 Feb 2013 20:45:06 GMT</pubDate></item><item><title><![CDATA[Reply to Algorithmus optimieren - Binary Tree Search on Mon, 18 Feb 2013 23:44:07 GMT]]></title><description><![CDATA[<p>geri scheint 0-Plan zu haben <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>
]]></description><link>https://www.c-plusplus.net/forum/post/2299838</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2299838</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Mon, 18 Feb 2013 23:44:07 GMT</pubDate></item><item><title><![CDATA[Reply to Algorithmus optimieren - Binary Tree Search on Tue, 19 Feb 2013 10:22:06 GMT]]></title><description><![CDATA[<p>der letzte geri1 war ein doppelgänger und nicht der orginale! <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>
]]></description><link>https://www.c-plusplus.net/forum/post/2299886</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2299886</guid><dc:creator><![CDATA[geri1]]></dc:creator><pubDate>Tue, 19 Feb 2013 10:22:06 GMT</pubDate></item><item><title><![CDATA[Reply to Algorithmus optimieren - Binary Tree Search on Tue, 19 Feb 2013 10:28:51 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/28594">@Ramanujan</a>: hast du die laufzeit von volkards version auch getestet?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2299889</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2299889</guid><dc:creator><![CDATA[Geri1]]></dc:creator><pubDate>Tue, 19 Feb 2013 10:28:51 GMT</pubDate></item><item><title><![CDATA[Reply to Algorithmus optimieren - Binary Tree Search on Tue, 19 Feb 2013 11:28:34 GMT]]></title><description><![CDATA[<p>jo, hab ich jetzt mal gemacht. Sie ist quasi genauso schnell wie meine. Manchmal war sie 2-3% schneller.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2299900</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2299900</guid><dc:creator><![CDATA[Ramanujan]]></dc:creator><pubDate>Tue, 19 Feb 2013 11:28:34 GMT</pubDate></item></channel></rss>