zprill 2009-03-28 22:32
浏览 180
已采纳

我想问一个递归的问题,应该是树的递归吧

id paredntid
1 0
2 1
3 1
4 2
5 3

这样一组数据,假设已经放在list里了,如何封装成{1,{2{4}},{3{5}}}.
哪位兄弟指点迷津下,谢谢

  • 写回答

1条回答 默认 最新

  • rednaxelafx 2009-03-29 02:46
    关注

    遍历树的过程通常可以用递归来实现,但是遍历一个list来构造一棵树纯粹是线性的遍历而已……

    简单实现了一个版本:
    [code="java"]import java.util.*;

    class TreeNode {
    // don't need a reference to parent for an ordinary tree
    //private TreeNode parent;

    // a list of children TreeNodes, implementing an N-ary tree
    private List<TreeNode> children;
    
    // use this field to carry values on all nodes
    private E value;
    
    public TreeNode() {
        this(null);
    }
    
    public TreeNode(E value) {
        this.value = value;
        this.children = new ArrayList<TreeNode>();
    }
    
    public TreeNode(E value, Iterable<TreeNode> children) {
        this(value);
        // add all children
        // can't use List<E>.addAll() because Iterable<E> is
        // a base interface of Collection<E>
        for (TreeNode child : children) {
            addChild(child);
        }
    }
    
    public E getValue() {
        return this.value;
    }
    
    public void setValue(E value) {
        this.value = value;
    }
    
    public Iterable<TreeNode> getChildren() {
        return this.children;
    }
    
    public void addChild(TreeNode child) {
        // skip duplicate children
        if (!this.children.contains(child)) {
            this.children.add(child);
        }
    }
    
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        toStringCore(sb);
        return sb.toString();
    }
    
    // builds the string recursively
    protected void toStringCore(StringBuilder sb) {
        sb.append("(");
        sb.append(value);
        for (TreeNode child : this.children) {
            sb.append(" ");
            child.toStringCore(sb);
        }
        sb.append(")");
    }
    

    }

    public class TestTree {
    private static Map getSampleNodeMapping() {
    return new HashMap() {{
    put(1, 0);
    put(2, 1);
    put(3, 1);
    put(4, 2);
    put(5, 3);
    }};
    }

    private static TreeNode<Integer> buildTreeFromMapping(
        Map<Integer, Integer> mapping) {
    
        // make a map from id to TreeNode instance
        Map<Integer, TreeNode<Integer>> nodes
            = new HashMap<Integer, TreeNode<Integer>>();
        TreeNode<Integer> root = null;
        for (Map.Entry<Integer, Integer> entry : mapping.entrySet()) {
            Integer key = entry.getKey();
            Integer value = entry.getValue();
            TreeNode<Integer> child = null;
            if (!nodes.containsKey(key)) {
                child = new TreeNode<Integer>(key);
                nodes.put(key, child);
            } else {
                child = nodes.get(key);
            }
            if (0 == value) {
                root = child;
            } else {
                TreeNode<Integer> parent = null;
                if (!nodes.containsKey(value)) {
                    parent = new TreeNode<Integer>(value);
                    nodes.put(key, parent);
                } else {
                    parent = nodes.get(value);
                }
                parent.addChild(child);
            }
        }
    
        return root;
    }
    
    public static void main(String[] args) {
        Map<Integer, Integer> mapping = getSampleNodeMapping();
        TreeNode<Integer> root = buildTreeFromMapping(mapping);
        System.out.println(root); // (1 (2 (4)) (3 (5)))
    }
    

    }[/code]
    这段代码遍历一个id到parent_id的映射表并创建出对应的树,这个遍历过程是线性的;在得到树的字符串表示(toString)时遍历了树,过程是递归的。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题