第二版主要解决的问题有:
1、不用指定根节点
2、根节点不能多于一个
直接上代码:
package com.archive.cpr; /** * 树节点 * @author xingxing_li * */ public class TreeNode { private String bizID ; private String refname ; public String getBizID() { return bizID; } public void setBizID(String bizID) { this.bizID = bizID; } public String getRefname() { return refname; } public void setRefname(String refname) { this.refname = refname; } @Override public int hashCode() { StringBuffer sb = new StringBuffer(100); sb.append(bizID).append(refname); return sb.toString().hashCode(); } @Override public boolean equals(Object obj) { if(obj instanceof TreeNode){ TreeNode tmpNode = (TreeNode)obj ; if(null != tmpNode){ if(this.bizID.equals(tmpNode.getBizID()) && this.refname.equals(tmpNode.getRefname())) return true ; } return false; } return super.equals(obj); } }
package com.archive.cpr; import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Queue; import java.util.Stack; /** * 多叉树结构 * @author xingxing_li * */ public class ManyTree { private TreeNode node ; private List<ManyTree> childNodes = new ArrayList<ManyTree>(); private static ManyTree root = new ManyTree(); // 虚拟一个ROOT节点 //private boolean isRoot = false; public ManyTree(){ } /** * 将节点插入到多叉树中 * @param _node 插入的节点 * @param _pnode 父节点 */ public void add(TreeNode _node,TreeNode _pnode){ if(null == _pnode){ // 如果父节点为NULL,直接添加至ROOT节点下 ManyTree tmpManyTree = new ManyTree() ; tmpManyTree.setNode(_node); root.getChildNodes().add(tmpManyTree); }else { // 查找pnode的父节点 ManyTree manyTree = findParentManyTree(_pnode); // 如果父节点为NULL,直接添加至ROOT节点下 if(null == manyTree) { ManyTree tmpManyTree = new ManyTree() ; tmpManyTree.setNode(_pnode); root.getChildNodes().add(tmpManyTree); /* ManyTree tmpManyTree1 = new ManyTree() ; tmpManyTree1.setNode(_node); tmpManyTree.getChildNodes().add(tmpManyTree1); */ add(_node,_pnode); }else { // 如果查找到了父节点,则将node节点添加至查找到的父节点中 ManyTree tmpManyTree = new ManyTree() ; tmpManyTree.setNode(_node); manyTree.getChildNodes().add(tmpManyTree); } } } /** * 获取多叉树的叶子结点到参数结点的反转顺序集合 * @param manyTree * @return */ public List<TreeNode> getReverseSequence(){ List<TreeNode> lstTreeNode = new ArrayList<TreeNode>(20); if( root.getChildNodes().size() == 0 ) { lstTreeNode.add(this.getNode()); return lstTreeNode; } Stack<TreeNode> stack = new Stack<TreeNode>(); Queue<ManyTree> queue = new LinkedList<ManyTree>() ; queue.addAll(root.getChildNodes()); ManyTree tmpTreeNode = null ; List<ManyTree> tmpChildNodes = null ; while(!queue.isEmpty()){ tmpTreeNode = queue.remove() ; stack.add(tmpTreeNode.getNode()); tmpChildNodes = tmpTreeNode.getChildNodes() ; if(tmpChildNodes.size()>0){ queue.addAll(tmpChildNodes); } } TreeNode node = null ; while(!stack.isEmpty()){ node = stack.pop() ; if(!lstTreeNode.contains(node)) lstTreeNode.add(node); } return lstTreeNode ; } // 查找pnode所在的ManyTree节点上 private ManyTree findParentManyTree(TreeNode _pnode){ if(node == _pnode){ return this ; } ManyTree manyTree = findManyTree(root.getChildNodes(),_pnode); return manyTree ; } // 递归查找 private ManyTree findManyTree(List<ManyTree> _lstManyTree,TreeNode _pnode){ Iterator<ManyTree> iter = _lstManyTree.iterator() ; ManyTree manyTree = null ; ManyTree rtnManyTree = null; while(iter.hasNext()){ manyTree = iter.next() ; if(_pnode.hashCode() == manyTree.getNode().hashCode()){ rtnManyTree = manyTree ; return rtnManyTree; }else { // 由于要深层的递归,返回值的写法有点特殊,将该次调用的值与上一层调用的值并起来 ManyTree rtn = findManyTree(manyTree.getChildNodes(),_pnode); rtnManyTree = (null!=rtn)?rtn:rtnManyTree ; } } return rtnManyTree ; } public TreeNode getNode() { return node; } public void setNode(TreeNode node) { this.node = node; } public List<ManyTree> getChildNodes() { return childNodes; } public void setChildNodes(List<ManyTree> childNodes) { this.childNodes = childNodes; } @Override public int hashCode() { return this.node.hashCode(); } }
调用端的例子: package com.archive.cpr; import java.util.Iterator; import java.util.List; public class TestManyTree { public static void main(String[] args) { TreeNode node1 = new TreeNode() ; node1.setBizID("1_STOCK_BASE"); node1.setRefname("BIZ_ID"); ManyTree manyTree = new ManyTree(); manyTree.add(node1, null); TreeNode node2 = new TreeNode() ; node2.setBizID("2_ACCOUNT"); node2.setRefname("BIZ_ID"); TreeNode node3 = new TreeNode() ; node3.setBizID("3_CHARACTER"); node3.setRefname("BIZ_ID"); TreeNode node4 = new TreeNode() ; node4.setBizID("4_DETAIL"); node4.setRefname("BIZ_ID"); TreeNode node5 = new TreeNode() ; node5.setBizID("5_DETAIL"); node5.setRefname("BIZ_ID"); TreeNode node6 = new TreeNode() ; node6.setBizID("6_DETAIL"); node6.setRefname("BIZ_ID"); manyTree.add(node2, node1); manyTree.add(node3, node1); manyTree.add(node4, node1); manyTree.add(node5, node3); manyTree.add(node6, node4); manyTree.add(node5, node6); List<TreeNode> lstTreeNode = manyTree.getReverseSequence(); for(Iterator<TreeNode> iter = lstTreeNode.iterator() ;iter.hasNext();){ System.out.println(iter.next().getBizID()); } System.out.println("constructor complete!"); } }