Sunday, July 29, 2012

N th level category using Tree Like Implementation in JAVA

Recently while working on one project I had scenario where we has to load N level category and maintain them into memory.Basically I needed something like Tree. So that from any given node I can start traversing down. So this is what I used It is bit customized to my needs but can easily be made generic
Since In my case Category ID was supposed be unique I used that to recognize each element uniquely
So this is my simple CategoyBean.java(Customized to my needs)
package com.dataStructure;

import java.util.List;

public class CategoryBean {
 private String parentID;
 private String catID;
 private List childIDs;
 private String catName;

 public String getCatID() {
  return catID;
 }

 public void setCatID(String catID) {
  this.catID = catID;
 }

 public List getChildIDs() {
  return childIDs;
 }

 public void setChildIDs(List childIDs) {
  this.childIDs = childIDs;
 }

 public String getCatName() {
  return catName;
 }

 public void setCatName(String catName) {
  this.catName = catName;
 }

 public String getParentID() {
  return parentID;
 }

 public void setParentID(String parentID) {
  this.parentID = parentID;
 }
}
I am using Hash Map to store the data. But put method is customized to achieve tree like functionality
package com.dataStructure;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class TreeImpl extends HashMap {
 public static String ROOT_PARENT = "-1";

 public boolean put(CategoryBean catBean) {
  boolean insertStatus = false;
  String parentID = catBean.getParentID();
  if (this.containsKey(parentID)) {
   CategoryBean parent = (CategoryBean) this.get(parentID);
   List children = parent.getChildIDs();
   if(null==children){
    children = new ArrayList();
   }
   children.add(catBean.getCatID());
   parent.setChildIDs(children);
   this.put(parent.getCatID(), parent);
   this.put(catBean.getCatID(), catBean);
   insertStatus = true;
  } else if (ROOT_PARENT.equals(parentID)) {
   System.out.println("Root Node added");
   this.put(catBean.getCatID(), catBean);
  } else {
   System.out.println("No such parent exist");
  }
  return insertStatus;
 }

 public Object get(String key){
  return super.get(key);
 }
}
Now finally lets say I want to have Category implementation as below
Root - Movies
1st level - BollyWood and HollyWood
2nd Level in BollyWood -Aamir Khan Movies,SRk Movies,Govinda
2nd level in HollyWood - Tom Cruise,Brad Pitt

So this is the way We approach t initialize and parse the data structure
package com.dataStructure;

import java.util.List;

public class CallTrees {
 public static int LEFT_INDENT = 0;
 public static void main(String[] args) {
  TreeImpl t = new TreeImpl();
  populateTree(t);  
  parseTree(t, "1");

 }

 private static void parseTree(TreeImpl t, String location) {
  CategoryBean cat = (CategoryBean) t.get(location);
  if (null != cat) {
   System.out.print(cat.getCatName()+"=>");
   List l = cat.getChildIDs();
   if (null != l) {
    int length = l.size();
    System.out.println();
    for (int i = 0; i < length; i++) {
     parseTree(t, l.get(i));
    }
    System.out.println();
   }
  }

 }

 private static void populateTree(TreeImpl t) {
  CategoryBean root = new CategoryBean();
  root.setParentID(TreeImpl.ROOT_PARENT);
  root.setCatID("1");
  root.setCatName("Movies");
  t.put(root);
  CategoryBean firstLevel = new CategoryBean();
  firstLevel.setParentID("1");
  firstLevel.setCatID("2");
  firstLevel.setCatName("BollyWood");
  t.put(firstLevel);
  firstLevel = new CategoryBean();
  firstLevel.setParentID("1");
  firstLevel.setCatID("3");
  firstLevel.setCatName("HollyWood");
  t.put(firstLevel);
  CategoryBean secondLevel = new CategoryBean();
  secondLevel.setParentID("2");
  secondLevel.setCatID("4");
  secondLevel.setCatName("Aamir Khan");
  t.put(secondLevel);
  secondLevel = new CategoryBean();
  secondLevel.setParentID("2");
  secondLevel.setCatID("5");
  secondLevel.setCatName("Sharukh Khan Movies");
  t.put(secondLevel);
  secondLevel = new CategoryBean();
  secondLevel.setParentID("2");
  secondLevel.setCatID("6");
  secondLevel.setCatName("Govinda Movies");
  t.put(secondLevel);
  secondLevel = new CategoryBean();
  secondLevel.setParentID("3");
  secondLevel.setCatID("7");
  secondLevel.setCatName("Tom Cruise Movies");
  t.put(secondLevel);
  secondLevel = new CategoryBean();
  secondLevel.setParentID("3");
  secondLevel.setCatID("8");
  secondLevel.setCatName("Brad Pitt Movies");
  t.put(secondLevel);
 }
}
So final out output when we navigate from ROOT node would be
Root Node added
Movies=>
BollyWood=>
Aamir Khan=>Sharukh Khan Movies=>Govinda Movies=>
HollyWood=>
Tom Cruise Movies=>Brad Pitt Movies=>
This is not a sophisticated implementation but very simple implementation to get Tree like functionality. Additional methods can be added depending on requirements
Any Suggestions are welcome!!