自己寫的Java 資料結構Tree,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:2level:1 node:2 parent:1 childs:1level:2 node:4 parent:2 childs:0level:1 node:3 parent:1 childs:0先序level:0 node:1 parent:null childs:2level:1 node:2 parent:1 childs:1level:1 node:3 parent:1 childs:0level:2 node:4 parent:2 childs:0