问题描述
一个集合中无序存放着菜单树那种关系数据对象,现在要求把集合中的对象以菜单树层次结构有序存放
解决方案
解决方案二:
按照菜单树对象定点值排序为一个树即可,就是一个无序整数树结构排序
解决方案三:
packagecom.wanju.project001.zonghe.test;importjava.util.ArrayList;importjava.util.Collections;importjava.util.Comparator;importjava.util.HashSet;importjava.util.Iterator;importjava.util.List;importjava.util.Map.Entry;importjava.util.Set;/***排序124575个数按照先序生成树**@authorgchai**/publicclassTreeTest{Set<Integer>lst=newHashSet<Integer>();publicstaticvoidmain(String[]args){TreeTesttt=newTreeTest();tt.sort();}publicvoidsort(){lst.add(4);lst.add(5);lst.add(2);lst.add(7);lst.add(1);//先序遍历sort(lst);}publicvoidsort(Setset){List<Tree>trees=newArrayList<Tree>();for(Iteratoriterator=set.iterator();iterator.hasNext();){Objectobject=(Object)iterator.next();trees.add(newTree(String.valueOf(object)));}//排序前System.out.println(trees);//如果先序顺序从小到大12457,则(因为有多种组合,所以)结构如下//1//27//45trees=TreeBuildHelper.sortXu(trees);//排序后System.out.println(trees);TreexuTree=TreeBuildHelper.buildTree(trees);}}classTreeBuildHelper{publicstaticTreebuildTree(List<Tree>trees){Treetree=newTree();//for(inti=0;i<trees.size();i++){//}tree=buildRealTree(tree,null,0,trees);returntree;}/***好吧,此处递归我不会**@paramtree*@paramnode*@parami*@paramtrees*@return*/publicstaticvoidbuildRealTree(Treetree,Treenode,inti,List<Tree>trees){if(node==null){tree.setRootNode(trees.get(i));i++;buildRealTree(tree,tree.getRootNode(),i,trees);}if(null!=node&&node.getLeftLeaf()==null){node.setLeftLeaf(trees.get(i));i++;}elseif(null!=node&&node.getRightLeaf()==null){node.setRightLeaf(trees.get(i));i++;}else{buildRealTree(tree,node.getRightLeaf(),i,trees);}}/***直接排序,不必自己写了**@paramtrees*@return*/publicTreegetMinTree(List<Tree>trees){for(inti=0;i<trees.size()-1;i++){}returnnull;}publicTreegetTreeByRootNootName(Stringname,List<Tree>trees){for(inti=0;i<trees.size();i++){if(name.equals(trees.get(i).getRootName())){returntrees.get(i);}}returnnull;}publicstaticList<Tree>sortXu(List<Tree>trees){Collections.sort(trees,newComparator<Tree>(){@Overridepublicintcompare(Treeo1,Treeo2){returno1.getRootName().compareTo(o2.getRootName());}});returntrees;}}classTree{//叶子就是没有子节点的树privatebooleanisLeaf;privatebooleanhasLeaves;privateTreerootNode;privateStringrootName;privateTreeleftLeaf;privateTreerightLeaf;publicTreegetLeftLeaf(){returnleftLeaf;}publicvoidsetLeftLeaf(TreeleftLeaf){this.leftLeaf=leftLeaf;}publicTreegetRightLeaf(){returnrightLeaf;}publicvoidsetRightLeaf(TreerightLeaf){this.rightLeaf=rightLeaf;}publicbooleanisLeaf(){returnisLeaf;}publicvoidsetLeaf(booleanisLeaf){this.isLeaf=isLeaf;}publicbooleanisHasLeaves(){returnhasLeaves;}publicvoidsetHasLeaves(booleanhasLeaves){this.hasLeaves=hasLeaves;}publicTreegetRootNode(){returnrootNode;}publicvoidsetRootNode(TreerootNode){this.rootNode=rootNode;}publicStringgetRootName(){returnrootName;}publicvoidsetRootName(StringrootName){this.rootName=rootName;}publicTree(){}publicTree(StringrootName){this.rootName=rootName;}publicTree(TreerootNode){this.rootNode=rootNode;}privateTreegetLeaves(){returnnull;}@OverridepublicStringtoString(){return"Tree[isLeaf="+isLeaf+",hasLeaves="+hasLeaves+",rootNode="+rootNode+",rootName="+rootName+"]";}}
不知对不对,先写着
解决方案四:
你这个前提是一个简单的集合,放到实际项目中,每一层的节点都有字段来标识节点的序号,用来显示菜单的顺序。而且还要考虑父节点与子节点的关系,子节点集合中的顺序。
解决方案五:
比如根节点A下有A1,A2,A3这3个子节点,A1下面又有A11,A12这2个子节点,A11下面有A111节点,A2下面有A21节点。现在想以A1->A11->A111->A12->A2->A21->A3这种顺序放在集合中
解决方案六:
DFS-深度优先遍历可以参考这个算法
解决方案七:
很简单用一个hashmap和一个tree就搞定了