树结构

package com;

import java.util.ArrayList;
import java.util.List;

public class Tree {
    //封装节点
    public List<Node> packagingNode()
    {
        List<Node> listNode = new ArrayList<Node>();
        
        Node a = new Node();
        Node b = new Node();
        Node c = new Node();
        Node d = new Node();
        Node e = new Node();
        Node f = new Node();
        Node j = new Node();
        Node h = new Node();
        Node i = new Node();
        Node g = new Node();
        Node k = new Node();
        Node l = new Node();
        Node m = new Node();
        Node n = new Node();
        
        e.info = "e";
        e.pather = null;
        e.childList.add(j);
        e.childList.add(d);
                
        j.info = "j";
        j.pather = e;
        j.childList.add(i);
        j.childList.add(n);
        j.childList.add(c);
        
        d.info = "d";
        d.pather = e;
        d.childList.add(h);
            
        i.info = "i";
        i.pather = j;
        i.childList.add(f);
    
        n.info = "n";
        n.pather = j;
        n.childList.add(a);
        n.childList.add(b);
    
        c.info = "c";
        c.pather = j;
        
        h.info = "h";
        h.pather = d;
        h.childList.add(m);
        h.childList.add(l);
        h.childList.add(g);
        
        f.info = "f";
        f.pather = i;
        
        a.info = "a";
        a.pather = n;
        a.childList.add(k);
        
        b.info = "b";
        b.pather = n;
        
        m.info = "m";
        m.pather = h;
        
        l.info = "l";
        l.pather = h;
        
        g.info = "g";
        g.pather = h;
        
        k.info = "k";
        k.pather = a;
        
        listNode.add(a);
        listNode.add(b);
        listNode.add(c);
        listNode.add(d);
        listNode.add(e);
        listNode.add(f);
        listNode.add(g);
        listNode.add(h);
        listNode.add(i);
        listNode.add(j);
        listNode.add(k);
        listNode.add(l);
        listNode.add(m);
        listNode.add(n);
        
        return listNode;
    }
    /**
     * 查找树
     * @param args
     */
    public Node lookNode(String arm){
        List<Node> listNode = this.packagingNode();
        Node nd = null;
        for (Node node : listNode) {
            if(node.info.equals(arm)){
                nd = node;
            }
        }
        return nd;
    }
    public static void main(String[] args) {
        Tree tree = new Tree();
        Node node = tree.lookNode("a");
        System.out.println(node.info);
    }
}

//节点类
class Node{
    public String info;
    public Node pather;
    public List<Node> childList = new ArrayList<Node>();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值