<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Binary Trees :: CS3 course</title><link>https://robark.gitlab.io/btree/index.html</link><description>Binary Trees</description><generator>Hugo</generator><language>en-us</language><lastBuildDate>Wed, 23 Jun 2021 23:05:30 +0000</lastBuildDate><atom:link href="https://robark.gitlab.io/btree/index.xml" rel="self" type="application/rss+xml"/><item><title>Intro</title><link>https://robark.gitlab.io/btree/intro/index.html</link><pubDate>Tue, 22 Jun 2021 21:56:15 +0000</pubDate><guid>https://robark.gitlab.io/btree/intro/index.html</guid><description>Binary Tree Introduction</description></item><item><title>Traversal</title><link>https://robark.gitlab.io/btree/traversal/index.html</link><pubDate>Wed, 23 Jun 2021 23:05:30 +0000</pubDate><guid>https://robark.gitlab.io/btree/traversal/index.html</guid><description>Binary Tree Traversal #include &lt;queue&gt; #include &lt;iostream&gt; using namespace std; struct node{ node(int x, node* l, node* r); int c; node* left; node* right; //bool operator&lt;(conc node &amp;rhs) conc{ // return ( c &lt; rhs.c ); //} }; node::node(int x, node* l=0, node* r=0){ c = x; left = l; right = r; } void prewalk(node* nd){ //print, left, right cout&lt;&lt; nd-&gt;c &lt;&lt;" "; if (nd-&gt;left != 0 ) prewalk(nd-&gt;left); if (nd-&gt;right != 0 ) prewalk(nd-&gt;right); //better sol /* if (nd == 0) return; cout&lt;&lt; nd-&gt;c &lt;&lt; " "; prewalk(nd-&gt;left); prewalk(nd-&gt;right); */ } void postwalk(node* nd){ //left, right, print if (nd-&gt;left != 0 ) postwalk(nd-&gt;left); if (nd-&gt;right != 0 ) postwalk(nd-&gt;right); cout&lt;&lt; nd-&gt;c &lt;&lt;" "; //better sol /* if (nd == 0) return; postwalk(nd-&gt;left); postwalk(nd-&gt;right); cout&lt;&lt; nd-&gt;c &lt;&lt; " "; */ } void inwalk(node* nd){ //left, print, right if (nd-&gt;left != 0 ) inwalk(nd-&gt;left); cout&lt;&lt; nd-&gt;c &lt;&lt;" "; if (nd-&gt;right != 0 ) inwalk(nd-&gt;right); //better sol /* if (nd == 0) return; inwalk(nd-&gt;left); cout&lt;&lt; nd-&gt;c &lt;&lt; " "; inwalk(nd-&gt;right); */ } void bfirstwalk(node* root){ queue &lt;node*&gt; q; node* n; q.push(root); while ( !q.empty() ) { n = q.front(); cout &lt;&lt; n-&gt;c &lt;&lt;" "; if ( n-&gt;left != 0 ) q.push(n-&gt;left); if ( n-&gt;right != 0 ) q.push(n-&gt;right); q.pop(); } cout&lt;&lt;endl; } //alternate method which pushes null nodes /* void bfirstwalk(node* root){ queue &lt;node*&gt; q; node* n; q.push(root); while ( !q.empty() ) { if (q.front() != 0) { n = q.front(); cout &lt;&lt; n-&gt;c &lt;&lt;","; q.push(n-&gt;left); q.push(n-&gt;right); } q.pop(); } cout&lt;&lt;endl; } */ /* node* NewNode(int c){ node* np = new node; np-&gt;c = c; np-&gt;left = 0; np-&gt;right = 0; return np; } */ /* must pass nd by ref since we may need to insert at the root node if tree is empty and hence change the root addr */ void insert(node* &amp;nd, int c){ if (nd == 0) { //nd = NewNode(c); nd = new node(c); return; } if (c &lt; nd-&gt;c) { insert(nd-&gt;left, c); } else { insert(nd-&gt;right, c); } } void btfree(node* nd){ //must be post order for deleting (fails if root=0) if (nd-&gt;left != 0) btfree(nd-&gt;left); if (nd-&gt;right != 0) btfree(nd-&gt;right); delete nd; nd = 0; //better sol /* if (nd == 0) return; btfree(nd-&gt;left); btfree(nd-&gt;right); delete nd; nd = 0; */ } //alternate better solution (check after instead of before) /* void btfree(node* nd){ //must be post order for deleting if (nd !=0) //same as if (nd) { btfree(nd-&gt;left); btfree(nd-&gt;right); delete nd; nd = 0; } } */ int main(){ const int iarray[7] = {4,2,6,1,3,5,7}; node* root=0; int x; for (x=0; x &lt; 7 ; x++) { insert(root, iarray[x]); //walk(root); //cout&lt;&lt;endl; } cout&lt;&lt;"prewalk "; prewalk(root); cout&lt;&lt;endl; cout&lt;&lt;"postwalk "; postwalk(root); cout&lt;&lt;endl; cout&lt;&lt;"inwalk "; inwalk(root); cout&lt;&lt;endl; cout&lt;&lt;"bfirstwalk "; bfirstwalk(root); cout&lt;&lt;endl; btfree(root); return (0); }</description></item></channel></rss>