自己写的Java 数据结构Tree

发现Java没有Tree,自己简单写了一个。

可通过new创建一个Tree,然后获取root Node节点。每个Node节点有自己的Data,并可以获取当前Node的level,parent Node和child Node list。可以在Node上添加,查找和删除child Node。


具体代码如下:


Tree.java

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

/**
 * Project:      JavaTest
 * FileName:     Tree.java
 * @Description: TODO
 * @author:      ligh4
 * @version      V1.0 
 * Createdate:   2015年3月23日 下午3:12:50
 * Copyright:    Copyright(C) 2014-2015
 * Company       Lenovo LTD.
 * All rights Reserved, Designed By Lenovo CIC.
 */

/**
 * 类 Tree 的实现描述:TODO 类实现描述
 * 
 * @author ligh4 2015年3月23日下午3:12:50
 */
public class Tree<T extends Object> {

    class Node {
        private T          data;
        private List<Node> childs;
        private Node       parent;
        private int        level;

        private Node(T data, Node parent) {
            this.data = data;
            this.parent = parent;
            if (parent == null) {
                level = 0;
            } else {
                level = parent.level + 1;
            }
        }

        public List<Node> getchilds() {
            return childs;
        }

        public void addChild(T data) {
            if (childs == null) {
                childs = new ArrayList<Node>();
            }
            childs.add(new Node(data, this));
        }

        public void deleteChild(Node node) {
            if (childs != null) {
                Iterator<Node> it = childs.iterator();
                while (it.hasNext()) {
                    Node child = it.next();
                    if (child.equals(node)) {
                        childs.remove(child);
                        break;
                    }
                }
            }
        }

        public Node getChild(T data) {
            if (childs != null) {
                Iterator<Node> it = childs.iterator();
                while (it.hasNext()) {
                    Node child = it.next();
                    if (child.data.equals(data)) {
                        return child;
                    }
                }
            }
            return null;
        }

        public Node getParent() {
            return parent;
        }

        public void setData(T data) {
            this.data = data;
        }

        public T getData() {
            return data;
        }

        public int getLevel() {
            return level;
        }
    }

    private Node root = new Node(null, null);

    public Node getRoot() {
        return root;
    }
}

测试:

import java.util.List;

/**
 * Project:      JavaTest
 * FileName:     TestTree.java
 * @Description: TODO
 * @author:      ligh4
 * @version      V1.0 
 * Createdate:   2015年3月23日 下午3:05:10
 * Copyright:    Copyright(C) 2014-2015
 * Company       Lenovo LTD.
 * All rights Reserved, Designed By Lenovo CIC.
 */

/**
 * 类 TestTree 的实现描述:TODO 类实现描述
 * 
 * @author ligh4 2015年3月23日下午3:05:10
 */
public class TestTree {

    public static void main(String[] args) {

        Tree<Integer> idTree = new Tree<Integer>();
        Tree.Node root = idTree.getRoot();

        root.setData(1);
        root.addChild(2);
        root.addChild(3);
        Tree.Node child2 = root.getChild(2);
        child2.addChild(4);

        //中序
        System.out.println("中序:");
        printTree(root);

        //先序
        System.out.println("先序");
        printTree2(root);
    }

    public static void printTree(Tree.Node node) {
        List<Tree.Node> childs = node.getchilds();
        Tree.Node parent = node.getParent();

        String msg = String.format("level:%d node:%s  parent:%s childs:%d", node.getLevel(), node
                .getData().toString(), parent == null ? "null" : parent.getData().toString(),
                childs == null ? 0 : childs.size());
        System.out.println(msg);

        if (childs != null) {
            for (Tree.Node n : childs) {
                printTree(n);
            }
        }
    }

    public static void printTree2(Tree.Node node) {
        List<Tree.Node> childs = node.getchilds();
        Tree.Node parent = node.getParent();

        if (parent == null) {
            String msg = String.format("level:%d node:%s  parent:%s childs:%d", node.getLevel(),
                    node.getData().toString(), parent == null ? "null" : parent.getData()
                            .toString(), childs == null ? 0 : childs.size());
            System.out.println(msg);
        }

        if (childs != null) {

            for (Tree.Node n : childs) {

                List<Tree.Node> childs2 = n.getchilds();
                Tree.Node parent2 = n.getParent();
                String msg = String.format("level:%d node:%s  parent:%s childs:%d", n.getLevel(), n
                        .getData().toString(), parent2 == null ? "null" : parent2.getData()
                        .toString(), childs2 == null ? 0 : childs2.size());
                System.out.println(msg);
            }

            for (Tree.Node n : childs) {
                printTree2(n);
            }
        }
    }
}

测试结果:

中序:
level:0 node:1  parent:null childs:2
level:1 node:2  parent:1 childs:1
level:2 node:4  parent:2 childs:0
level:1 node:3  parent:1 childs:0
先序
level:0 node:1  parent:null childs:2
level:1 node:2  parent:1 childs:1
level:1 node:3  parent:1 childs:0
level:2 node:4  parent:2 childs:0


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值