Am trying to write a method that fetches records from DB and prepares a tree. I have uniqueID and parentID fields in records. Am trying to find a parent from the already built tree to insert new records using recursion. But my tree only builds upto level 2. After that it fails to return proper parent nodes. Below is my code
//Code calling the recursive code :
Java Code:
private Node<TreeDataModel> findParent(TreeDataModel treeDataModel, String domainID,
			String userID) {
		// TODO Auto-generated method stub
		LOGGER.info("Inside findParent :: "+userID+" domain ID :: "+domainID+" Parent ID :: "+treeDataModel.getParentID_());
		List<Tree<TreeDataModel>> baseList = domainTreeMap.get(domainID);
		if(null != baseList) {
			Iterator<Tree<TreeDataModel>> itr = baseList.iterator();
			while(itr.hasNext()) {
				Tree<TreeDataModel> tempTree = itr.next();
				if(tempTree.getRootElement().getData().getUserID_().trim().equalsIgnoreCase(treeDataModel.getParentID_().trim())) {
					return tempTree.getRootElement();
				}else {
					return findParentNode(tempTree.getRootElement(), tempTree.getRootElement().getChildren(), treeDataModel);
				}
			}
		}else {
			LOGGER.info("BaseList is NULL");
		}
		return null;
	}


//Code where recursion is used:

private Node<TreeDataModel> findParentNode(Node<TreeDataModel> rootNode, List<Node<TreeDataModel>> childList,
			TreeDataModel treeDataModel) {
		// TODO Auto-generated method stub
		LOGGER.info("Entering findParentNode :: "+rootNode.getData().getUserID_()+" Children lIst :: "+childList+" Model :: "+treeDataModel.getUserID_());
		if(!childList.isEmpty()) {
			Iterator<Node<TreeDataModel>> itr = childList.iterator();
			Node<TreeDataModel> tempNode = null;
			while(itr.hasNext()) {
				tempNode = itr.next();
				LOGGER.info("Temp Node :: "+tempNode.getData().getUserID_()+" Child Count :: "+tempNode.getNumberOfChildren()+" New Node parent ID :: "+treeDataModel.getParentID_());
				if(tempNode.getData().getUserID_().trim().equalsIgnoreCase(treeDataModel.getParentID_().trim())) {
					LOGGER.info("Inside if :: "+tempNode.getData().getUserID_());
					return tempNode;
				}
				LOGGER.info("Calling findParentNode");
				return findParentNode(tempNode, tempNode.getChildren(), treeDataModel);
			}
		}
		return null;
	}
In recursive method i have to return the previous parent node to continue iteration of while loop. But am not able to figure out what i should return. Please help asap.