<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>§ &#8211; Brombo Software development</title>
	<atom:link href="http://www.brombo.co.uk/archives/tag/%C2%A7/feed" rel="self" type="application/rss+xml" />
	<link>http://www.brombo.co.uk</link>
	<description>C++ and more</description>
	<lastBuildDate>Wed, 23 May 2018 11:56:39 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>http://www.brombo.co.uk/wp-content/uploads/2017/12/cropped-IMAG0894-32x32.jpg</url>
	<title>§ &#8211; Brombo Software development</title>
	<link>http://www.brombo.co.uk</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Tree traversals</title>
		<link>http://www.brombo.co.uk/archives/303</link>
					<comments>http://www.brombo.co.uk/archives/303#respond</comments>
		
		<dc:creator><![CDATA[brombo]]></dc:creator>
		<pubDate>Wed, 23 May 2018 11:56:39 +0000</pubDate>
				<category><![CDATA[Uncategorised]]></category>
		<category><![CDATA[§]]></category>
		<guid isPermaLink="false">http://www.brombo.co.uk/?p=303</guid>

					<description><![CDATA[Preamble None standard tree traversals. Determine the height of a tree, and height order tree traversal. Often used as interview questions. Determine height of a tree Iterate to the bottom of the tree and on returning return the max height of each branch, add one for this current level. Code: [js] int height(Node* node_) { &#8230; <p class="link-more"><a href="http://www.brombo.co.uk/archives/303" class="more-link">Continue reading<span class="screen-reader-text"> "Tree traversals"</span></a></p>]]></description>
										<content:encoded><![CDATA[<p><strong>Preamble</strong><br />
None standard tree traversals. Determine the height of a tree, and height order tree traversal. Often used as interview questions.</p>
<p><strong>Determine height of a tree</strong><br />
Iterate to the bottom of the tree and on returning return the max height of each branch, add one for this current level.</p>
<p><em>Code:</em><br />
[js]<br />
int height(Node* node_)<br />
{<br />
    if (node_ == nullptr)<br />
    {<br />
        return 0;<br />
    }</p>
<p>    int left  = height(node_->_left);<br />
    int right = height(node_->_right);</p>
<p>    return max(left, right) + 1;<br />
}<br />
[/js]</p>
<p><strong>Height order traversal</strong><br />
Iterate over all nodes by using a queue to push each level into the queue as it occurs.</p>
<p><em>Code:</em><br />
[js]<br />
queue<Node*> d;</p>
<p>void heightWiseTransversal(Node* node_)<br />
{<br />
	if (node_ == nullptr)<br />
	{<br />
		return;<br />
	}</p>
<p>	cout << node_->_data << endl;

	d.push(node_->_left);<br />
	d.push(node_->_right);</p>
<p>	Node* frontNode1 = d.front();<br />
	d.pop();<br />
	heightWiseTransversal(frontNode1);</p>
<p>	Node* frontNode2 = d.front();<br />
	d.pop();<br />
	heightWiseTransversal(frontNode2);<br />
}<br />
[/js]</p>
<p><strong>Full code</strong><br />
Just in case you want to run it.</p>
<p>[js]<br />
#include <iostream><br />
#include <memory><br />
#include <queue><br />
using namespace std;</p>
<p>class Node<br />
{<br />
public:<br />
	Node(string data_, Node* left_, Node* right_) : _data(data_), _left(left_), _right(right_) {}<br />
	string _data;<br />
	Node* _left;<br />
	Node* _right;<br />
};</p>
<p>queue<Node*> d;</p>
<p>void heightWiseTransversal(Node* node_)<br />
{<br />
	if (node_ == nullptr)<br />
	{<br />
		return;<br />
	}</p>
<p>	cout << node_->_data << endl;

	d.push(node_->_left);<br />
	d.push(node_->_right);</p>
<p>	Node* frontNode1 = d.front();<br />
	d.pop();<br />
	heightWiseTransversal(frontNode1);</p>
<p>	Node* frontNode2 = d.front();<br />
	d.pop();<br />
	heightWiseTransversal(frontNode2);<br />
}</p>
<p>int height(Node* node_)<br />
{<br />
    if (node_ == nullptr)<br />
    {<br />
        return 0;<br />
    }</p>
<p>    int left  = height(node_->_left);<br />
    int right = height(node_->_right);</p>
<p>    return max(left, right) + 1;<br />
}</p>
<p>int main()<br />
{<br />
	//Note I keep lifetime management separate from functionality!</p>
<p>	unique_ptr<Node> G = make_unique<Node>(&#8220;G4&#8221;, nullptr, nullptr);<br />
	unique_ptr<Node> F = make_unique<Node>(&#8220;F3&#8221;, nullptr, nullptr);<br />
	unique_ptr<Node> E = make_unique<Node>(&#8220;E3&#8221;, nullptr, nullptr);<br />
	unique_ptr<Node> D = make_unique<Node>(&#8220;D3&#8221;, G.get(), nullptr);<br />
	unique_ptr<Node> C = make_unique<Node>(&#8220;C2&#8221;, E.get(), F.get());<br />
	unique_ptr<Node> B = make_unique<Node>(&#8220;B2&#8221;, D.get(), nullptr);<br />
    unique_ptr<Node> A = make_unique<Node>(&#8220;A1&#8221;, B.get(), C.get());</p>
<p>    cout << "Height wise traversal" << endl;
    heightWiseTransversal(A.get());

    cout << endl;

    cout << "height = " << height(A.get()) << endl;

	return 0;
}
[/js]

<strong>Output</strong><br />
Height wise traversal<br />
A1<br />
B2<br />
C2<br />
D3<br />
E3<br />
F3<br />
G4</p>
<p>Height = 4</p>
]]></content:encoded>
					
					<wfw:commentRss>http://www.brombo.co.uk/archives/303/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
