-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDGHNode.java
122 lines (102 loc) · 2.61 KB
/
DGHNode.java
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
package datafly;
import java.util.ArrayList;
import java.util.Enumeration;
import javax.swing.tree.TreeNode;
/**
*
* @author adenugad
*/
public class DGHNode implements TreeNode {
ArrayList<DGHNode> children = new ArrayList<>();
DGHNode parent = null;
String data = "";
int level = Integer.MIN_VALUE;
public DGHNode(){
}
public DGHNode(String data){
this.data = data;
}
public void setLevel(int i){
level = i;
}
public DGHNode(String data, DGHNode parent, ArrayList<DGHNode> children){
this.data = data;
this.parent = parent.copy();//have to define copy method
//this.children = children;
for(int i = 0; i < children.size();i++){
this.children.add(children.get(i).copy());
}
}
@Override
public TreeNode getChildAt(int childIndex) {
return children.get(childIndex);
}
@Override
public int getChildCount() {
return children.size();
}
@Override
public TreeNode getParent() {
return parent;
}
@Override
public int getIndex(TreeNode node) {
for(int i = 0; i < children.size(); i++){
if(children.get(i) == node){
return i;
}
}
return -1;
}
@Override
public boolean isLeaf() {
return children.isEmpty();
}
@Override
public Enumeration children() {
return (Enumeration)children.iterator();
}
public ArrayList<DGHNode> getChildren(){
return children;
}
/**
* I won't be using this method
* @return
*/
@Override
public boolean getAllowsChildren() {
return true;
}
public void setData(String inData){
data = inData;
}
public String getData(){
return data ;
}
public void setParent(String inData){
parent = new DGHNode(inData);
}
public void setParent(DGHNode parent){
this.parent = parent;
}
public DGHNode copy(){
DGHNode newNode = new DGHNode();
newNode.setData(data);
newNode.setParent(parent.getData());
for(int i = 0; i < children.size(); i++){
newNode.children.add(i, children.get(i));
}
return newNode;
}
public boolean isNull(){
return data.equals("");
}
public boolean hasChild(DGHNode node){
for(int i = 0; i < children.size(); i++){
if(children.get(i).getData().equalsIgnoreCase(node.getData())){
return true;
}
}
return false;
}
}