<?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>fforw.de &#187; database</title>
	<atom:link href="http://fforw.de/tag/database/feed/" rel="self" type="application/rss+xml" />
	<link>http://fforw.de</link>
	<description>skip the boring parts</description>
	<lastBuildDate>Sat, 12 Nov 2011 11:54:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Storing hierarchical data in a database</title>
		<link>http://fforw.de/post/storing-hierarchical-data-in-a-database/</link>
		<comments>http://fforw.de/post/storing-hierarchical-data-in-a-database/#comments</comments>
		<pubDate>Wed, 24 May 2006 00:50:34 +0000</pubDate>
		<dc:creator>fforw</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[download]]></category>

		<guid isPermaLink="false">http://blog.willie/?p=25</guid>
		<description><![CDATA[When looking for a good way to store nested comments in a SQL database I stumbled upon the explanation of an algorithm called &#8220;modified preorder tree traversal&#8221; algorithm [2]. Instead of storing a parent-child relationship into the database and having to do one SQL query for every node, it allows querying the whole comment tree [...]]]></description>
			<content:encoded><![CDATA[<p> When looking for a good way to store nested comments in a SQL database I stumbled upon the explanation of an algorithm called &#8220;modified preorder tree traversal&#8221; algorithm <a href="http://www.sitepoint.com/article/hierarchical-data-database/2/">[2]</a>. Instead of storing a parent-child relationship into the database and having to do one SQL query for every node, it allows querying the whole comment tree (and subtrees) with a single query. </p>
<hr />
<p> The basic idea is to walk along the outside of the tree hierarchy and to number the left and right side of every node along the way:</p>
<p style="text-align: center"> <img src="../../static/image/user/fforw/tree.png" alt="" /> </p>
<p> While this is rather unintuitive it allows to: </p>
<ul>
<li>Get the sub tree of an node by selecting all nodes whose left value is between the left and the right value of the top node of the subtree </li>
<li>Get the path of a node by selecting all nodes whose left value is smaller than the left value of the node in question <strong>and</strong> whose right value is larger than its right value</li>
<li>Determine the number of children of a node by calculating:<br /> <em>( right value &#8211; left value -1 ) / 2</em></li>
</ul>
<p>  The disadvantage of this approach is that the addition of a single node leads to changes in all nodes to the &#8220;right&#8221; of it. After pondering the problem for a while I came up with a variant of the algorithm that gets rid of that. Instead of using simple consecutive numbers for the left and right values I wrote an algorithm which spreads all the left/right values over a fixed range of numbers. The top node starts with a fixed left and right value and children are inserted by splitting that interval into smaller sub-intervals. This makes it possible to not change the key values on inserting and do that insert with a single writing operation.
<p> <strong>Links:</strong> </p>
<ol>
<li> <a href="../../static/files/addMessage.sql" title="shdiad">Postgresql stored procedure implementing the keyspace variant</a></li>
<li> <a href="http://www.sitepoint.com/article/hierarchical-data-database/2/">Modified preorder tree traversal algorithm</a><br />Another explanation of the modified tree traversal algorithm with code samples in PHP</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://fforw.de/post/storing-hierarchical-data-in-a-database/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

