-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeUtils.java
More file actions
185 lines (173 loc) · 6.69 KB
/
TreeUtils.java
File metadata and controls
185 lines (173 loc) · 6.69 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
package net.posick.tree;
@SuppressWarnings({"rawtypes","unchecked"})
public class TreeUtils
{
/**
* Adds a node to the tree.
*
* @param refNod The reference node, reference point within tree
* @param newNode The node that is to be added.
* @param mode The node indicator, how the node will be added
*/
public static <T extends Tree> T add(T refNode, T newNode, int mode)
{
switch (mode)
{
case Tree.PARENT:
newNode.setParent(refNode.getParent());
newNode.setFirstChild(refNode);
if (refNode.getParent() != null)
{
refNode.getParent().setFirstChild(newNode);
}
refNode.setParent(newNode);
newNode.setNextSibling(null);
newNode.setPreviousSibling(null);
break;
case Tree.LASTCHILD:
case Tree.CHILD:
// Add a child node. If one already exists replace
// it and assign the current child to be a sibling.
if (refNode.getFirstChild() != null)
{
if (mode == Tree.CHILD)
{
refNode = (T) refNode.getFirstChild();
if (refNode.getParent().getFirstChild() == refNode)
{
refNode.getParent().setFirstChild(newNode);
}
newNode.setParent(refNode.getParent());
newNode.setNextSibling(refNode);
newNode.setPreviousSibling(refNode.getPreviousSibling());
if (refNode.getPreviousSibling() != null)
{
refNode.getPreviousSibling().setNextSibling(newNode);
}
refNode.setPreviousSibling(newNode);
} else if (mode == Tree.LASTCHILD)
{
refNode = (T) refNode.getFirstChild();
while (refNode.getNextSibling() != null)
{
refNode = (T) refNode.getNextSibling();
}
newNode.setParent(refNode.getParent());
newNode.setNextSibling(null);
newNode.setPreviousSibling(refNode);
refNode.setNextSibling(newNode);
}
} else
{
refNode.setFirstChild(newNode);
newNode.setParent(refNode);
newNode.setNextSibling(null);
newNode.setPreviousSibling(null);
}
break;
case Tree.NEXT:
newNode.setParent(refNode.getParent());
newNode.setNextSibling(refNode.getNextSibling());
newNode.setPreviousSibling(refNode);
if (refNode.getNextSibling() != null)
{
refNode.getNextSibling().setPreviousSibling(newNode);
}
refNode.setNextSibling(newNode);
break;
case Tree.PREVIOUS:
if (refNode.getParent().getFirstChild() == refNode)
{
refNode.getParent().setFirstChild(newNode);
}
newNode.setParent(refNode.getParent());
newNode.setNextSibling(refNode);
newNode.setPreviousSibling(refNode.getPreviousSibling());
if (refNode.getPreviousSibling() != null)
{
refNode.getPreviousSibling().setNextSibling(newNode);
}
refNode.setPreviousSibling(newNode);
break;
case Tree.LAST:
while (refNode.getNextSibling() != null)
{
refNode = (T) refNode.getNextSibling();
}
newNode.setParent(refNode.getParent());
newNode.setNextSibling(refNode.getNextSibling());
newNode.setPreviousSibling(refNode);
if (refNode.getNextSibling() != null)
{
refNode.getNextSibling().setPreviousSibling(newNode);
}
refNode.setNextSibling(newNode);
break;
}
return (T) newNode;
}
/**
* Replaces a node with another node.
*
* @param oldNode The node to replace.
* @param newNode The node that will replace the old node.
*/
public static <T extends Tree> void replace(T oldNode, T newNode)
{
if (oldNode == null)
{
return;
}
if (newNode == null)
{
remove(oldNode);
} else
{
newNode.setParent(oldNode.getParent());
if (oldNode.getParent() != null && oldNode.getParent().getFirstChild() == oldNode)
{
oldNode.getParent().setFirstChild(newNode);
}
newNode.setNextSibling(oldNode.getNextSibling());
if (oldNode.getNextSibling() != null)
{
oldNode.getNextSibling().setPreviousSibling(newNode);
}
newNode.setPreviousSibling(oldNode.getPreviousSibling());
if (oldNode.getPreviousSibling() != null)
{
oldNode.getPreviousSibling().setNextSibling(newNode);
}
oldNode.setParent(null);
oldNode.setNextSibling(null);
oldNode.setPreviousSibling(null);
}
}
/**
* This method deletes the specified node and all its descendants.
*
* @param refNode The node to be removed from the tree.
*/
public static <T extends Tree> void remove(T refNode)
{
if (refNode == null)
{
return;
}
if (refNode.getParent() != null && refNode.getParent().getFirstChild() == refNode)
{
refNode.getParent().setFirstChild(refNode.getNextSibling());
}
if (refNode.getPreviousSibling() != null && refNode.getPreviousSibling().getNextSibling() == refNode)
{
refNode.getPreviousSibling().setNextSibling(refNode.getNextSibling());
}
if (refNode.getNextSibling() != null && refNode.getNextSibling().getPreviousSibling() == refNode)
{
refNode.getNextSibling().setPreviousSibling(refNode.getPreviousSibling());
}
refNode.setParent(null);
refNode.setNextSibling(null);
refNode.setPreviousSibling(null);
}
}