67,538
社区成员
发帖
与我相关
我的任务
分享package com.wanju.project001.zonghe.test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
/**
* 排序12457 5个数按照先序生成树
*
* @author gchai
*
*/
public class TreeTest {
Set<Integer> lst = new HashSet<Integer>();
public static void main(String[] args) {
TreeTest tt = new TreeTest();
tt.sort();
}
public void sort() {
lst.add(4);
lst.add(5);
lst.add(2);
lst.add(7);
lst.add(1);
// 先序遍历
sort(lst);
}
public void sort(Set set) {
List<Tree> trees = new ArrayList<Tree>();
for (Iterator iterator = set.iterator(); iterator.hasNext();) {
Object object = (Object) iterator.next();
trees.add(new Tree(String.valueOf(object)));
}
// 排序前
System.out.println(trees);
// 如果先序顺序从小到大 12457,则(因为有多种组合,所以)结构如下
// 1
// 2 7
// 4 5
trees = TreeBuildHelper.sortXu(trees);
// 排序后
System.out.println(trees);
Tree xuTree = TreeBuildHelper.buildTree(trees);
}
}
class TreeBuildHelper {
public static Tree buildTree(List<Tree> trees) {
Tree tree = new Tree();
// for (int i = 0; i < trees.size(); i++) {
// }
tree = buildRealTree(tree, null, 0, trees);
return tree;
}
/**
* 好吧,此处递归我不会
*
* @param tree
* @param node
* @param i
* @param trees
* @return
*/
public static void buildRealTree(Tree tree, Tree node, int i,
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++;
} else if (null != node && node.getRightLeaf() == null) {
node.setRightLeaf(trees.get(i));
i++;
} else {
buildRealTree(tree, node.getRightLeaf(), i, trees);
}
}
/**
* 直接排序,不必自己写了
*
* @param trees
* @return
*/
public Tree getMinTree(List<Tree> trees) {
for (int i = 0; i < trees.size() - 1; i++) {
}
return null;
}
public Tree getTreeByRootNootName(String name, List<Tree> trees) {
for (int i = 0; i < trees.size(); i++) {
if (name.equals(trees.get(i).getRootName())) {
return trees.get(i);
}
}
return null;
}
public static List<Tree> sortXu(List<Tree> trees) {
Collections.sort(trees, new Comparator<Tree>() {
@Override
public int compare(Tree o1, Tree o2) {
return o1.getRootName().compareTo(o2.getRootName());
}
});
return trees;
}
}
class Tree {
// 叶子就是没有子节点的树
private boolean isLeaf;
private boolean hasLeaves;
private Tree rootNode;
private String rootName;
private Tree leftLeaf;
private Tree rightLeaf;
public Tree getLeftLeaf() {
return leftLeaf;
}
public void setLeftLeaf(Tree leftLeaf) {
this.leftLeaf = leftLeaf;
}
public Tree getRightLeaf() {
return rightLeaf;
}
public void setRightLeaf(Tree rightLeaf) {
this.rightLeaf = rightLeaf;
}
public boolean isLeaf() {
return isLeaf;
}
public void setLeaf(boolean isLeaf) {
this.isLeaf = isLeaf;
}
public boolean isHasLeaves() {
return hasLeaves;
}
public void setHasLeaves(boolean hasLeaves) {
this.hasLeaves = hasLeaves;
}
public Tree getRootNode() {
return rootNode;
}
public void setRootNode(Tree rootNode) {
this.rootNode = rootNode;
}
public String getRootName() {
return rootName;
}
public void setRootName(String rootName) {
this.rootName = rootName;
}
public Tree() {
}
public Tree(String rootName) {
this.rootName = rootName;
}
public Tree(Tree rootNode) {
this.rootNode = rootNode;
}
private Tree getLeaves() {
return null;
}
@Override
public String toString() {
return "Tree [isLeaf=" + isLeaf + ", hasLeaves=" + hasLeaves
+ ", rootNode=" + rootNode + ", rootName=" + rootName + "]";
}
}
不知对不对,先写着