首页 / JAVA / 自己写的Java 数据结构Tree
自己写的Java 数据结构Tree
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了自己写的Java 数据结构Tree,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4017字,纯文字阅读大概需要6分钟。
内容图文
发现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
原文:http://blog.csdn.net/jmppok/article/details/44566741
内容总结
以上是互联网集市为您收集整理的自己写的Java 数据结构Tree全部内容,希望文章能够帮你解决自己写的Java 数据结构Tree所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。