package xyrj.tree.utils;

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

/**
* @description
* 
* @classname PrintTree 
* @author maoyun0903<maoyun19900818@foxmail.com>
* @date 2012-12-16 下午02:14:15 
* @version 1.0
*/
public class PrintTree {

/**
* @field    整个tree模块中所有的记录
*/
private static List<Map<String,String>> _totalList;

/**
* @field    生成的树形结构集合
*/
private static List<Map<String,String>> treeList;

/**
* @field    深度
*/
private static int _level = 0;

/**
* @field    树形前缀符
*/
private static String _prefix="|";

/**
* @field    树形修饰符
*/
private static String _fix="-";

/**
* 构造方法(传所有记录数组集合)
*
* @description 
* 
* @param totalList
*/
public PrintTree(List<Map<String,String>> totalList)
{
_totalList    =    totalList;
treeList =    new ArrayList<Map<String,String>>();
System.out.println("构造方法");
}



/** 
* 打印树形结构
* @description
* @title main
* @param @param args
* @return void
* @throws 
*/
public static void main(String[] args) {

List<Map<String,String>> listMap = new ArrayList<Map<String,String>>();
Map map01    =    new HashMap<String,String>();
map01.put("tree_id", "1");
map01.put("tree_name", "湖南省");
map01.put("parent_id", "-1");

Map map02    =    new HashMap<String,String>();
map02.put("tree_id", "2");
map02.put("tree_name", "湖北省");
map02.put("parent_id", "-1");

Map map03    =    new HashMap<String,String>();
map03.put("tree_id", "3");
map03.put("tree_name", "常德市");
map03.put("parent_id", "1");

Map map04    =    new HashMap<String,String>();
map04.put("tree_id", "4");
map04.put("tree_name", "长沙市");
map04.put("parent_id", "1");

Map map05    =    new HashMap<String,String>();
map05.put("tree_id", "5");
map05.put("tree_name", "荆州市");
map05.put("parent_id", "2");

listMap.add(map01);
listMap.add(map02);
listMap.add(map03);
listMap.add(map04);
listMap.add(map05);

PrintTree    printTree    =    new PrintTree(listMap);
String str    =    printTree.genTree();
System.out.println(str.toString());

}

/**
* 打印最后树形结构
* 
* @description
* 
* @title genTree
* @param @return
* @return String
* @throws
*/
public static String genTree()
{
String father_id    =    "-1";
//System.out.println("genTree"+_totalList.size());
_genDiGuiList(father_id , 0);
StringBuffer    printTree    =    new StringBuffer();
//System.out.println("treeList"+treeList.size());
for(int i=0 ; i < treeList.size() ; i++)
{
printTree.append("<option name=\""+treeList.get(i).get("tree_id")+"\">"+
treeList.get(i).get("tree_name")+
"</option>");
}
return printTree.toString();
}

/**
* 递归遍历整个List集合
* 
* @description
* 
* @title _genDiGuiList
* @param @return    经过处理后的Map集合数组
* @return String
* @throws
*/
private static List<Map<String,String>> _genDiGuiList(String father_name,int count)
{
if(null != _totalList && _totalList.size() > 0)
{
for(int i = 0 ; i < _totalList.size() ; i++ )
{
_level=count;
String str    =    "";
String allStr    =    "";
//System.out.println(_totalList.get(i).get("tree_name"));
if((_totalList.get(i).get("parent_id")).equals(father_name))
{
//System.out.println(_level);
if(_level>0)
{
for(int j=0;j<_level;j++)
{
str    += _fix ;
}
allStr    =    _prefix + str ;
}
_level++;
//把totalList[i]取出赋值tree_name
String field_id    =    _totalList.get(i).get("tree_id");
String field_name    =    _totalList.get(i).get("tree_name");
//System.out.println(field_id+"...."+field_name);
Map<String,String> map    =    new HashMap<String,String>();
map.put("tree_id", field_id);
map.put("tree_name", allStr+field_name);
treeList.add(map);
//System.out.println(field_id);
_genDiGuiList(field_id,_level);
}else
{
continue;
}
}
}
return treeList;
}
}

 

写博客不仅是一种习惯,更是一种沉淀。