Well I have to implement tree programming here , what i mean by tree programming is i have data stored in tree format i,e I have parent object which will have child objects of same type the level of depth can go any long:
I am able to store objects in tree format and also able to display properly Here is code but I am facing problems when i have to filter some child nodes based on some conditions:
I have two question is this code fine is there anything wrong with desin Plus how to handle removing child node in tree scenation where child can be in any place.Below is code
package menu;
import java.util.List;
public class Links{
private String nodeName;
private List<Links> children;
public List<Links> getChildren(){
return children;
}
public void setChildren( List<Links> children ){
this.children = children;
}
public String getNodeName(){
return nodeName;
}
public void setNodeName( String nodeName ){
this.nodeName = nodeName;
}
}
package menu;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.chartis.gp.support.util.BrokerSupportUtil;
import com.chartis.gp.support.vo.Links;
import com.chartis.kernel.user.UserVO;
import com.chartis.kernel.utils.Utils;
public class Utility{
/* IN this class NavModel,CModel,CNode are some dummy classes
* which help us read contents form some resources which are stored in dummy format
* as Example of tree format stored data below is the example
* tree
* child1
* child2
* child3
* child3-1
* child:q
* child:r
* child:a
* child3-2
*
* child4
*/
private static void populateChildLinks(NavModel navModel, Object objectNode, Links parent ){
try{
List<Links> childLinks = new ArrayList<Links>();
Iterator it = navModel.getChildren( objectNode );
while( it.hasNext() ){
NavNode node = (NavNode) it.next();
CNode contentNode = node.getContentNode();
Links links = new Links();
links.setNodeName( contentNode.getNodeName() );
childLinks.add( links );
if( navModel.hasChildren( node ) ){
populateChildLinks( node, links );
}
}
parent.setChildren( childLinks );
}
catch( Exception e ){
}
}
private static Links createCategoryLinks(String categoryLinkName){
Links categoryLinks = new Links();
categoryLinks.setNodeName( categoryLinkName );
return categoryLinks;
}
public static Links setupLinks(String categoryLinkName,String name) {
Links categoryLinks=null;
CModel contentModel = new CModel();
NavModel navModel = new NavModel();
categoryLinks = Utility.createCategoryLinks( categoryLinkName);
Object objectNode = contentModel.getLocator().findByUniqueName(name);
if( objectNode != null ){
if( navModel.hasChildren( objectNode ) ){
populateChildLinks( navModel,objectNode, categoryLinks );
}
}
}
// This is where i am facing issue once i get list of links of childs
// i have to delete how can i find that particular child in the list
// do i have to iterate through all the links and delete or which
// way is better
private static void filterLinks( Links parentNode,
List<Links> childNodeList ){
List<Links> filteredResourceList = new ArrayList<Links>();
if( childNodeList!=null ){
Iterator<Links> childNodeIt = childNodeList.iterator();
while( childNodeIt.hasNext() ){
Links childNode = (Links) childNodeIt.next();
if(childNode.getChildren().size() >0 ){
filterLinks( childNode, childNode.getChildren() );
}
boolean removeNode = filterContents( childNode);
if(! removeNode ){
filteredResourceList.add( childNode );
}
}
}
Iterator<Links> filteredResourceIt = filteredResourceList.iterator();
while( filteredResourceIt.hasNext() ){
Links childNode = (Links) filteredResourceIt.next();
parentNode.getChildren().remove( childNode );
}
}
// Let us consider this as some dummy method which returns true or false based on some conditions
private static boolean filterContents( menu.Links childNode ){
return false;
}
}
package menu;
public class TreeDisplay{
public static void main( String[] args ){
Links link = Utility.setupLinks( "SomeName", "ResiyrbceBane" );
Utility.filterLinks( link, link.getChildren() );
}
}
once i have a list of objects i have to delete since it is tree object how can i delete a child, i know i have to do a recursive search but how can i do that which way i will delete.In the above tree structure where i have commented in the code how can i delete child:q