Data type
package org.mo.common.structure.tree;public class student implements java.io.serializable {/** * */private static final long serialversionuid = 1l;private int id;private string name;private int Age;public int getid () {return id;} Public void setid (Int id) {this.id = id;} Public string getname () {return name;} Public void setname (String name) {this.name = name;} Public int getage () {return age;} Public void setage (Int age) {this.age = age;} @Overridepublic string tostring () {return "student [id=" + id + " , name= " + name + ", age= " + age + "] ";} @Overridepublic int hashcode () {final int prime = 31;int result = 1;result = prime * result + age;result = prime * result + id;result = prime * result + (name == null) ? 0 : name.hashcode ()); return result;} @Overridepublic boolean equals (object obj) {if (this == obj) return true;if (obj == null) return false;if (GetClass () != obj.getclass ()) return false; student other = (Student) obj;if (age != other.age) return false;if (id != other.id) return false;if (name == null) {if (other.name != null) Return false;} else if (!name.equals (other.name)) return false;return true;}
Node type
package org.mo.common.structure.tree;public class pnode<t> implements java.io.serializable {/** * */private static final long serialversionuid = 1l;private t data;// node Data private int parent;// Parent position Public pnode () {super ();} Public pnode (T data) {this.data = data;} Public pnode (t data, int parent) {this.data = data;this.parent = Parent;} Public t getdata () {return data;} Public void setdata (T data) {this.data = data;} Public int getparent () {return parent;} Public void setparent (int parent) {this.parent = parent;} @Overridepublic string tostring () {return "Pnode [data=" + data + ", parent=" + parent + "]";} @Overridepublic int hashCode () {final int prime = 31;int result = 1;result = prime * result + ((Data == null) ? 0 : data.hashcode ()); result = prime * result + parent;return result;} @Overridepublic boolean equals (object obj) {if (this == obj) return true;if (obj == null) return false;if (GetClass () != obj.getclass ()) return false; @SuppressWarnings ("Unchecked") pnode<t> other = (pnode<t>) obj;if (Data == null) {if (Other.data != null) Return false;} else if (!data.equals (Other.data)) return false;if (parent != other.parent) Return false;return true;}}
Tree type
package org.mo.common.structure.tree;import java.util.arraylist;import java.util.list;/** * Parent Representation * * @author moziqi * */public class PTree< E> {private final int default_tree_size = 100;private pnode<e>[] pNodes; // Knot Point Group private int treesize;private int nodenums;// node/** * to specify root node creation tree * * @param data */@SuppressWarnings ("unchecked") public ptree (E data) {treesize = default_tree_size;pnodes = new pnode[ treesize]; // such writing although can pass, but not good, have interest oneself to understand why pnodes[0] = new pnode<e> (data, -1) ;// -1 denotes root node nodenums++;} /** * to specify root nodes, specify treesize to create trees * * @param data * @param treesize */@SuppressWarnings ("Unchecked") Public ptree (e data, int treesize) { This.Treesize = treesize;pnodes = new pnode[treesize];p Nodes[0] = new pnode <E> (data, -1); nodenums++;} /** * Add sub-nodes * * @param data * @param parent */for a specified node Public void addnode (e data, pnode<e> parent) {for (int i = 0; i < treesize; i++) {if (pnodes[i] == null) {pNodes[i] = new PNode<E> (Data, getpos (parent)); Nodenums++;return;}} Throw new runtimeexception ("The tree is full and cannot add new nodes"); /** * determine if tree is empty * * @return */public boolean isempty () { Return pnodes[0] == null;} /** * return root node * * @return */public pnode<e> getroot () { if (IsEmpty ()) {return null;} else {return pnodes[0];}} /** * returns all the child nodes of the specified node (not the leaf node). * * @param parent * @return */public List<PNode<E>> GetChildren (pnode<e> parent) {List<PNode<E>> list = new Arraylist<pnode<e>> ();for (int i = 0; i < treesize; i++ ) {if (Pnodes[i] != null && pnodes[i].getparent () != getpos ( Parent) {list.add (Pnodes[i]);}} Return list;} /** * returns the parent node of the specified node (non-root node) * * @param pNode * @return */public pnode<e> getparent (Pnode<e> pnode) {if (pnode != null) { Return pnodes[pnode.getparent ()];} Return null;} /** * returns the node that contains the specified value * * @param node * @return */public Int getpos (Pnode<e> node) {for (int i = 0; i < treesize; i++) &NBSP;{//&NFind the specified node if (pnodes[i].equals (node)) {return i;} Return -1;} /** * returns the depth of the tree * * @return */public int getdeep () {// Maximum depth int max = 0;for for recording nodes (int i = 0; i < treesize && pnodes[i] != null; i++) {// Initialize the depth int def = 1;//of this node m records the position of the parent node of the current node int m = pnodes[i].getparent ();// if its parent node exists while (m != -1 && pnodes[m] != null) {// Continue searching parent node m = pnodes[m].getparent (); def++;} if (Max < def) {max = def;}} return maximum depth Return max;}}
Test
package org.mo.common.structure.tree;import java.util.list;import org.junit.test;public class ptreetest {@Testpublic void testptree () {// build a tree student data = new student ();d Ata.setid (1);d ata.setname ("Test1");d Ata.setage (19); Ptree<student> ptree = new ptree<student> (data);// get the parent node pnode<student > parent = new PNode<Student> (data);p arent.setparent ( -1);// traverse all nodes list< Pnode<student>> children = ptree.getchildren (parent); System.out.println ("All Nodes:");for (Pnode<student> node : children) { System.out.println (Node.tostring ());} System.out.print ("depth:"); Int deep = ptree.getdeep (); System.out.println (deep);// Add one more node student data2 = new student ();d Ata2.setid (2); Data2.setname ("Test1" + 2);d ata2.setage (19 + 2);p Tree.addnode (data2, parent); Pnode<Student> parent2 = new PNode<Student> (DATA2); List<pnode<student>> children2 = ptree.getchildren (Parent2); System.out.println ("All Nodes:");for (PNODE<STUDENT>&NBSP;NODE&NBSP;:&NBSP;CHILDREN2) { System.out.println (Node.tostring ());} System.out.print ("depth:"); Int deep2 = ptree.getdeep (); System.out.println (DEEP2);}}
Reference:
http://justsee.iteye.com/blog/1097166
(Java) tree-parent representation