背景介绍:数据表之间有外键约束,用一个多叉树模型来抽象。
数据结构:多叉树
有外键约束表删除顺序:按层级,叶子层先删,自下往上按层删除。
将来改造的地方:解决层与层之间有约束关系表之间的删除
代码如下:
/** * 树节点 * @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.Deque; 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 boolean isRoot = false; public ManyTree(){ } /** * 将节点插入到多叉树中 * @param _node 插入的节点 * @param _pnode 父节点 */ public void add(TreeNode _node,TreeNode _pnode){ if(null == _pnode){ isRoot = true ; node = _node ; }else { ManyTree manyTree = findParentManyTree(_pnode); ManyTree tmpManyTree = new ManyTree() ; tmpManyTree.setNode(_node); tmpManyTree.setRoot(false); manyTree.getChildNodes().add(tmpManyTree); } } /** * 获取多叉树的叶子结点到参数结点的反转顺序集合 * @param manyTree * @return */ public List<TreeNode> getReverseSequence(){ List<TreeNode> lstTreeNode = new ArrayList<TreeNode>(20); if( this.getChildNodes().size() == 0 ) { lstTreeNode.add(this.getNode()); return lstTreeNode; } Stack<TreeNode> stack = new Stack<TreeNode>(); Queue<ManyTree> queue = new LinkedList<ManyTree>() ; queue.add(this); 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(this.getChildNodes(),_pnode); return manyTree ; } // 递归查找 private ManyTree findManyTree(List<ManyTree> _lstManyTree,TreeNode _pnode){ Iterator<ManyTree> iter = _lstManyTree.iterator() ; ManyTree manyTree = null ; while(iter.hasNext()){ manyTree = iter.next() ; if(_pnode.hashCode() == manyTree.getNode().hashCode()){ break ; // return manyTree ; }else { findManyTree(manyTree.getChildNodes(),_pnode); } } return manyTree ; } 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(); } public boolean isRoot() { return isRoot; } public void setRoot(boolean isRoot) { this.isRoot = isRoot; } }
测试用例:
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(node6, node5); List<TreeNode> lstTreeNode = manyTree.getReverseSequence(); for(Iterator<TreeNode> iter = lstTreeNode.iterator() ;iter.hasNext();){ System.out.println(iter.next().getBizID()); } System.out.println("constructor complete!"); } }
如果代码中有改进的地方,请给我留言!