自己写的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
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。