怎样用Java实现一个多叉树数据结构java实现

本系列文章主要学习如何使用JAVA语言以邻接表的方式实现了数据结构---图(Graph),这是第一篇文章,学习如何用JAVA来表示图的顶点。从数据的表示方法来说,有二种表示图的方式:一种是邻接矩阵,其实是一个二维数组;一种是邻接表,其实是一个顶点表,每个顶点又拥有一个边列表。下图是图的邻接表表示。
从图中可以看出,图的实现需要能够表示顶点表,能够表示边表。邻接表指是的哪部分呢?每个顶点都有一个邻接表,一个指定顶点的邻接表中,起始顶点表示边的起点,其他顶点表示边的终点。这样,就可以用邻接表来实现边的表示了。如顶点V0的邻接表如下:
与V0关联的边有三条,因为V0的邻接表中有三个顶点(不考虑V0)。
&2,具体分析
先来分析边表:
在图中如何来表示一条边?很简单,就是:起始顶点指向结束顶点、就是顶点对&startVertex, endVertex&。在这里,为了考虑边带有权值的情况,单独设计一个类Edge.java,作为Vertex.java的内部类,Edge.java如下:
1 protected class Edge implements java.io.Serializable {
private VertexInterface&T&// 终点
private double//权值
Edge类中只有两个属性,vertex 用来表示顶点,该顶点是边的终点。weight 表示边的权值。若不考虑带权的情况,就不需要weight属性,那么可以直接定义一个顶点列表 来存放 终点 就可以表示边了。这是因为:这些属性是定义在Vertex.java中,而Vertex本身就表示顶点,如果在Vertex内部定义一个List存放终点,那么该List再加上Vertex所表示的顶点本身,就可以表示与起点邻接的各个点了(称之为这个 起点的邻接表)。这样的边的特点是:边的所有的起始点都相同。
但是为了表示带权的边,因此,新增加weight属性,并用类Edge来封装,这样不管是带权的边还是不带权的边都可以用同一个Edge类来表示。不带权的边将weight赋值为0即可。
再分析顶点表:
定义接口VertexInterface&T&表示顶点的接口,所有的顶点都需要实现这个接口,该接口中定义了顶点的基本操作,如:判断顶点是否有邻接点,将顶点与另一个顶点连接起来...。其次,顶点表中的每个顶点有两个域,一个是标识域:V0,V1,V2,V3 。一个是指针域,指针域指向一个"单链表"。综上,设计一个类Vertex.java 用来表示顶点,其数据域如下:
class Vertex&T& implements VertexInterface&T&, java.io.Serializable {
private T//标识标点,可以用不同类型来标识顶点如String,Integer....
private List&Edge& edgeL//到该顶点邻接点的边,实际以java.util.LinkedList存储
private boolean//标识顶点是否已访问
private VertexInterface&T& previousV//该顶点的前驱顶点
private double//顶点的权值,与边的权值要区别开来
现在一一解释Vertex类中定义的各个属性:
label : 用来标识顶点,如图中的 V0,V1,V2,V3,在实际代码中,V0...V3 以字符串的形式表示,就可以用来标识不同的顶点了。因此,需要在Vertex类中添加获得顶点标识的方法---getLabel()
public T getLabel() {
edgeList : 存放与该顶点关联的边。从上面Edge.java中可以看到,Edge的实质是&顶点&,因为,Edge类除去wight属性,就只剩表示顶点的vertex属性了。借助edgeList,当给定一个顶点时,就可以访问该顶点的所有邻接点。因此,Vertex.java中就需要实现根据edgeList中存放的边来遍历 某条边的终点(也即相应顶点的各个邻接点) 的迭代器了。
1 public Iterator&VertexInterface&T&& getNeighborInterator() {
return new NeighborIterator();
迭代器的实现如下:
1 /**Task: 遍历该顶点邻接点的迭代器--为 getNeighborInterator()方法 提供迭代器
* 由于顶点的邻接点以边的形式存储在java.util.List中,因此借助List的迭代器来实现
* 由于顶点的邻接点由Edge类封装起来了--见Edge.java的定义的第一个属性
* 因此,首先获得遍历Edge对象的迭代器,再根据获得的Edge对象解析出邻接点对象
private class NeighborIterator implements Iterator&VertexInterface&T&&{
Iterator&Edge& edgesI
private NeighborIterator() {
edgesIterator = edgeList.iterator();//获得遍历edgesList 的迭代器
public boolean hasNext() {
return edgesIterator.hasNext();
public VertexInterface&T& next() {
VertexInterface&T& nextNeighbor = null;
if(edgesIterator.hasNext()){
Edge edgeToNextNeighbor = edgesIterator.next();//LinkedList中存储的是Edge
nextNeighbor = edgeToNextNeighbor.getEndVertex();//从Edge对象中取出顶点
throw new NoSuchElementException();
return nextN
public void remove() {
throw new UnsupportedOperationException();
visited : 之所以给每个顶点设置一个用来标记它是否被访问的属性,是因为:实现一个数据结构,是要用它去完成某些功能的,如遍历、查找&& 而在图的遍历过程中,就需要标记某个顶点是否被访问了,因此:设置该属性以便实现这些功能。那么,也就需要定义获取顶点是否被访问的isVisited()方法了。
public boolean isVisited() {
previousVertex 属性 ,在求图中某两个顶点之间的最短路径时,在从起始顶点遍历过程中,需要记录下遍历到某个顶点时的前驱顶点, previousVertex 属性就派上用场了。因此,需要有判断和获取顶点的前驱顶点的方法:
public boolean hasPredecessor() {//判断顶点是否有前驱顶点
return this.previousVertex != null;
public VertexInterface&T& getPredecessor() {//获得前驱顶点
return this.previousV
cost 属性:用来表示顶点的权值。注意,顶点的权值与边的权值是不同的。比如求无权图(默认是边不带权值)的最短路径时,如何求出顶点A到顶点B的最短的路径?由定义,该最短路径其实就是A走到B经历的最少边数目。因此,就可以用 cost 属性来记录A到B之间的距离是多少了。比如说,A 先走到 C 再走到B;初始时,A的 cost = 0,由于 A 是 C 的前驱,A到B需要经历C,C 的 cost 就是 c.previousVertex.cost + 1,直至 B,就可以求出 A 到 B 的最短路径了。详细算法及实现将会在第二篇博客中给出。
因此,针对 cost 属性,Vertex.java需要实现的方法如下:
1 public void setCost(double newCost) {
cost = newC
4 public double getCost() {
从上可以看出,设计一个数据结构时,该数据结构需要包含哪些属性不是随意的,而是先确定该数据结构需要完成哪些功能(如,图的DFS、BFS、拓扑排序、最短路径),这些功能的实现需要借助哪些属性(如,求最短路径需要记录每个顶点的前驱顶点,就需要 previousVertex)。然后,去定义这些属性以及关于该属性的基本操作。设计一个合适的数据结构,当借助该数据结构来实现算法时,可以有效地降低算法的实现难度和复杂度!
Vertex.java的完整代码如下:
3 import java.util.I
4 import java.util.LinkedL
5 import java.util.L
6 import java.util.NoSuchElementE
8 class Vertex&T& implements VertexInterface&T&, java.io.Serializable {
private T//标识标点,可以用不同类型来标识顶点如String,Integer....
private List&Edge& edgeL//到该顶点邻接点的边,实际以java.util.LinkedList存储
private boolean//标识顶点是否已访问
private VertexInterface&T& previousV//该顶点的前驱顶点
private double//顶点的权值,与边的权值要区别开来
public Vertex(T vertexLabel){
label = vertexL
edgeList = new LinkedList&Edge&();//是Vertex的属性,说明每个顶点都有一个edgeList用来存储所有与该顶点关系的边
visited = false;
previousVertex = null;
*Task: 这里用了一个单独的类来表示边,主要是考虑到带权值的边
*可以看出,Edge类封装了一个顶点和一个double类型变量
*若不需要考虑权值,可以不需要单独创建一个Edge类来表示边,只需要一个保存顶点的列表即可
* @author hapjin
protected class Edge implements java.io.Serializable {
private VertexInterface&T&// 终点
private double//权值
//Vertex 类本身就代表顶点对象,因此在这里只需提供 endVertex,就可以表示一条边了
protected Edge(VertexInterface&T& endVertex, double edgeWeight){
vertex = endV
weight = edgeW
protected VertexInterface&T& getEndVertex(){
protected double getWeight(){
/**Task: 遍历该顶点邻接点的迭代器--为 getNeighborInterator()方法 提供迭代器
* 由于顶点的邻接点以边的形式存储在java.util.List中,因此借助List的迭代器来实现
* 由于顶点的邻接点由Edge类封装起来了--见Edge.java的定义的第一个属性
* 因此,首先获得遍历Edge对象的迭代器,再根据获得的Edge对象解析出邻接点对象
private class NeighborIterator implements Iterator&VertexInterface&T&&{
Iterator&Edge& edgesI
private NeighborIterator() {
edgesIterator = edgeList.iterator();//获得遍历edgesList 的迭代器
public boolean hasNext() {
return edgesIterator.hasNext();
public VertexInterface&T& next() {
VertexInterface&T& nextNeighbor = null;
if(edgesIterator.hasNext()){
Edge edgeToNextNeighbor = edgesIterator.next();//LinkedList中存储的是Edge
nextNeighbor = edgeToNextNeighbor.getEndVertex();//从Edge对象中取出顶点
throw new NoSuchElementException();
return nextN
public void remove() {
throw new UnsupportedOperationException();
/**Task: 生成一个遍历该顶点所有邻接边的权值的迭代器
* 权值是Edge类的属性,因此先获得一个遍历Edge对象的迭代器,取得Edge对象,再获得权值
* @author hapjin
* @param &Double& 权值的类型
private class WeightIterator implements Iterator{//这里不知道为什么,用泛型报编译错误???
private Iterator&Edge& edgesI
private WeightIterator(){
edgesIterator = edgeList.iterator();
public boolean hasNext() {
return edgesIterator.hasNext();
public Object next() {
if(edgesIterator.hasNext()){
Edge edge = edgesIterator.next();
result = edge.getWeight();
else throw new NoSuchElementException();
return (Object)//从迭代器中取得结果时,需要强制转换成Double
public void remove() {
throw new UnsupportedOperationException();
public T getLabel() {
public void visit() {
this.visited = true;
public void unVisit() {
this.visited = false;
public boolean isVisited() {
public boolean connect(VertexInterface&T& endVertex, double edgeWeight) {
// 将"边"(边的实质是顶点)插入顶点的邻接表
boolean result = false;
if(!this.equals(endVertex)){//顶点互不相同
Iterator&VertexInterface&T&& neighbors = this.getNeighborInterator();
boolean duplicateEdge = false;
while(!duplicateEdge && neighbors.hasNext()){//保证不添加重复的边
VertexInterface&T& nextNeighbor = neighbors.next();
if(endVertex.equals(nextNeighbor)){
duplicateEdge = true;
}//end while
if(!duplicateEdge){
edgeList.add(new Edge(endVertex, edgeWeight));//添加一条新边
result = true;
public boolean connect(VertexInterface&T& endVertex) {
return connect(endVertex, 0);
public Iterator&VertexInterface&T&& getNeighborInterator() {
return new NeighborIterator();
public Iterator getWeightIterator() {
return new WeightIterator();
public boolean hasNeighbor() {
return !(edgeList.isEmpty());//邻接点实质是存储是List中
public VertexInterface&T& getUnvisitedNeighbor() {
VertexInterface&T& result = null;
Iterator&VertexInterface&T&& neighbors = getNeighborInterator();
while(neighbors.hasNext() && result == null){//获得该顶点的第一个未被访问的邻接点
VertexInterface&T& nextNeighbor = neighbors.next();
if(!nextNeighbor.isVisited())
result = nextN
public void setPredecessor(VertexInterface&T& predecessor) {
this.previousVertex =
public VertexInterface&T& getPredecessor() {
return this.previousV
public boolean hasPredecessor() {
return this.previousVertex != null;
public void setCost(double newCost) {
cost = newC
public double getCost() {
//判断两个顶点是否相同
public boolean equals(Object other){
if((other == null) || (getClass() != other.getClass()))
result = false;
Vertex&T& otherVertex = (Vertex&T&)
result = label.equals(otherVertex.label);//节点是否相同最终还是由标识 节点类型的类的equals() 决定
阅读(...) 评论()Java 多叉树的实现,完成树的初始化和遍历。包括两个文件(TreeNode.java和TreeHelper.java)&
TreeNode类完成树节点的数据结构,TreeHelper类通过输入一个TreeNode列表,生成一颗有一个树根的树!其它函数接口自己看看就明白了,希望对你有帮助。&
一:树节点的定义(TreeNode.java)&
Java代码&&
package&com.&&
import&java.util.L&&
import&java.util.ArrayL&&
import&java.io.S&&
public&class&TreeNode&implements&Serializable&{&&
&&&&private&int&parentId;&&
&&&&private&int&selfId;&&
&&&&protected&String&nodeN&&
&&&&protected&Object&&&
&&&&protected&TreeNode&parentN&&
&&&&protected&List&TreeNode&&childL&&
&&&&public&TreeNode()&{&&
&&&&&&&&initChildList();&&
&&&&public&TreeNode(TreeNode&parentNode)&{&&
&&&&&&&&this.getParentNode();&&
&&&&&&&&initChildList();&&
&&&&public&boolean&isLeaf()&{&&
&&&&&&&&if&(childList&==&null)&{&&
&&&&&&&&&&&&return&true;&&
&&&&&&&&}&else&{&&
&&&&&&&&&&&&if&(childList.isEmpty())&{&&
&&&&&&&&&&&&&&&&return&true;&&
&&&&&&&&&&&&}&else&{&&
&&&&&&&&&&&&&&&&return&false;&&
&&&&&&&&&&&&}&&
&&&&&&&&}&&
&&&&public&void&addChildNode(TreeNode&treeNode)&{&&
&&&&&&&&initChildList();&&
&&&&&&&&childList.add(treeNode);&&
&&&&public&void&initChildList()&{&&
&&&&&&&&if&(childList&==&null)&&
&&&&&&&&&&&&childList&=&new&ArrayList&TreeNode&();&&
&&&&public&boolean&isValidTree()&{&&
&&&&&&&&return&true;&&
&&&&public&List&TreeNode&&getElders()&{&&
&&&&&&&&List&TreeNode&&elderList&=&new&ArrayList&TreeNode&();&&
&&&&&&&&TreeNode&parentNode&=&this.getParentNode();&&
&&&&&&&&if&(parentNode&==&null)&{&&
&&&&&&&&&&&&return&elderL&&
&&&&&&&&}&else&{&&
&&&&&&&&&&&&elderList.add(parentNode);&&
&&&&&&&&&&&&elderList.addAll(parentNode.getElders());&&
&&&&&&&&&&&&return&elderL&&
&&&&&&&&}&&
&&&&public&List&TreeNode&&getJuniors()&{&&
&&&&&&&&List&TreeNode&&juniorList&=&new&ArrayList&TreeNode&();&&
&&&&&&&&List&TreeNode&&childList&=&this.getChildList();&&
&&&&&&&&if&(childList&==&null)&{&&
&&&&&&&&&&&&return&juniorL&&
&&&&&&&&}&else&{&&
&&&&&&&&&&&&int&childNumber&=&childList.size();&&
&&&&&&&&&&&&for&(int&i&=&0;&i&&&childN&i++)&{&&
&&&&&&&&&&&&&&&&TreeNode&junior&=&childList.get(i);&&
&&&&&&&&&&&&&&&&juniorList.add(junior);&&
&&&&&&&&&&&&&&&&juniorList.addAll(junior.getJuniors());&&
&&&&&&&&&&&&}&&
&&&&&&&&&&&&return&juniorL&&
&&&&&&&&}&&
&&&&public&List&TreeNode&&getChildList()&{&&
&&&&&&&&return&childL&&
&&&&public&void&deleteNode()&{&&
&&&&&&&&TreeNode&parentNode&=&this.getParentNode();&&
&&&&&&&&int&id&=&this.getSelfId();&&
&&&&&&&&if&(parentNode&!=&null)&{&&
&&&&&&&&&&&&parentNode.deleteChildNode(id);&&
&&&&&&&&}&&
&&&&public&void&deleteChildNode(int&childId)&{&&
&&&&&&&&List&TreeNode&&childList&=&this.getChildList();&&
&&&&&&&&int&childNumber&=&childList.size();&&
&&&&&&&&for&(int&i&=&0;&i&&&childN&i++)&{&&
&&&&&&&&&&&&TreeNode&child&=&childList.get(i);&&
&&&&&&&&&&&&if&(child.getSelfId()&==&childId)&{&&
&&&&&&&&&&&&&&&&childList.remove(i);&&
&&&&&&&&&&&&&&&&return;&&
&&&&&&&&&&&&}&&
&&&&&&&&}&&
&&&&public&boolean&insertJuniorNode(TreeNode&treeNode)&{&&
&&&&&&&&int&juniorParentId&=&treeNode.getParentId();&&
&&&&&&&&if&(this.parentId&==&juniorParentId)&{&&
&&&&&&&&&&&&addChildNode(treeNode);&&
&&&&&&&&&&&&return&true;&&
&&&&&&&&}&else&{&&
&&&&&&&&&&&&List&TreeNode&&childList&=&this.getChildList();&&
&&&&&&&&&&&&int&childNumber&=&childList.size();&&
&&&&&&&&&&&&boolean&insertF&&
&&&&&&&&&&&&for&(int&i&=&0;&i&&&childN&i++)&{&&
&&&&&&&&&&&&&&&&TreeNode&childNode&=&childList.get(i);&&
&&&&&&&&&&&&&&&&insertFlag&=&childNode.insertJuniorNode(treeNode);&&
&&&&&&&&&&&&&&&&if&(insertFlag&==&true)&&
&&&&&&&&&&&&&&&&&&&&return&true;&&
&&&&&&&&&&&&}&&
&&&&&&&&&&&&return&false;&&
&&&&&&&&}&&
&&&&public&TreeNode&findTreeNodeById(int&id)&{&&
&&&&&&&&if&(this.selfId&==&id)&&
&&&&&&&&&&&&return&this;&&
&&&&&&&&if&(childList.isEmpty()&||&childList&==&null)&{&&
&&&&&&&&&&&&return&null;&&
&&&&&&&&}&else&{&&
&&&&&&&&&&&&int&childNumber&=&childList.size();&&
&&&&&&&&&&&&for&(int&i&=&0;&i&&&childN&i++)&{&&
&&&&&&&&&&&&&&&&TreeNode&child&=&childList.get(i);&&
&&&&&&&&&&&&&&&&TreeNode&resultNode&=&child.findTreeNodeById(id);&&
&&&&&&&&&&&&&&&&if&(resultNode&!=&null)&{&&
&&&&&&&&&&&&&&&&&&&&return&resultN&&
&&&&&&&&&&&&&&&&}&&
&&&&&&&&&&&&}&&
&&&&&&&&&&&&return&null;&&
&&&&&&&&}&&
&&&&public&void&traverse()&{&&
&&&&&&&&if&(selfId&&&0)&&
&&&&&&&&&&&&return;&&
&&&&&&&&print(this.selfId);&&
&&&&&&&&if&(childList&==&null&||&childList.isEmpty())&&
&&&&&&&&&&&&return;&&
&&&&&&&&int&childNumber&=&childList.size();&&
&&&&&&&&for&(int&i&=&0;&i&&&childN&i++)&{&&
&&&&&&&&&&&&TreeNode&child&=&childList.get(i);&&
&&&&&&&&&&&&child.traverse();&&
&&&&&&&&}&&
&&&&public&void&print(String&content)&{&&
&&&&&&&&System.out.println(content);&&
&&&&public&void&print(int&content)&{&&
&&&&&&&&System.out.println(String.valueOf(content));&&
&&&&public&void&setChildList(List&TreeNode&&childList)&{&&
&&&&&&&&this.childList&=&childL&&
&&&&public&int&getParentId()&{&&
&&&&&&&&return&parentId;&&
&&&&public&void&setParentId(int&parentId)&{&&
&&&&&&&&this.parentId&=&parentId;&&
&&&&public&int&getSelfId()&{&&
&&&&&&&&return&selfId;&&
&&&&public&void&setSelfId(int&selfId)&{&&
&&&&&&&&this.selfId&=&selfId;&&
&&&&public&TreeNode&getParentNode()&{&&
&&&&&&&&return&parentN&&
&&&&public&void&setParentNode(TreeNode&parentNode)&{&&
&&&&&&&&this.parentNode&=&parentN&&
&&&&public&String&getNodeName()&{&&
&&&&&&&&return&nodeN&&
&&&&public&void&setNodeName(String&nodeName)&{&&
&&&&&&&&this.nodeName&=&nodeN&&
&&&&public&Object&getObj()&{&&
&&&&&&&&return&&&
&&&&public&void&setObj(Object&obj)&{&&
&&&&&&&&this.obj&=&&&
二:TreeHelper.java&
Java代码&&
package&com.&&
import&java.util.L&&
import&java.util.I&&
import&java.util.ArrayL&&
import&java.util.HashM&&
public&class&TreeHelper&{&&
&&&&private&TreeNode&&&
&&&&private&List&TreeNode&&tempNodeL&&
&&&&private&boolean&isValidTree&=&true;&&
&&&&public&TreeHelper()&{&&
&&&&public&TreeHelper(List&TreeNode&&treeNodeList)&{&&
&&&&&&&&tempNodeList&=&treeNodeL&&
&&&&&&&&generateTree();&&
&&&&public&static&TreeNode&getTreeNodeById(TreeNode&tree,&int&id)&{&&
&&&&&&&&if&(tree&==&null)&&
&&&&&&&&&&&&return&null;&&
&&&&&&&&TreeNode&treeNode&=&tree.findTreeNodeById(id);&&
&&&&&&&&return&treeN&&
&&&&public&void&generateTree()&{&&
&&&&&&&&HashMap&nodeMap&=&putNodesIntoMap();&&
&&&&&&&&putChildIntoParent(nodeMap);&&
&&&&protected&HashMap&putNodesIntoMap()&{&&
&&&&&&&&int&maxId&=&Integer.MAX_VALUE;&&
&&&&&&&&HashMap&nodeMap&=&new&HashMap&String,&TreeNode&();&&
&&&&&&&&Iterator&it&=&tempNodeList.iterator();&&
&&&&&&&&while&(it.hasNext())&{&&
&&&&&&&&&&&&TreeNode&treeNode&=&(TreeNode)&it.next();&&
&&&&&&&&&&&&int&id&=&treeNode.getSelfId();&&
&&&&&&&&&&&&if&(id&&&maxId)&{&&
&&&&&&&&&&&&&&&&maxId&=&&&
&&&&&&&&&&&&&&&&this.root&=&treeN&&
&&&&&&&&&&&&}&&
&&&&&&&&&&&&String&keyId&=&String.valueOf(id);&&
&&&&&&&&&&&&nodeMap.put(keyId,&treeNode);&&
&&&&&&&&&&&&&&
&&&&&&&&}&&
&&&&&&&&return&nodeM&&
&&&&protected&void&putChildIntoParent(HashMap&nodeMap)&{&&
&&&&&&&&Iterator&it&=&nodeMap.values().iterator();&&
&&&&&&&&while&(it.hasNext())&{&&
&&&&&&&&&&&&TreeNode&treeNode&=&(TreeNode)&it.next();&&
&&&&&&&&&&&&int&parentId&=&treeNode.getParentId();&&
&&&&&&&&&&&&String&parentKeyId&=&String.valueOf(parentId);&&
&&&&&&&&&&&&if&(nodeMap.containsKey(parentKeyId))&{&&
&&&&&&&&&&&&&&&&TreeNode&parentNode&=&(TreeNode)&nodeMap.get(parentKeyId);&&
&&&&&&&&&&&&&&&&if&(parentNode&==&null)&{&&
&&&&&&&&&&&&&&&&&&&&this.isValidTree&=&false;&&
&&&&&&&&&&&&&&&&&&&&return;&&
&&&&&&&&&&&&&&&&}&else&{&&
&&&&&&&&&&&&&&&&&&&&parentNode.addChildNode(treeNode);&&
&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&}&&
&&&&&&&&&&&&}&&
&&&&&&&&}&&
&&&&protected&void&initTempNodeList()&{&&
&&&&&&&&if&(this.tempNodeList&==&null)&{&&
&&&&&&&&&&&&this.tempNodeList&=&new&ArrayList&TreeNode&();&&
&&&&&&&&}&&
&&&&public&void&addTreeNode(TreeNode&treeNode)&{&&
&&&&&&&&initTempNodeList();&&
&&&&&&&&this.tempNodeList.add(treeNode);&&
&&&&public&boolean&insertTreeNode(TreeNode&treeNode)&{&&
&&&&&&&&boolean&insertFlag&=&root.insertJuniorNode(treeNode);&&
&&&&&&&&return&insertF&&
&&&&public&static&List&TreeNode&&changeEnititiesToTreeNodes(List&entityList)&{&&
&&&&&&&&OrganizationEntity&orgEntity&=&new&OrganizationEntity();&&
&&&&&&&&List&TreeNode&&tempNodeList&=&new&ArrayList&TreeNode&();&&
&&&&&&&&TreeNode&treeN&&
&&&&&&&&Iterator&it&=&entityList.iterator();&&
&&&&&&&&while&(it.hasNext())&{&&
&&&&&&&&&&&&orgEntity&=&(OrganizationEntity)&it.next();&&
&&&&&&&&&&&&treeNode&=&new&TreeNode();&&
&&&&&&&&&&&&treeNode.setObj(orgEntity);&&
&&&&&&&&&&&&treeNode.setParentId(orgEntity.getParentId());&&
&&&&&&&&&&&&treeNode.setSelfId(orgEntity.getOrgId());&&
&&&&&&&&&&&&treeNode.setNodeName(orgEntity.getOrgName());&&
&&&&&&&&&&&&tempNodeList.add(treeNode);&&
&&&&&&&&}&&
&&&&&&&&return&tempNodeL&&
&&&&public&boolean&isValidTree()&{&&
&&&&&&&&return&this.isValidT&&
&&&&public&TreeNode&getRoot()&{&&
&&&&&&&&return&&&
&&&&public&void&setRoot(TreeNode&root)&{&&
&&&&&&&&this.root&=&&&
&&&&public&List&TreeNode&&getTempNodeList()&{&&
&&&&&&&&return&tempNodeL&&
&&&&public&void&setTempNodeList(List&TreeNode&&tempNodeList)&{&&
&&&&&&&&this.tempNodeList&=&tempNodeL&&
三 实体OrganizationEntity&
Java代码&&
package&com.&&
public&class&OrganizationEntity&{&&
&&&&public&int&parentId;&&
&&&&public&int&orgId;&&
&&&&public&String&orgN&&
&&&&public&int&getParentId()&{&&
&&&&&&&&return&parentId;&&
&&&&public&void&setParentId(int&parentId)&{&&
&&&&&&&&this.parentId&=&parentId;&&
&&&&public&int&getOrgId()&{&&
&&&&&&&&return&orgId;&&
&&&&public&void&setOrgId(int&orgId)&{&&
&&&&&&&&this.orgId&=&orgId;&&
&&&&public&String&getOrgName()&{&&
&&&&&&&&return&orgN&&
&&&&public&void&setOrgName(String&orgName)&{&&
&&&&&&&&this.orgName&=&orgN&&
本文已收录于以下专栏:
相关文章推荐
转载另外一个关于多叉树的实现类:
TreeNode.Java
view plain
/*  * Copyright Walker Stu...
转载另外一个关于多叉树的实现类:
TreeNode.java
* Copyright Walker Studio
* All Rights Reserved.
package com.T
import java.util.LinkedL
import java.util.Q
import java.util.S
package com.T
public class sk_node {
public sk_node childs[];
sk_node() {...
我有这么个需求,是一张地区表,地区表中包含多层级的地区,如:中国,河北省,邢台市,桥东区。一共有4个层级。数据库字段设计为
在java编程中,经常会用到创建树形结构的菜单或者目录结构,这就需要设计一个树形类,帮助实现类似功能。本文从其他地方摘抄了一个多叉树类设计,包括两个文件(TreeNode.java和TreeHelpe...
public void getTreeNodes()
List list = mapper.getTreeNodes(); //从数据库中获取数据,数据中的节点以ID正序排列
Java 多叉树的简单实现
多叉树,简单地说,与二叉树类似,但叉可能要多的树形结构;类似于计算机文件目录。
package com.T
public class BTNode {
public BTN
public BTNode right...
他的最新文章
讲师:刘文志
讲师:陈伟
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)

我要回帖

更多关于 数据结构顺序表的实现 的文章

 

随机推荐