Permalink
Browse files

First Commit.

  • Loading branch information...
0 parents commit 6dbebafbc963d357c7e6600b3bc2ed8516ef533d @vivin committed Mar 7, 2010
10 LICENSE
@@ -0,0 +1,10 @@
+Copyright (c) 2010, Vivin Suresh Paliath
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
+ * Neither the name of the <ORGANIZATION> nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2 README
@@ -0,0 +1,2 @@
+Implementation of Generic (n-ary) Tree in Java.
+Author: Vivin Suresh Paliath
47 pom.xml
@@ -0,0 +1,47 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>net.vivin</groupId>
+ <artifactId>generictree</artifactId>
+ <version>1.0</version>
+ <name>GenericTree</name>
+ <description>A generic (n-ary) tree implementation.</description>
+ <url>http://vivin.net/2010/01/30/generic-n-ary-tree-in-java</url>
+ <inceptionYear>2010</inceptionYear>
+ <developers>
+ <developer>
+ <name>Vivin Paliath</name>
+ <url>http://vivin.net/</url>
+ <timezone>-7</timezone>
+ <roles>
+ <role>Original author</role>
+ <role>Developer</role>
+ </roles>
+ </developer>
+ </developers>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>2.1</version>
+ <configuration>
+ <source>1.5</source>
+ <target>1.5</target>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+ <dependencies>
+ <dependency>
+ <groupId>org.testng</groupId>
+ <artifactId>testng</artifactId>
+ <version>5.11</version>
+ <scope>test</scope>
+ <classifier>jdk15</classifier>
+ </dependency>
+ </dependencies>
+ <properties>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ </properties>
+</project>
@@ -0,0 +1,191 @@
+/*
+ Copyright 2010 Vivin Suresh Paliath
+ Distributed under the BSD License
+*/
+
+package net.vivin;
+
+import java.util.*;
+
+public class GenericTree<T> {
+
+ private GenericTreeNode<T> root;
+
+ public GenericTree() {
+ super();
+ }
+
+ public GenericTreeNode<T> getRoot() {
+ return this.root;
+ }
+
+ public void setRoot(GenericTreeNode<T> root) {
+ this.root = root;
+ }
+
+ public int getNumberOfNodes() {
+ int numberOfNodes = 0;
+
+ if(root != null) {
+ numberOfNodes = auxiliaryGetNumberOfNodes(root) + 1; //1 for the root!
+ }
+
+ return numberOfNodes;
+ }
+
+ private int auxiliaryGetNumberOfNodes(GenericTreeNode<T> node) {
+ int numberOfNodes = node.getNumberOfChildren();
+
+ for(GenericTreeNode<T> child : node.getChildren()) {
+ numberOfNodes += auxiliaryGetNumberOfNodes(child);
+ }
+
+ return numberOfNodes;
+ }
+
+ public boolean exists(GenericTreeNode<T> nodeToFind) {
+ return (find(nodeToFind) != null);
+ }
+
+ public GenericTreeNode<T> find(GenericTreeNode<T> nodeToFind) {
+ GenericTreeNode<T> returnNode = null;
+
+ if(root != null) {
+ returnNode = auxiliaryFind(root, nodeToFind);
+ }
+
+ return returnNode;
+ }
+
+ private GenericTreeNode<T> auxiliaryFind(GenericTreeNode<T> currentNode, GenericTreeNode<T> nodeToFind) {
+ GenericTreeNode<T> returnNode = null;
+ int i = 0;
+
+ if (currentNode.equals(nodeToFind)) {
+ returnNode = currentNode;
+ }
+
+ else if(currentNode.hasChildren()) {
+ i = 0;
+ while(returnNode == null && i < currentNode.getNumberOfChildren()) {
+ returnNode = auxiliaryFind(currentNode.getChildAt(i), nodeToFind);
+ i++;
+ }
+ }
+
+ return returnNode;
+ }
+
+ public boolean isEmpty() {
+ return (root == null);
+ }
+
+ public List<GenericTreeNode<T>> build(GenericTreeTraversalOrderEnum traversalOrder) {
+ List<GenericTreeNode<T>> returnList = null;
+
+ if(root != null) {
+ returnList = build(root, traversalOrder);
+ }
+
+ return returnList;
+ }
+
+ public List<GenericTreeNode<T>> build(GenericTreeNode<T> node, GenericTreeTraversalOrderEnum traversalOrder) {
+ List<GenericTreeNode<T>> traversalResult = new ArrayList<GenericTreeNode<T>>();
+
+ if(traversalOrder == GenericTreeTraversalOrderEnum.PRE_ORDER) {
+ buildPreOrder(node, traversalResult);
+ }
+
+ else if(traversalOrder == GenericTreeTraversalOrderEnum.POST_ORDER) {
+ buildPostOrder(node, traversalResult);
+ }
+
+ return traversalResult;
+ }
+
+ private void buildPreOrder(GenericTreeNode<T> node, List<GenericTreeNode<T>> traversalResult) {
+ traversalResult.add(node);
+
+ for(GenericTreeNode<T> child : node.getChildren()) {
+ buildPreOrder(child, traversalResult);
+ }
+ }
+
+ private void buildPostOrder(GenericTreeNode<T> node, List<GenericTreeNode<T>> traversalResult) {
+ for(GenericTreeNode<T> child : node.getChildren()) {
+ buildPostOrder(child, traversalResult);
+ }
+
+ traversalResult.add(node);
+ }
+
+ public Map<GenericTreeNode<T>, Integer> buildWithDepth(GenericTreeTraversalOrderEnum traversalOrder) {
+ Map<GenericTreeNode<T>, Integer> returnMap = null;
+
+ if(root != null) {
+ returnMap = buildWithDepth(root, traversalOrder);
+ }
+
+ return returnMap;
+ }
+
+ public Map<GenericTreeNode<T>, Integer> buildWithDepth(GenericTreeNode<T> node, GenericTreeTraversalOrderEnum traversalOrder) {
+ Map<GenericTreeNode<T>, Integer> traversalResult = new LinkedHashMap<GenericTreeNode<T>, Integer>();
+
+ if(traversalOrder == GenericTreeTraversalOrderEnum.PRE_ORDER) {
+ buildPreOrderWithDepth(node, traversalResult, 0);
+ }
+
+ else if(traversalOrder == GenericTreeTraversalOrderEnum.POST_ORDER) {
+ buildPostOrderWithDepth(node, traversalResult, 0);
+ }
+
+ return traversalResult;
+ }
+
+ private void buildPreOrderWithDepth(GenericTreeNode<T> node, Map<GenericTreeNode<T>, Integer> traversalResult, int depth) {
+ traversalResult.put(node, depth);
+
+ for(GenericTreeNode<T> child : node.getChildren()) {
+ buildPreOrderWithDepth(child, traversalResult, depth + 1);
+ }
+ }
+
+ private void buildPostOrderWithDepth(GenericTreeNode<T> node, Map<GenericTreeNode<T>, Integer> traversalResult, int depth) {
+ for(GenericTreeNode<T> child : node.getChildren()) {
+ buildPostOrderWithDepth(child, traversalResult, depth + 1);
+ }
+
+ traversalResult.put(node, depth);
+ }
+
+ public String toString() {
+ /*
+ We're going to assume a pre-order traversal by default
+ */
+
+ String stringRepresentation = "";
+
+ if(root != null) {
+ stringRepresentation = build(GenericTreeTraversalOrderEnum.PRE_ORDER).toString();
+
+ }
+
+ return stringRepresentation;
+ }
+
+ public String toStringWithDepth() {
+ /*
+ We're going to assume a pre-order traversal by default
+ */
+
+ String stringRepresentation = "";
+
+ if(root != null) {
+ stringRepresentation = buildWithDepth(GenericTreeTraversalOrderEnum.PRE_ORDER).toString();
+ }
+
+ return stringRepresentation;
+ }
+}
@@ -0,0 +1,126 @@
+/*
+ Copyright 2010 Visin Suresh Paliath
+ Distributed under the BSD license
+*/
+
+package net.vivin;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public class GenericTreeNode<T> {
+
+ public T data;
+ public List<GenericTreeNode<T>> children;
+
+ public GenericTreeNode() {
+ super();
+ children = new ArrayList<GenericTreeNode<T>>();
+ }
+
+ public GenericTreeNode(T data) {
+ this();
+ setData(data);
+ }
+
+ public List<GenericTreeNode<T>> getChildren() {
+ return this.children;
+ }
+
+ public int getNumberOfChildren() {
+ return getChildren().size();
+ }
+
+ public boolean hasChildren() {
+ return (getNumberOfChildren() > 0);
+ }
+
+ public void setChildren(List<GenericTreeNode<T>> children) {
+ this.children = children;
+ }
+
+ public void addChild(GenericTreeNode<T> child) {
+ children.add(child);
+ }
+
+ public void addChildAt(int index, GenericTreeNode<T> child) throws IndexOutOfBoundsException {
+ children.add(index, child);
+ }
+
+ public void removeChildren() {
+ this.children = new ArrayList<GenericTreeNode<T>>();
+ }
+
+ public void removeChildAt(int index) throws IndexOutOfBoundsException {
+ children.remove(index);
+ }
+
+ public GenericTreeNode<T> 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();
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null) {
+ return false;
+ }
+ if (getClass() != obj.getClass()) {
+ return false;
+ }
+ GenericTreeNode<?> other = (GenericTreeNode<?>) obj;
+ if (data == null) {
+ if (other.data != null) {
+ return false;
+ }
+ } else if (!data.equals(other.data)) {
+ return false;
+ }
+ return true;
+ }
+
+ /* (non-Javadoc)
+ * @see java.lang.Object#hashCode()
+ */
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((data == null) ? 0 : data.hashCode());
+ return result;
+ }
+
+ public String toStringVerbose() {
+ String stringRepresentation = getData().toString() + ":[";
+
+ for (GenericTreeNode<T> node : getChildren()) {
+ stringRepresentation += node.getData().toString() + ", ";
+ }
+
+ //Pattern.DOTALL causes ^ and $ to match. Otherwise it won't. It's retarded.
+ Pattern pattern = Pattern.compile(", $", Pattern.DOTALL);
+ Matcher matcher = pattern.matcher(stringRepresentation);
+
+ stringRepresentation = matcher.replaceFirst("");
+ stringRepresentation += "]";
+
+ return stringRepresentation;
+ }
+}
+
@@ -0,0 +1,11 @@
+/*
+ Copyright 2010 Vivin Suresh Paliath
+ Distributed under the BSD License
+*/
+
+package net.vivin;
+
+public enum GenericTreeTraversalOrderEnum {
+ PRE_ORDER,
+ POST_ORDER
+}
Oops, something went wrong.

0 comments on commit 6dbebaf

Please sign in to comment.