美文网首页
算法实现

算法实现

作者: hjm1fb | 来源:发表于2019-09-26 14:53 被阅读0次
  • 生成一个固定高度的二叉树,每个父节点有随机的0~2个子节点。
  1. 先定义树的类:
public class Tree<T> {

    public Node<T> root;
    public Tree(T rootData)

    {
        root = new Node<T>(rootData);
        root.data = rootData;
        root.children = new ArrayList<Node<T>>();
    }

    public static class Node<T>
    {
        public T data;
        public Node<T> parent;
        public List<Node<T>> children;

        public Node<T> appendChildrenNode(Node<T> node){
            node.parent = this;
            children.add(node);
            return node;
        }

        public Node(T nodeData)
        {
            data = nodeData;
            children = new ArrayList<Node<T>>();
        }
    }

    public Node<T> appendNode(Node<T> node){
        node.parent = root;
        root.children.add(node);
        return node;
    }

}
  1. 生成二叉树的方法:
   private Tree generateRandomTree(int targetDepth) {
        Random random = new Random();
        if (targetDepth <= 0){
            return null;
        }else {
            Tree<Integer> integerTree = new Tree<>(0);
            List<Tree.Node<Integer>> currentParentNodeList = Collections.singletonList(integerTree.root);
            List<Tree.Node<Integer>> currentChildrenNodeList = new ArrayList<>();
            for (int currentDepth = 1; currentDepth< targetDepth; currentDepth++ ) {
                for (int m = 0; m < currentParentNodeList.size(); m++) {
                    Tree.Node<Integer> parentNode = currentParentNodeList.get(m);
                    if (m == currentParentNodeList.size() - 1 && currentChildrenNodeList.size() == 0) {
                        generateChidNode(random.nextInt(2) + 1, parentNode);
                        currentChildrenNodeList.addAll(parentNode.children);
                    }else {
                        generateChidNode(random.nextInt(3), parentNode);
                        currentChildrenNodeList.addAll(parentNode.children);
                    }
                }
                currentParentNodeList = currentChildrenNodeList;
                currentChildrenNodeList = new ArrayList<>();
            }
            return integerTree;
        }
    }

    private void generateChidNode(int number,Tree.Node<Integer> node){
        for (int k = 0; k < number; k++){
            node.appendChildrenNode(new Tree.Node<Integer>(node.data*10 + k + 1));
        }
    }

    public void showTree(View view) {
           if (generatedTree == null){
               Toast.makeText(this,"Please Generate Tree first",Toast.LENGTH_SHORT).show();
               return;
           }else {
               printNode(Collections.singletonList(generatedTree.root),new StringBuffer(".\n"));
           }
    }

    private void printNode(List<Tree.Node<Integer>> nodeList, StringBuffer stringBuffer) {
        List<Tree.Node<Integer>> nextNodeList = new ArrayList<>();
        for (Tree.Node<Integer> node : nodeList) {
            stringBuffer.append(node.data + "  ");
            nextNodeList.addAll(node.children);
        }
        stringBuffer.append("\n");
        if (nextNodeList.isEmpty()){
            Log.d(TAG, stringBuffer.toString());
        }else {
            printNode(nextNodeList,stringBuffer);
        }
    }

相关文章

网友评论

      本文标题:算法实现

      本文链接:https://www.haomeiwen.com/subject/etalyctx.html