Java基础:构建树结构

将括号结构转换为树结构

一、括号文本案例

( test
   ( test01
      ( test02 test021 )
      ( test03 test031 )
      
    )
    ( test04
         ( test05
            ( test06 test061 )
            ( test07 test071 )
            ( test08
               ( test09 test091 )
               		 ( test10 test101 )
                     ( test12 test121 )
               ( test13 test131 )
            )
            ( test14 test141 )
         )
     )
 )

**上述,test只作为名称,任意起的,重要的是表达这种结构;

二、创建树模型

package cn.com.xia.model;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xia
 *
 */
public class TreeNode {

	// 叶子节点所必有得属性
	private String key;
	private String value;

	// 非叶子节点所具有的属性
	private Map<String, ArrayList<TreeNode>> childMap = new LinkedHashMap<>();

	// 非根节点共有属性
	private TreeNode parent;

	private String nodeName;

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public Map<String, ArrayList<TreeNode>> getMap() {
		return childMap;
	}

	public void setMap(Map<String, ArrayList<TreeNode>> map) {
		this.childMap = map;
	}

	public TreeNode getParent() {
		return parent;
	}

	public void setParent(TreeNode parent) {
		this.parent = parent;
	}

	public String getNodeName() {
		return nodeName;
	}

	public void setNodeName(String nodeName) {
		this.nodeName = nodeName;
	}

	public TreeNode(String nodeName) {
		super();
		this.nodeName = nodeName;
	}
}

三、將()结构的文本转换为树结构

private static List<TreeNode> creatTreeFromContent(String panelContent) {
		List<TreeNode> uis = new ArrayList<>();
		String[] lines = panelContent.split("\n");
		 // 左括号结构
		Pattern leftBrackets = Pattern.compile("[\\s|\\t]*\\(\\s*([^\\s]+)\\s*");
		// 右括号结构
		Pattern rightBrackets = Pattern.compile("\\s*\\)\\s*"); 
		// 配对括号
		Pattern pairBrackets = Pattern.compile(" [\\s|\\t]*\\(\\s*([^\\s]+)\\s*([^\\)]+|\\\".*?\\\")\\s*\\)\\s*");
		// 右侧值可能为字符串结构,需要两端去括号
		TreeNode currentNode = null;
		for (String line : lines) {
			Matcher matcher = leftBrackets.matcher(line);
			if (matcher.matches()) {
				String key = matcher.group(1);
				if (currentNode == null) {
					TreeNode node = newTreeNode(key);
					uis.add(node);
					currentNode = node;
				} else {
					TreeNode chileNode = new TreeNode(key);
					chileNode.setParent(currentNode);
					if (currentNode.getMap().containsKey(key)) {
						currentNode.getMap().get(key).add(chileNode);
					} else {
						ArrayList<TreeNode> nodes = new ArrayList<>();
						nodes.add(chileNode);
						currentNode.getMap().put(key, nodes);
					}
					currentNode = chileNode;
				}
				continue;
			}
			matcher = rightBrackets.matcher(line);
			if (matcher.matches() && currentNode != null) {
				currentNode = currentNode.getParent();
				continue;
			}
			matcher = pairBrackets.matcher(line);
			if (matcher.matches()) {
				String key = matcher.group(1);
				String value = matcher.group(2);
				TreeNode chileNode = new TreeNode(key);
				chileNode.setKey(key);
				chileNode.setValue(value);
				chileNode.setParent(currentNode);
				if (currentNode != null && currentNode.getMap().containsKey(key)) {
					currentNode.getMap().get(key).add(chileNode);
				} else {
					ArrayList<TreeNode> nodes = new ArrayList<>();
					nodes.add(chileNode);
					if (currentNode.getMap() != null) {
						currentNode.getMap().put(key, nodes);
					}
				}
			}
		}
		return uis;
	}

四、获取树节点
例如:获取test01节点及它的树结构

//获取树
List<TreeNode> nodes = creatUITreeFromContent(“括号文本”);
for (TreeNode treeNode : nodes) {
		TreeNode attributes = treeNode.getTreeNodeFromPath("test01".split("/"));
	}
public TreeNode getTreeNodeFromPath(String[] names) {
		TreeNode node = null;
		if (names.length > 1) {
			ArrayList<TreeNode> entrys = this.getMap().get(names[0]);
			if (entrys != null && !entrys.isEmpty()) {
				List<String> temps = new ArrayList<>(Arrays.asList(names));
				temps.remove(0);
				node = entrys.get(0).getTreeNodeFromPath(temps.toArray(new String[temps.size()]));
			}
		} else if (names.length == 1) {
			ArrayList<TreeNode> entrys = this.getMap().get(names[0]);
			if (entrys != null && !entrys.isEmpty()) {
				return entrys.get(0);
			} else {
				return null;
			}
		} else {
			return null;
		}
		return node;
	}

**之所以不写xml转换成树结构,是因为网上有很多现成的,并且xml有自己直接读取的方法,一般都不用转成树形结构;而这个括号结构只是本人自己在工作中正巧碰到了,所以就写出来了,构建方法基本和xml构建树一致。

以上内容仅供学习参考

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值