tree programming in java
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: tree programming in java

  1. #1
    Join Date
    Dec 2012
    Posts
    8

    tree programming in java

    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

    Code:
    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;
     
    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;
     
    public class TreeDisplay{
    	public static void main( String[] args ){
          Links link = Utility.setupLinks( "SomeName", "ResiyrbceBane");
           Utility.filterLinks( link, link.getChildren() );
    	}
    }
    *
    Is the utility class with so many static class is ok?

  2. #2
    Join Date
    Dec 2012
    Posts
    8

    Re: tree programming in java

    any suggestion appriciated

  3. #3
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: tree programming in java

    Did you consider using javax.swing.TreeModel instead if writing your own class.

    The only way to remove a child if you don't know it's location is to search the tree until you find it. Recursive searching can be a relatively easy way to implement this.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  4. #4
    Join Date
    Dec 2012
    Posts
    8

    Re: tree programming in java

    have written this function to do recursive search and delte is this fine

    Code:
    private static void delObj(Links parentNode,List<Links> childNodeList,List<Links> filteredResourceList){
    		Iterator<Links> filteredResourceIt = filteredResourceList.iterator();
    		while( filteredResourceIt.hasNext() ){
    			Links filteredLink = (Links) filteredResourceIt.next();
    			if(childNodeList.contains( filteredLink )){
    				parentNode.getChildren().remove( filteredLink );
    			}
    			for( Links links : childNodeList ){
    				delObj( links, links.getChildren(), filteredResourceList );
    			}
    		}
    	}

  5. #5
    Join Date
    Dec 2012
    Posts
    8

    Re: tree programming in java

    can it be optimized

  6. #6
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: tree programming in java

    have written this function to do recursive search and delte is this fine
    Does it work under all conditions? if so then it's fine.

    can it be optimized
    Probably, but does it need to be? If it works fast enough then don't bother trying to optimise it, otherwise get a profiler and work out which part is taking the most time and go from there.

    There are no guarantees when trying to optimise code but things that might help are: recursion is generally expensive so you could rewrite it without using recursion or minimize the recursion by aborting as soon the filter list is empty, not going recursive if there are no children etc. Don't do any changes until you taken some accurate timings of the current code and do 1 change at a time to see if it reduces the time taken. Remember trees can vary massively in their depth and width so you need to either get test timing on a large range of trees or make sure your test tree is representative of the real world tree your code will handle.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center