-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBasicTree.java
More file actions
258 lines (238 loc) · 7.47 KB
/
BasicTree.java
File metadata and controls
258 lines (238 loc) · 7.47 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package com.wuda.tree;
import java.util.LinkedList;
import java.util.List;
/**
* 基本的树型结构.first-child next-sibling方式的实现.树中的节点有一些限制条件
* <ul>
* <li>兄弟节点之间,{@link Node#element}是唯一的,即不能存在任意两个兄弟节点的{@link Node#element}
* 一样.但是如果节点不是兄弟关系,则他们的{@link Node#element}是可以一样的.</li>
* <li>节点的{@link Node#element}虽然是任意类型, 但是我们会检查数据类型,使他只能是<Strong>基本数据类型</Strong>.
* </li>
* <li>root节点是默认生成的,它的{@link Node#element}等于"root",而且至少为root绑定一个子节点,即至少调用一次
* {@link BasicTree#createRelationShip(Node, Node)},然后第一个Node等于
* {@link BasicTree#getRoot()}</li>
* </ul>
*
* @param <T>
* 节点中包含的元素类型
* @author wuda
*/
public class BasicTree<T extends Comparable<T>> {
/**
* 树的根节点.
*/
private Node<T> root = new Node<>(null);
/**
* 创建一个新的节点.
*
* @param element
* 节点的元素
* @return a new node
*/
public Node<T> createNode(T element) {
if (element == null) {
throw new NullPointerException("element 为null");
}
return new Node<>(element);
}
/**
* 创建两个节点的关系.很明显,第一个是父节点,第二个是子节点.
*
* @param parent
* 父节点
* @param child
* 子节点
* @throws AlreadyHasParentException
* 子节点已经拥有父节点
* @throws DuplicateElementException
* 在给定的父节点下,已经有一个子节点的{@link Node#getElement()}和child的相同
*/
public void createRelationShip(Node<T> parent, Node<T> child)
throws AlreadyHasParentException, DuplicateElementException {
if (parent == null) {
throw new NullPointerException("父节点不能为空");
}
if (child.parent != null && !child.parent.equals(parent)) {
throw new AlreadyHasParentException("child 已经拥有了一个父节点,并且这个父节点不是当前提供的父节点");
}
if (find(parent, child.element) != null) {
throw new DuplicateElementException("在给定的父节点下,已经有一个子节点的元素和child的元素相同");
}
if (parent.firstChild == null) {
parent.firstChild = child;
} else {
Node<T> sibling = parent.firstChild;
while (sibling != null && sibling.nextSibling != null) {
sibling = sibling.nextSibling;
}
sibling.nextSibling = child;
}
child.parent = parent;
}
/**
* 寻找父节点下的指定元素的子节点.
*
* @param parent
* 父节点
* @param childElement
* 子节点的元素
* @return 子节点, null-如果没有找到
*/
public Node<T> find(Node<T> parent, T childElement) {
if (parent == null) {
return null;
}
Node<T> child = parent.firstChild;
while (child != null) {
if (child.getElement().compareTo(childElement) == 0) {// 找到
return child;
}
child = child.nextSibling;
}
return null;
}
/**
* 深度优先遍历.数据量多的时候不能调用.
*
* @param start
* 开始节点
* @return 此节点的所有后裔, 包括此节点, 并且此节点一定是位于集合的第一个位置
*/
public List<Node<T>> dfs(Node<T> start) {
if (start == null) {
return null;
}
LinkedList<Node<T>> backtrack = new LinkedList<>();
List<Node<T>> children = new LinkedList<>();
backtrack.addFirst(start);
Node<T> current;
while (!backtrack.isEmpty()) {
current = backtrack.removeFirst();
while (current != null) {
if (current.nextSibling != null) {
backtrack.addFirst(current.nextSibling);
}
children.add(current);
current = current.firstChild;
}
}
return children;
}
/**
* 获取树的根节点.
*
* @return the root
*/
public Node<T> getRoot() {
return root;
}
/**
* 树的节点.
*
* @param <T>
* 元素的类型
* @author wuda
*/
public static class Node<T extends Comparable<T>> {
/**
* 当前节点的第一个子节点.
*/
private Node<T> firstChild = null;
/**
* 当前节点的兄弟节点.
*/
private Node<T> nextSibling = null;
/**
* 当前节点的父节点.
*/
private Node<T> parent = null;
/**
* 兄弟节点之间,element是唯一的,即不能存在任意两个兄弟节点的element一样.如果节点不是兄弟关系,
* 则他们的element是可以一样的.虽然这里是泛型, 但是我们会检查数据类型,使他只能是基本数据类型.
*/
private T element;
/**
* 构建一个节点.
*
* @param element
* 节点的元素.
*/
Node(T element) {
this.element = element;
}
@Override
public String toString() {
return element.toString();
}
/**
* @return the firstChild
*/
Node<T> getFirstChild() {
return firstChild;
}
/**
* @param firstChild
* the firstChild to set
*/
void setFirstChild(Node<T> firstChild) {
this.firstChild = firstChild;
}
/**
* @return the nextSibling
*/
Node<T> getNextSibling() {
return nextSibling;
}
/**
* @param nextSibling
* the nextSibling to set
*/
void setNextSibling(Node<T> nextSibling) {
this.nextSibling = nextSibling;
}
/**
* 获取当前节点的父节点.
*
* @return the parent
*/
public Node<T> getParent() {
return parent;
}
/**
* @param parent
* the parent to set
*/
void setParent(Node<T> parent) {
this.parent = parent;
}
/**
* 获取节点的元素.
*
* @return the element
*/
public T getElement() {
return element;
}
/**
* @param element
* the element to set
*/
void setElement(T element) {
this.element = element;
}
/**
* 获取节点的深度.
*
* @return the depth
*/
public int getDepth() {
int tmpDepth = 0;
Node<T> parent = this.parent;
while (parent != null) {
tmpDepth++;
parent = parent.parent;
}
return tmpDepth;
}
}
}