Self-written Java Data Structure Tree and java Data Structure tree
I found that Java does not have a Tree and I simply wrote one.
You can use new to create a Tree and then obtain the root Node. Each Node has its own Data and can obtain the level, parent Node, and child Node list of the current Node. You can add, locate, and delete child nodes on a Node.
The Code is as follows:
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: March 23, 2015 3:12:50 * Copyright: Copyright (C) 2014-2015 * Company Lenovo LTD. * All rights Reserved, Designed By Lenovo CIC. * // ** implementation description of the class Tree: TODO class implementation description ** @ author ligh4 3:12:50, December 4, March 23, 2015 */public class T Ree <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 ;}}
Test:
Import java. util. list;/*** Project: JavaTest * FileName: TestTree. java * @ Description: TODO * @ author: ligh4 * @ version V1.0 * Createdate: March 23, 2015 3:05:10 * Copyright: Copyright (C) 2014-2015 * Company Lenovo LTD. * All rights Reserved, Designed By Lenovo CIC. * // implementation description of the *** class TestTree: TODO class implementation description ** @ author ligh4 3:05:10 on April 4, March 23, 2015 */public class TestTree {public static void main (String [] ar Gs) {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 in the middle order. out. println ("Middle Order:"); printTree (root); // first order System. out. println ("First Order"); printTree2 (root);} public static void printTree (Tree. node node) {List <Tree. node> childs = node. getchilds (); Tree. node paren T = 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 );}}}}
Test results:
Central order: 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