NumberTree.java
5.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package com.subsidy.util.tree;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
@Data
public class NumberTree {
private String id;
private List<Node> nodeList;
private String separator;
private int idSeq;
private SerialNumber number;
public NumberTree(String separator, SerialNumber number) {
init();
this.separator = separator;
this.number = number;
}
public void init() {
idSeq = 1;
this.separator = ".";
if (this.nodeList == null) {
nodeList = new ArrayList<Node>();
}
}
/**
* <p>功能描述:根据父节点获取所有子节点。</p>
*/
public List<Node> getChildNodes(Node pNode) {
String pId = pNode.getId();
return getChildNodes(pId);
}
/**
* <p>功能描述:根据父节点获取所有子节点。</p>
*/
public List<Node> getChildNodes(String pId) {
List<Node> childNodes = new ArrayList<Node>();
for (Node n : nodeList) {
if (pId.equals(n.getParentId())) {
childNodes.add(n);
}
}
return childNodes;
}
/**
* <p>功能描述:获取本级值最大的节点。</p>
*/
public Node getMaxNodeForThisLevel(Node pNode) {
List<Node> childList = getChildNodes(pNode);
Node root = getRoot(nodeList);
if (childList.size() <= 0) {
return null;
}
Node maxNode = root;
for (Node node : childList) {
if (maxNode.getNumber().compareTo(node.getNumber()) < 0) {
maxNode = node;
}
}
return maxNode;
}
/**
* <p>功能描述:生成下一个子节点。</p>
*/
public Node generateNextChildNode(Node node) {
Node newNode = null;
Node maxNode = getMaxNodeForThisLevel(node);
String nextNumber = number.firstNumber();
int level = node.getLevel();
if (maxNode != null && !"0".equals(maxNode.getId())) {//本级存在子节点,且非根节点
nextNumber = number.produceNext(maxNode.getNumber());
level = maxNode.getLevel();
}
newNode = new Node(String.valueOf(++idSeq), nextNumber, node.getId(), level);
generateNodeText(newNode, nextNumber);
return newNode;
}
/**
*
* <p>功能描述:获取父节点。</p>
*/
public Node getParentNode(Node node) {
for (Node n : nodeList) {
if (node.getParentId() == n.getId()) {
return n;
}
}
return node;
}
/**
* <p>功能描述:生成节点路径。</p>
*/
public void generateNodeText(Node node, String text) {
if (node == null || "0".equals(node.getId())) {
return;
}
Node pNode = getParentNode(node);
if (!"0".equals(pNode.getId())) {
text = pNode.getText() + separator + text;
}
node.setText(text);
}
/**
* <p>功能描述:遍历所有树节点。</p>
*/
public void traverseNodeList(Node node) {
if(node==null){
node = getRoot(nodeList);
}
List<Node> childNodes = getChildNodes(node);
System.out.println(node.getText());
if (childNodes.size() > 0) {
for (Node n : childNodes) {
traverseNodeList(n);
}
}
}
public static void main(String[] args) {
SerialNumber number = new SerialNumber();
NumberTree treeNode = new NumberTree(".", number);
addSomeNodes(treeNode);
treeNode.traverseNodeList(null);
}
/**
* <p>功能描述:获取根节点。</p>
*/
public Node getRoot(List<Node> nodeList) {
Node root = null;
if (nodeList.size() <= 0 || (root = getNodeById(nodeList, "0")) == null) {
root = createRoot();
nodeList.add(root);
}
return root;
}
private Node getNodeById(List<Node> nodeList, String id) {
Node node = null;
if(id!=null){
for (Node n : nodeList) {
if (id.equals(n.getId())) {
node = n;
break;
}
}
}
return node;
}
private Node createRoot() {
Node root = new Node("0", number.rootNumber(), "-1", 0);
root.setText("0");
return root;
}
/**
* <p>功能描述:测试添加节点。</p>
*/
private static Node addSomeNodes(NumberTree tree) {
Node root = tree.getRoot(tree.nodeList);
Node node1 = getNextNode(tree, root);//1
Node node2 = getNextNode(tree, root);//2
Node node3 = getNextNode(tree, root);//3
Node node11 = getNextNode(tree, node1);//1.1
Node node12 = getNextNode(tree, node1);//1.2
Node node21 = getNextNode(tree, node2);//2.1
Node node211 = getNextNode(tree, node21);//2.1.1
Node node212 = getNextNode(tree, node21);//2.1.2
Node node22 = getNextNode(tree, node2);//2.2
Node node221 = getNextNode(tree, node22);//2.2.1
Node node31 = getNextNode(tree, node3);
Node node32 = getNextNode(tree, node3);
Node node311 = getNextNode(tree, node31);
Node node3111 = getNextNode(tree, node311);
return root;
}
public static Node getNextNode(NumberTree tree, Node pNode) {
Node node = tree.generateNextChildNode(pNode);
if (node != null) {
tree.nodeList.add(node);
}
return node;
}
}