<?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/"
	
	xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Rough Book &#187; generic trees</title>
	<atom:link href="http://vivin.net/tag/generic-trees/feed/" rel="self" type="application/rss+xml" />
	<link>http://vivin.net</link>
	<description>random musings of just another computer nerd</description>
	<lastBuildDate>Tue, 17 Jan 2012 23:32:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Maven project for Generic (n-ary) Tree in Java</title>
		<link>http://vivin.net/2010/03/02/maven-project-for-generic-n-ary-tree-in-java/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=rss</link>
		<comments>http://vivin.net/2010/03/02/maven-project-for-generic-n-ary-tree-in-java/#comments</comments>
		<pubDate>Wed, 03 Mar 2010 04:49:48 +0000</pubDate>
		<dc:creator>vivin</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming and Development]]></category>
		<category><![CDATA[data structures]]></category>
		<category><![CDATA[generic trees]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[k-ary trees]]></category>
		<category><![CDATA[n-ary trees]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[software engineering]]></category>
		<category><![CDATA[trees (data structure)]]></category>

		<guid isPermaLink="false">http://vivin.net/?p=1405</guid>
		<description><![CDATA[Guus was kind enough to make a maven project for the Generic Tree. He also fixed an error in my equals method for the GenericTreeNode (I intended to do Object.equals(Object obj) but was doing GenericTreeNode.equals(GenericTreeNode obj). Since it&#8217;s a maven project, you can easily create a jar out of it and add it as a [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://vivin.net/2010/01/30/generic-n-ary-tree-in-java/#comment-1461">Guus</a> was kind enough to make a maven project for the Generic Tree. He also fixed an error in my <em>equals</em> method for the GenericTreeNode (I intended to do <em>Object.equals(Object obj)</em> but was doing <em>GenericTreeNode.equals(GenericTreeNode obj)</em>. Since it&#8217;s a maven project, you can easily create a <em>jar</em> out of it and add it as a dependency to your project. You can download the project <a href="https://github.com/vivin/GenericTree">here</a>. Thanks Guus!</p>
<br /><a href="http://vivin.net/?p=1405#comments" title="Comments on &quot;Maven project for Generic (n-ary) Tree in Java&quot;"><img src="http://vivin.net/wp-content/plugins/feed-comments-number/image.php?1405" alt="Comments" /></a><img src="http://vivin.net/?ak_action=api_record_view&#38;id=1405&#38;type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://vivin.net/2010/03/02/maven-project-for-generic-n-ary-tree-in-java/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:thumbnail url="http://vivin.net/wp-content/plugins/feed-comments-number/image.php?1405" />
		<media:content url="http://vivin.net/wp-content/plugins/feed-comments-number/image.php?1405" medium="image">
			<media:title type="html">Comments</media:title>
		</media:content>
		<media:content url="http://vivin.net/?ak_action=api_record_view&#38;id=1405&#38;type=feed" medium="image" />
	</item>
		<item>
		<title>Generic (n-ary) Tree in Java</title>
		<link>http://vivin.net/2010/01/30/generic-n-ary-tree-in-java/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=rss</link>
		<comments>http://vivin.net/2010/01/30/generic-n-ary-tree-in-java/#comments</comments>
		<pubDate>Sat, 30 Jan 2010 22:05:13 +0000</pubDate>
		<dc:creator>vivin</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming and Development]]></category>
		<category><![CDATA[data structures]]></category>
		<category><![CDATA[generic trees]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[k-ary trees]]></category>
		<category><![CDATA[n-ary trees]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[software engineering]]></category>
		<category><![CDATA[trees (data structure)]]></category>

		<guid isPermaLink="false">http://vivin.net/?p=1395</guid>
		<description><![CDATA[This project is available on github. Please download from there to make sure you have the latest version. Last week I was solving a problem at work that required the use of a Generic (n-ary) Tree. An n-ary tree is a tree where node can have between 0 and n children. There is a special [...]]]></description>
			<content:encoded><![CDATA[<p><strong>This project is available on <a href="https://github.com/vivin/GenericTree">github</a>. Please download from there to make sure you have the latest version.</strong></p>
<p>Last week I was solving a problem at work that required the use of a Generic (<em>n</em>-ary) Tree. An <em>n</em>-ary tree is a tree where node can have between 0 and <em>n</em> children. There is a special case of <em>n</em>-ary trees where each node can have <em>at most</em> <em>n</em> nodes (<em>k</em>-ary tree). This implementation focuses on the most general case, where any node can have between 0 and <em>n</em> children. Java doesn&#8217;t have a Tree or Tree Node data structure. I couldn&#8217;t find any third-party implementations either (like in commons-lang). So I decided to write my own.<br />
<span id="more-1395"></span><br />
First, we need to define the node of our tree. Our node has two attributes. One is the data, and another is a List which can contain references to the children of that node:</p>
<div id="attachment_1397" class="wp-caption aligncenter" style="width: 310px;  border: 1px solid #dddddd; background-color: #f3f3f3; padding-top: 4px; margin: 10px; text-align:center; display: block; margin-right: auto; margin-left: auto;"><a href="http://vivin.net/wp-content/uploads/2010/01/generictreestructure.png"><img class="size-medium wp-image-1397" title="Generic Tree Node Structure" src="http://vivin.net/wp-content/uploads/2010/01/generictreestructure-300x167.png" alt="Structure of a Generic Tree Node" width="300" height="167" /></a><p style=' padding: 0 4px 5px; margin: 0;'  class="wp-caption-text">Structure of a Generic Tree Node</p></div>
<p>The code for a Generic Tree Node looks like this:</p>
<p><strong>GenericTreeNode.java</strong></p>
<pre class="brush: java">
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GenericTreeNode&lt;T&gt; {

    public T data;
    public List&lt;GenericTreeNode&lt;T&gt;&gt; children;

    public GenericTreeNode() {
        super();
        children = new ArrayList&lt;GenericTreeNode&lt;T&gt;&gt;();
    }

    public GenericTreeNode(T data) {
        this();
        setData(data);
    }

    public List&lt;GenericTreeNode&lt;T&gt;&gt; getChildren() {
        return this.children;
    }

    public int getNumberOfChildren() {
        return getChildren().size();
    }

    public boolean hasChildren() {
        return (getNumberOfChildren() &gt; 0);
    }

    public void setChildren(List&lt;GenericTreeNode&lt;T&gt;&gt; children) {
        this.children = children;
    }

    public void addChild(GenericTreeNode&lt;T&gt; child) {
        children.add(child);
    }

    public void addChildAt(int index, GenericTreeNode&lt;T&gt; child) throws IndexOutOfBoundsException {
        children.add(index, child);
    }

    public void removeChildren() {
        this.children = new ArrayList&lt;GenericTreeNode&lt;T&gt;&gt;();
    }

    public void removeChildAt(int index) throws IndexOutOfBoundsException {
        children.remove(index);
    }

    public GenericTreeNode&lt;T&gt; getChildAt(int index) throws IndexOutOfBoundsException {
        return children.get(index);
    }

    public T getData() {
        return this.data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String toString() {
        return getData().toString();
    }

    public boolean equals(GenericTreeNode&lt;T&gt; node) {
        return node.getData().equals(getData());
    }

    public int hashCode() {
        return getData().hashCode();
    }

    public String toStringVerbose() {
        String stringRepresentation = getData().toString() + &quot;:[&quot;;

        for (GenericTreeNode&lt;T&gt; node : getChildren()) {
            stringRepresentation += node.getData().toString() + &quot;, &quot;;
        }

        //Pattern.DOTALL causes ^ and $ to match. Otherwise it won&#039;t. It&#039;s retarded.
        Pattern pattern = Pattern.compile(&quot;, $&quot;, Pattern.DOTALL);
        Matcher matcher = pattern.matcher(stringRepresentation);

        stringRepresentation = matcher.replaceFirst(&quot;&quot;);
        stringRepresentation += &quot;]&quot;;

        return stringRepresentation;
    }
}
</pre>
<br /><a href="http://vivin.net/?p=1395#comments" title="Comments on &quot;Generic (n-ary) Tree in Java&quot;"><img src="http://vivin.net/wp-content/plugins/feed-comments-number/image.php?1395" alt="Comments" /></a><img src="http://vivin.net/?ak_action=api_record_view&#38;id=1395&#38;type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://vivin.net/2010/01/30/generic-n-ary-tree-in-java/feed/</wfw:commentRss>
		<slash:comments>34</slash:comments>
	
		<media:thumbnail url="http://vivin.net/wp-content/uploads/2010/01/generictreestructure-150x150.png" />
		<media:content url="http://vivin.net/wp-content/uploads/2010/01/generictreestructure.png" medium="image">
			<media:title type="html">Generic Tree Node Structure</media:title>
			<media:description type="html">Structure of a Generic Tree Node</media:description>
			<media:thumbnail url="http://vivin.net/wp-content/uploads/2010/01/generictreestructure-150x150.png" />
		</media:content>
		<media:content url="http://vivin.net/wp-content/plugins/feed-comments-number/image.php?1395" medium="image">
			<media:title type="html">Comments</media:title>
		</media:content>
		<media:content url="http://vivin.net/?ak_action=api_record_view&#38;id=1395&#38;type=feed" medium="image" />
	</item>
	</channel>
</rss>

